forked from squizlabs/PHP_CodeSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added PSR12.Functions.ReturnTypeDeclaration sniff (ref squizlabs#750)
- Loading branch information
Showing
6 changed files
with
282 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/Standards/PSR12/Sniffs/Functions/ReturnTypeDeclarationSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
/** | ||
* Ensure return types are defined correctly for fucnctions and closures. | ||
* | ||
* @author Greg Sherwood <gsherwood@squiz.net> | ||
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Standards\PSR12\Sniffs\Functions; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Util\Tokens; | ||
|
||
class ReturnTypeDeclarationSniff implements Sniff | ||
{ | ||
|
||
|
||
/** | ||
* Returns an array of tokens this test wants to listen for. | ||
* | ||
* @return array | ||
*/ | ||
public function register() | ||
{ | ||
return [ | ||
T_FUNCTION, | ||
T_CLOSURE, | ||
]; | ||
|
||
}//end register() | ||
|
||
|
||
/** | ||
* Processes this test when one of its tokens is encountered. | ||
* | ||
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token | ||
* in the stack passed in $tokens. | ||
* | ||
* @return void | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
if (isset($tokens[$stackPtr]['parenthesis_opener']) === false | ||
|| isset($tokens[$stackPtr]['parenthesis_closer']) === false | ||
|| $tokens[$stackPtr]['parenthesis_opener'] === null | ||
|| $tokens[$stackPtr]['parenthesis_closer'] === null | ||
) { | ||
return; | ||
} | ||
|
||
$methodProperties = $phpcsFile->getMethodProperties($stackPtr); | ||
if ($methodProperties['return_type'] === '') { | ||
return; | ||
} | ||
|
||
$returnType = $methodProperties['return_type_token']; | ||
if ($tokens[($returnType - 1)]['code'] !== T_WHITESPACE | ||
|| $tokens[($returnType - 1)]['content'] !== ' ' | ||
|| $tokens[($returnType - 2)]['code'] !== T_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), ' '); | ||
} | ||
} else if ($tokens[($returnType - 1)]['code'] === T_COLON) { | ||
$fix = $phpcsFile->addFixableError($error, $returnType, 'SpaceBeforeReturnType'); | ||
if ($fix === true) { | ||
$phpcsFile->fixer->addContentBefore($returnType, ' '); | ||
} | ||
} 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); | ||
if ($tokens[$prev]['code'] === T_CLOSE_PARENTHESIS) { | ||
$fix = $phpcsFile->addFixableError($error, $colon, 'SpaceBeforeColon'); | ||
if ($fix === true) { | ||
$phpcsFile->fixer->beginChangeset(); | ||
for ($x = ($prev + 1); $x < $colon; $x++) { | ||
$phpcsFile->fixer->replaceToken($x, ''); | ||
} | ||
|
||
$phpcsFile->fixer->endChangeset(); | ||
} | ||
} else { | ||
$phpcsFile->addError($error, $colon, 'SpaceBeforeColon'); | ||
} | ||
} | ||
|
||
}//end process() | ||
|
||
|
||
}//end class |
56 changes: 56 additions & 0 deletions
56
src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
public function functionName(int $arg1, $arg2): string | ||
{ | ||
return 'foo'; | ||
} | ||
|
||
public function anotherFunction( | ||
int $baz | ||
): string { | ||
return 'foo'; | ||
} | ||
|
||
$longArgs_noVars = function ( | ||
$longArgument, | ||
): string { | ||
// body | ||
}; | ||
|
||
$shortArgs_longVars = function ($arg) use ( | ||
$longVar1, | ||
): string { | ||
// body | ||
}; | ||
|
||
public function functionName(int $arg1, $arg2) | ||
: | ||
string | ||
{ | ||
return 'foo'; | ||
} | ||
|
||
public function anotherFunction( | ||
int $baz | ||
) : string { | ||
return 'foo'; | ||
} | ||
|
||
$longArgs_noVars = function ( | ||
$longArgument, | ||
) :string { | ||
// body | ||
}; | ||
|
||
$shortArgs_longVars = function ($arg) use ( | ||
$longVar1, | ||
) | ||
:string { | ||
// body | ||
}; | ||
|
||
public function functionName(int $arg1, $arg2) /* can't fix */ : string {} | ||
|
||
public function functionName(int $arg1, $arg2) /* can't fix */ | ||
: // phpcs:ignore Standard.Category.Sniff | ||
string {} |
53 changes: 53 additions & 0 deletions
53
src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.inc.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
public function functionName(int $arg1, $arg2): string | ||
{ | ||
return 'foo'; | ||
} | ||
|
||
public function anotherFunction( | ||
int $baz | ||
): string { | ||
return 'foo'; | ||
} | ||
|
||
$longArgs_noVars = function ( | ||
$longArgument, | ||
): string { | ||
// body | ||
}; | ||
|
||
$shortArgs_longVars = function ($arg) use ( | ||
$longVar1, | ||
): string { | ||
// body | ||
}; | ||
|
||
public function functionName(int $arg1, $arg2): string | ||
{ | ||
return 'foo'; | ||
} | ||
|
||
public function anotherFunction( | ||
int $baz | ||
): string { | ||
return 'foo'; | ||
} | ||
|
||
$longArgs_noVars = function ( | ||
$longArgument, | ||
): string { | ||
// body | ||
}; | ||
|
||
$shortArgs_longVars = function ($arg) use ( | ||
$longVar1, | ||
): string { | ||
// body | ||
}; | ||
|
||
public function functionName(int $arg1, $arg2) /* can't fix */ : string {} | ||
|
||
public function functionName(int $arg1, $arg2) /* can't fix */ | ||
: // phpcs:ignore Standard.Category.Sniff | ||
string {} |
57 changes: 57 additions & 0 deletions
57
src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Unit test class for the ReturnTypeDeclaration sniff. | ||
* | ||
* @author Greg Sherwood <gsherwood@squiz.net> | ||
* @copyright 2006-2018 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Standards\PSR12\Tests\Functions; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
class ReturnTypeDeclarationUnitTest extends AbstractSniffUnitTest | ||
{ | ||
|
||
|
||
/** | ||
* Returns the lines where errors should occur. | ||
* | ||
* 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. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
protected function getErrorList() | ||
{ | ||
return [ | ||
27 => 1, | ||
28 => 1, | ||
35 => 2, | ||
41 => 2, | ||
48 => 2, | ||
52 => 1, | ||
55 => 1, | ||
56 => 1, | ||
]; | ||
|
||
}//end getErrorList() | ||
|
||
|
||
/** | ||
* Returns the lines where warnings should occur. | ||
* | ||
* 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. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
protected function getWarningList() | ||
{ | ||
return []; | ||
|
||
}//end getWarningList() | ||
|
||
|
||
}//end class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters