-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new Generic.PHP.LowerCaseType sniff to ensure PHP types used fo…
…r type hints, return types, and type casting are lowercase
- Loading branch information
Showing
6 changed files
with
311 additions
and
0 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
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,38 @@ | ||
<documentation title="Lowercase PHP Types"> | ||
<standard> | ||
<![CDATA[ | ||
All PHP types used for type hints and return types should be lowercase. | ||
]]> | ||
</standard> | ||
<code_comparison> | ||
<code title="Valid: Lowercase types used."> | ||
<![CDATA[ | ||
function myFunction(int $foo) : string { | ||
} | ||
]]> | ||
</code> | ||
<code title="Invalid: Non-lowercase types used."> | ||
<![CDATA[ | ||
function myFunction(<em>Int</em> $foo) : <em>STRING</em> { | ||
} | ||
]]> | ||
</code> | ||
</code_comparison> | ||
<standard> | ||
<![CDATA[ | ||
All PHP types used for type casting should be lowercase. | ||
]]> | ||
</standard> | ||
<code_comparison> | ||
<code title="Valid: Lowercase type used."> | ||
<![CDATA[ | ||
$foo = (bool) $isValid; | ||
]]> | ||
</code> | ||
<code title="Invalid: Non-lowercase type used."> | ||
<![CDATA[ | ||
$foo = <em>(BOOL)</em> $isValid; | ||
]]> | ||
</code> | ||
</code_comparison> | ||
</documentation> |
148 changes: 148 additions & 0 deletions
148
src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.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,148 @@ | ||
<?php | ||
/** | ||
* Checks that all PHP types are lowercase. | ||
* | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006-2015 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\Generic\Sniffs\PHP; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Util\Tokens; | ||
|
||
class LowerCaseTypeSniff implements Sniff | ||
{ | ||
|
||
|
||
/** | ||
* Returns an array of tokens this test wants to listen for. | ||
* | ||
* @return array | ||
*/ | ||
public function register() | ||
{ | ||
$tokens = Tokens::$castTokens; | ||
$tokens[] = T_FUNCTION; | ||
$tokens[] = T_CLOSURE; | ||
return $tokens; | ||
|
||
}//end register() | ||
|
||
|
||
/** | ||
* Processes this sniff, 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::$castTokens[$tokens[$stackPtr]['code']]) === true) { | ||
// A cast token. | ||
if (strtolower($tokens[$stackPtr]['content']) !== $tokens[$stackPtr]['content']) { | ||
if ($tokens[$stackPtr]['content'] === strtoupper($tokens[$stackPtr]['content'])) { | ||
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'upper'); | ||
} else { | ||
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'mixed'); | ||
} | ||
|
||
$error = 'PHP types must be lowercase; expected "%s" but found "%s"'; | ||
$data = [ | ||
strtolower($tokens[$stackPtr]['content']), | ||
$tokens[$stackPtr]['content'], | ||
]; | ||
|
||
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found', $data); | ||
if ($fix === true) { | ||
$phpcsFile->fixer->replaceToken($stackPtr, strtolower($tokens[$stackPtr]['content'])); | ||
} | ||
} else { | ||
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'lower'); | ||
}//end if | ||
|
||
return; | ||
} | ||
|
||
$phpTypes = [ | ||
'self' => true, | ||
'array' => true, | ||
'callable' => true, | ||
'bool' => true, | ||
'float' => true, | ||
'int' => true, | ||
'string' => true, | ||
'iterable' => true, | ||
]; | ||
|
||
$props = $phpcsFile->getMethodProperties($stackPtr); | ||
$returnType = $props['return_type']; | ||
if ($returnType !== '' | ||
&& isset($phpTypes[strtolower($returnType)]) === true | ||
) { | ||
// A function return type. | ||
if (strtolower($returnType) !== $returnType) { | ||
if ($returnType === strtoupper($returnType)) { | ||
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'upper'); | ||
} else { | ||
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'mixed'); | ||
} | ||
|
||
$error = 'PHP types must be lowercase; expected "%s" but found "%s"'; | ||
$data = [ | ||
strtolower($returnType), | ||
$returnType, | ||
]; | ||
|
||
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found', $data); | ||
if ($fix === true) { | ||
$token = $props['return_type_token']; | ||
$phpcsFile->fixer->replaceToken($token, strtolower($tokens[$token]['content'])); | ||
} | ||
} else { | ||
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'lower'); | ||
}//end if | ||
} | ||
|
||
$params = $phpcsFile->getMethodParameters($stackPtr); | ||
foreach ($params as $param) { | ||
$typeHint = $param['type_hint']; | ||
if ($typeHint !== '' | ||
&& isset($phpTypes[strtolower($typeHint)]) === true | ||
) { | ||
// A function return type. | ||
if (strtolower($typeHint) !== $typeHint) { | ||
if ($typeHint === strtoupper($typeHint)) { | ||
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'upper'); | ||
} else { | ||
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'mixed'); | ||
} | ||
|
||
$error = 'PHP types must be lowercase; expected "%s" but found "%s"'; | ||
$data = [ | ||
strtolower($typeHint), | ||
$typeHint, | ||
]; | ||
|
||
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found', $data); | ||
if ($fix === true) { | ||
$token = $param['type_hint_token']; | ||
$phpcsFile->fixer->replaceToken($token, strtolower($tokens[$token]['content'])); | ||
} | ||
} else { | ||
$phpcsFile->recordMetric($stackPtr, 'PHP type case', 'lower'); | ||
}//end if | ||
} | ||
} | ||
|
||
}//end process() | ||
|
||
|
||
}//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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
$foo = (int) $bar; | ||
$foo = (integer) $bar; | ||
$foo = (bool) $bar; | ||
$foo = (boolean) $bar; | ||
$foo = (float) $bar; | ||
$foo = (double) $bar; | ||
$foo = (real) $bar; | ||
$foo = (string) $bar; | ||
$foo = (array) $bar; | ||
$foo = (object) $bar; | ||
$foo = (unset) $bar; | ||
|
||
$foo = (Int) $bar; | ||
$foo = (INTEGER) $bar; | ||
$foo = (BOOL) $bar; | ||
$foo = (String) $bar; | ||
$foo = (Array) $bar; | ||
|
||
function foo(int $a, string $b, bool $c, array $d, Foo\Bar $e) : int {} | ||
function foo(Int $a, String $b, BOOL $c, Array $d, Foo\Bar $e) : Foo\Bar {} | ||
function foo(Int $a, Bar $b, BOOL $c, Array $d, Foo\Bar $e) : Bar {} | ||
function foo(callable $a, Callable $b, self $c, Iterable $d, iterable $e) : Float {} | ||
|
||
$foo = function (int $a, Bool $b) {}; | ||
$foo = function (int $a, Callable $b) :INT{}; | ||
$foo = function (BOOL $a, float $b) use ($foo) : INT {}; | ||
$foo = function (Foo $a, Foo\Bar $b) use ($foo) : \Foo\Bar {}; | ||
$foo = function (bool $a, callable $b) use ($foo) : Bar {}; |
29 changes: 29 additions & 0 deletions
29
src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.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,29 @@ | ||
<?php | ||
$foo = (int) $bar; | ||
$foo = (integer) $bar; | ||
$foo = (bool) $bar; | ||
$foo = (boolean) $bar; | ||
$foo = (float) $bar; | ||
$foo = (double) $bar; | ||
$foo = (real) $bar; | ||
$foo = (string) $bar; | ||
$foo = (array) $bar; | ||
$foo = (object) $bar; | ||
$foo = (unset) $bar; | ||
|
||
$foo = (int) $bar; | ||
$foo = (integer) $bar; | ||
$foo = (bool) $bar; | ||
$foo = (string) $bar; | ||
$foo = (array) $bar; | ||
|
||
function foo(int $a, string $b, bool $c, array $d, Foo\Bar $e) : int {} | ||
function foo(int $a, string $b, bool $c, array $d, Foo\Bar $e) : Foo\Bar {} | ||
function foo(int $a, Bar $b, bool $c, array $d, Foo\Bar $e) : Bar {} | ||
function foo(callable $a, callable $b, self $c, iterable $d, iterable $e) : float {} | ||
|
||
$foo = function (int $a, bool $b) {}; | ||
$foo = function (int $a, callable $b) :int{}; | ||
$foo = function (bool $a, float $b) use ($foo) : int {}; | ||
$foo = function (Foo $a, Foo\Bar $b) use ($foo) : \Foo\Bar {}; | ||
$foo = function (bool $a, callable $b) use ($foo) : Bar {}; |
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,60 @@ | ||
<?php | ||
/** | ||
* Unit test class for the LowerCaseType sniff. | ||
* | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006-2015 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\Generic\Tests\PHP; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
class LowerCaseTypeUnitTest 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> | ||
*/ | ||
public function getErrorList() | ||
{ | ||
return [ | ||
14 => 1, | ||
15 => 1, | ||
16 => 1, | ||
17 => 1, | ||
18 => 1, | ||
21 => 4, | ||
22 => 3, | ||
23 => 3, | ||
25 => 1, | ||
26 => 2, | ||
27 => 2, | ||
]; | ||
|
||
}//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> | ||
*/ | ||
public function getWarningList() | ||
{ | ||
return []; | ||
|
||
}//end getWarningList() | ||
|
||
|
||
}//end class |