Skip to content

Commit

Permalink
Added new Generic.PHP.LowerCaseType sniff to ensure PHP types used fo…
Browse files Browse the repository at this point in the history
…r type hints, return types, and type casting are lowercase
  • Loading branch information
gsherwood committed Apr 19, 2018
1 parent 48eaf90 commit 5a98939
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 0 deletions.
7 changes: 7 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Functionality of the Squiz standard remains the same, but the error codes are now different
-- Previously, Squiz.PHP.ForbiddenFunctions.Found and Squiz.PHP.ForbiddenFunctions.FoundWithAlternative
-- Now, Generic.PHP.ForbiddenFunctions.Found and Generic.PHP.ForbiddenFunctions.FoundWithAlternative
- Added new Generic.PHP.LowerCaseType sniff
-- Ensures PHP types used for type hints, return types, and type casting are lowercase
- Added new Generic.WhiteSpace.ArbitraryParenthesesSpacing sniff
-- Generates an error for whitespace inside parenthesis that don't belong to a function call/declaration or control structure
-- Generates a warning for any empty parenthesis found
Expand Down Expand Up @@ -391,6 +393,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="ForbiddenFunctionsStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="LowerCaseConstantStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="LowerCaseKeywordStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="LowerCaseTypeStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="NoSilencedErrorsStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="SAPIUsageStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="UpperCaseConstantStandard.xml" role="php" />
Expand Down Expand Up @@ -489,6 +492,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="ForbiddenFunctionsSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="LowerCaseConstantSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="LowerCaseKeywordSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="LowerCaseTypeSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="NoSilencedErrorsSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="SAPIUsageSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="SyntaxSniff.php" role="php" />
Expand Down Expand Up @@ -705,6 +709,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="LowerCaseKeywordUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="LowerCaseKeywordUnitTest.inc.fixed" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="LowerCaseKeywordUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="LowerCaseTypeUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="LowerCaseTypeUnitTest.inc.fixed" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="LowerCaseTypeUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="NoSilencedErrorsUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="NoSilencedErrorsUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="SAPIUsageUnitTest.inc" role="test" />
Expand Down
38 changes: 38 additions & 0 deletions src/Standards/Generic/Docs/PHP/LowerCaseTypeStandard.xml
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 src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php
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
29 changes: 29 additions & 0 deletions src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc
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 src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc.fixed
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 {};
60 changes: 60 additions & 0 deletions src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php
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

0 comments on commit 5a98939

Please sign in to comment.