Skip to content

Commit 24c5688

Browse files
committed
Squiz/LowercasePHPFunctions: fix sniff to work with the PHP 8 identifier tokens
The existing unit tests already contain tests covering this change. Includes updating two tests with code which is considered a parse error in PHP 8.0. Handling that type of code is no longer supported by this sniff.
1 parent f80aeb3 commit 24c5688

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

src/Standards/Squiz/Sniffs/PHP/LowercasePHPFunctionsSniff.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public function __construct()
4343
*/
4444
public function register()
4545
{
46-
return [T_STRING];
46+
return [
47+
T_STRING,
48+
T_NAME_FULLY_QUALIFIED,
49+
];
4750

4851
}//end register()
4952

@@ -61,7 +64,11 @@ public function process(File $phpcsFile, $stackPtr)
6164
{
6265
$tokens = $phpcsFile->getTokens();
6366

64-
$content = $tokens[$stackPtr]['content'];
67+
$content = $tokens[$stackPtr]['content'];
68+
if ($tokens[$stackPtr]['code'] === T_NAME_FULLY_QUALIFIED) {
69+
$content = ltrim($content, '\\');
70+
}
71+
6572
$contentLc = strtolower($content);
6673
if ($content === $contentLc) {
6774
return;
@@ -93,7 +100,7 @@ public function process(File $phpcsFile, $stackPtr)
93100

94101
if ($tokens[$next]['code'] !== T_OPEN_PARENTHESIS) {
95102
// Is this a use statement importing a PHP native function ?
96-
if ($tokens[$next]['code'] !== T_NS_SEPARATOR
103+
if ($tokens[$stackPtr]['code'] === T_STRING
97104
&& $tokens[$prev]['code'] === T_STRING
98105
&& $tokens[$prev]['content'] === 'function'
99106
&& $prevPrev !== false
@@ -120,19 +127,6 @@ public function process(File $phpcsFile, $stackPtr)
120127
return;
121128
}
122129

123-
if ($tokens[$prev]['code'] === T_NS_SEPARATOR) {
124-
if ($prevPrev !== false
125-
&& ($tokens[$prevPrev]['code'] === T_STRING
126-
|| $tokens[$prevPrev]['code'] === T_NAMESPACE
127-
|| $tokens[$prevPrev]['code'] === T_NEW)
128-
) {
129-
// Namespaced class/function, not an inbuilt function.
130-
// Could potentially give false negatives for non-namespaced files
131-
// when namespace\functionName() is encountered.
132-
return;
133-
}
134-
}
135-
136130
if ($tokens[$prev]['code'] === T_NEW) {
137131
// Object creation, not an inbuilt function.
138132
return;
@@ -158,7 +152,7 @@ public function process(File $phpcsFile, $stackPtr)
158152

159153
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'CallUppercase', $data);
160154
if ($fix === true) {
161-
$phpcsFile->fixer->replaceToken($stackPtr, $contentLc);
155+
$phpcsFile->fixer->replaceToken($stackPtr, strtolower($tokens[$stackPtr]['content']));
162156
}
163157

164158
}//end process()

src/Standards/Squiz/Tests/PHP/LowercasePHPFunctionsUnitTest.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class ArrayUnique {}
3333
$sillyComments = strToLower /*comment*/ ($string);
3434

3535
$callToGlobalFunction = \STR_REPEAT($a, 2);
36-
$callToGlobalFunction = \ /*comment*/ str_Repeat($a, 2);
36+
$callToGlobalFunction = \str_Repeat($a, 2);
3737

38-
$callToNamespacedFunction = MyNamespace /* phpcs:ignore Standard */ \STR_REPEAT($a, 2);
38+
$callToNamespacedFunction = MyNamespace\STR_REPEAT($a, 2);
3939
$callToNamespacedFunction = namespace\STR_REPEAT($a, 2); // Could potentially be false negative.
4040

4141
$filePath = new \File($path);

src/Standards/Squiz/Tests/PHP/LowercasePHPFunctionsUnitTest.inc.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class ArrayUnique {}
3333
$sillyComments = strtolower /*comment*/ ($string);
3434

3535
$callToGlobalFunction = \str_repeat($a, 2);
36-
$callToGlobalFunction = \ /*comment*/ str_repeat($a, 2);
36+
$callToGlobalFunction = \str_repeat($a, 2);
3737

38-
$callToNamespacedFunction = MyNamespace /* phpcs:ignore Standard */ \STR_REPEAT($a, 2);
38+
$callToNamespacedFunction = MyNamespace\STR_REPEAT($a, 2);
3939
$callToNamespacedFunction = namespace\STR_REPEAT($a, 2); // Could potentially be false negative.
4040

4141
$filePath = new \File($path);

0 commit comments

Comments
 (0)