Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions src/Standards/PSR12/Sniffs/Functions/ReturnTypeDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,35 @@ public function process(File $phpcsFile, int $stackPtr)
$returnType = $phpcsFile->findPrevious(T_NULLABLE, ($returnType - 1));
}

$colon = $phpcsFile->findPrevious(T_COLON, ($returnType - 1), $tokens[$stackPtr]['parenthesis_closer']);
if ($colon === false) {
// Parse error / live coding.
return;
}

if ($tokens[($returnType - 1)]['code'] !== T_WHITESPACE
|| $tokens[($returnType - 1)]['content'] !== ' '
|| $tokens[($returnType - 2)]['code'] !== T_COLON
|| ($returnType - 2) !== $colon
) {
$error = 'There must be a single space between the colon and type in a return type declaration';
if ($tokens[($returnType - 1)]['code'] === T_WHITESPACE
&& $tokens[($returnType - 2)]['code'] === T_COLON
) {
$fix = $phpcsFile->addFixableError($error, $returnType, 'SpaceBeforeReturnType');
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($returnType - 1), ' ');
}
} elseif ($tokens[($returnType - 1)]['code'] === T_COLON) {

$nonWhitespaceToken = $phpcsFile->findNext(T_WHITESPACE, ($colon + 1), $returnType, true);
if ($nonWhitespaceToken !== false) {
$phpcsFile->addError($error, $returnType, 'SpaceBeforeReturnType');
} else {
$fix = $phpcsFile->addFixableError($error, $returnType, 'SpaceBeforeReturnType');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($returnType - 1); $i > $colon; $i--) {
$phpcsFile->fixer->replaceToken($i, '');
}
Comment on lines +83 to +85
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious: any particular reason to walk backwards instead of forwards ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why this is going down versus up. There might have been a good reason, but I can't think of it currently. Would you prefer it to go up instead?


$phpcsFile->fixer->addContentBefore($returnType, ' ');
$phpcsFile->fixer->endChangeset();
}
} else {
$phpcsFile->addError($error, $returnType, 'SpaceBeforeReturnType');
}
}

$colon = $phpcsFile->findPrevious(T_COLON, $returnType);
if ($tokens[($colon - 1)]['code'] !== T_CLOSE_PARENTHESIS) {
$error = 'There must not be a space before the colon in a return type declaration';
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($colon - 1), null, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ function functionName(?string $arg1, ?int &$arg2):
fn (?\DateTime $arg) : ?\DateTime => $arg;

return (!$a ? [ new class { public function b(): c {} } ] : []);

// This should be fixed in one fixer loop iteration.
function multipleWhitespaceTokens():




?int {}
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ function functionName(?string $arg1, ?int &$arg2): ?string {}
fn (?\DateTime $arg): ?\DateTime => $arg;

return (!$a ? [ new class { public function b(): c {} } ] : []);

// This should be fixed in one fixer loop iteration.
function multipleWhitespaceTokens(): ?int {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

// Intentional parse error. There should be a colon between the function parameters and its return type.
$closure = function () null { return null; };
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

// Intentional parse error. There should be a colon between the function 'use' and its return type.
$closure = function ($foo) use ($bar) string { return 'baz'; };
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

// Intentional parse error. There should be a colon between the function parameters and its return type.
function foo($obj = new DateTime(datetime: "now")) null {}
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,32 @@ final class ReturnTypeDeclarationUnitTest extends AbstractSniffTestCase
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
protected function getErrorList()
protected function getErrorList($testFile = '')
{
return [
27 => 1,
28 => 1,
35 => 2,
41 => 2,
48 => 2,
52 => 1,
55 => 1,
56 => 1,
59 => 1,
60 => 1,
62 => 1,
64 => 1,
];
switch ($testFile) {
case 'ReturnTypeDeclarationUnitTest.1.inc':
return [
27 => 1,
28 => 1,
35 => 2,
41 => 2,
48 => 2,
52 => 1,
55 => 1,
56 => 1,
59 => 1,
60 => 1,
62 => 1,
64 => 1,
74 => 1,
];
default:
return [];
}
}


Expand All @@ -54,9 +62,11 @@ protected function getErrorList()
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
protected function getWarningList()
protected function getWarningList($testFile = '')
{
return [];
}
Expand Down