Skip to content

Commit 8cbf4e8

Browse files
committed
PSR12/ShortFormTypeKeywords: bug fix / false positive
The PSR 12 standard (currently) says nothing about whether or not spaces within type casts are allowed. As it was, the `PSR12.Keywords.ShortFormTypeKeywords` sniff would throw a false positive for a short form type cast like `( bool )` and "fix" it by removing the spaces, which is not the concern of this sniff. If at some point the PSR12 standard would take a stand about spaces within type casts, the `Squiz.WhiteSpace.CastSpacing` sniff could be added to check & fix these issues. In the mean time, the `ShortFormTypeKeywords` sniff should be space-agnostic. This PR takes care of that. It also makes the error message slightly more descriptive by being explicit about the type cast found in the error message. Includes unit tests.
1 parent 390bffe commit 8cbf4e8

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

src/Standards/PSR12/Sniffs/Keywords/ShortFormTypeKeywordsSniff.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,31 @@ public function register()
4242
*/
4343
public function process(File $phpcsFile, $stackPtr)
4444
{
45-
$tokens = $phpcsFile->getTokens();
45+
$tokens = $phpcsFile->getTokens();
46+
$typecast = str_replace(' ', '', $tokens[$stackPtr]['content']);
47+
$typecast = str_replace("\t", '', $typecast);
48+
$typecast = trim($typecast, '()');
49+
$typecastLc = strtolower($typecast);
4650

4751
if (($tokens[$stackPtr]['code'] === T_BOOL_CAST
48-
&& strtolower($tokens[$stackPtr]['content']) === '(bool)')
52+
&& $typecastLc === 'bool')
4953
|| ($tokens[$stackPtr]['code'] === T_INT_CAST
50-
&& strtolower($tokens[$stackPtr]['content']) === '(int)')
54+
&& $typecastLc === 'int')
5155
) {
5256
return;
5357
}
5458

55-
$error = 'Short form type keywords must be used';
56-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'LongFound');
59+
$error = 'Short form type keywords must be used. Found: %s';
60+
$data = [$tokens[$stackPtr]['content']];
61+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'LongFound', $data);
5762
if ($fix === true) {
5863
if ($tokens[$stackPtr]['code'] === T_BOOL_CAST) {
59-
$phpcsFile->fixer->replaceToken($stackPtr, '(bool)');
64+
$replacement = str_replace($typecast, 'bool', $tokens[$stackPtr]['content']);
6065
} else {
61-
$phpcsFile->fixer->replaceToken($stackPtr, '(int)');
66+
$replacement = str_replace($typecast, 'int', $tokens[$stackPtr]['content']);
6267
}
68+
69+
$phpcsFile->fixer->replaceToken($stackPtr, $replacement);
6370
}
6471

6572
}//end process()

src/Standards/PSR12/Tests/Keywords/ShortFormTypeKeywordsUnitTest.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ $bar = (BOOLEAN) $foo;
66
$bar = (int) $foo;
77
$bar = (integer) $foo;
88
$bar = (INT) $foo;
9+
10+
// Test recognition with whitespace within the cast.
11+
$bar = ( bool ) $foo;
12+
$bar = ( int ) $foo;
13+
$bar = ( boolean ) $foo;
14+
$bar = ( integer) $foo;

src/Standards/PSR12/Tests/Keywords/ShortFormTypeKeywordsUnitTest.inc.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ $bar = (bool) $foo;
66
$bar = (int) $foo;
77
$bar = (int) $foo;
88
$bar = (INT) $foo;
9+
10+
// Test recognition with whitespace within the cast.
11+
$bar = ( bool ) $foo;
12+
$bar = ( int ) $foo;
13+
$bar = ( bool ) $foo;
14+
$bar = ( int) $foo;

src/Standards/PSR12/Tests/Keywords/ShortFormTypeKeywordsUnitTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ class ShortFormTypeKeywordsUnitTest extends AbstractSniffUnitTest
2626
public function getErrorList()
2727
{
2828
return [
29-
3 => 1,
30-
5 => 1,
31-
7 => 1,
29+
3 => 1,
30+
5 => 1,
31+
7 => 1,
32+
13 => 1,
33+
14 => 1,
3234
];
3335

3436
}//end getErrorList()

0 commit comments

Comments
 (0)