From ad45d8ab125fdd56dd61e466584a9b2db6477592 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Tue, 29 Oct 2024 11:20:56 -0300 Subject: [PATCH] Generic/ConstructorName: fix minor bug This commit fixes a minor bug in `Generic.NamingConventions .ConstructorName` when checking if a given class has a parent. The code was checking if the lower case version of the value returned by `File::findExtendedClassName()`` is `false`. The problem is that `strtolower()` never returns `false`, it always returns a `string`. Thus, the condition would never evaluate to `true` and the sniff would not bail at this point when a given class has no parent. This did not cause any issues to the sniff, even for invalid code, as there is not a scenario where a class method can have a `T_DOUBLE_COLON` token followed by a `T_STRING` token with an empty `content`. This is true even for empty strings as PHPCS includes the quotes in the `content`. --- .../Sniffs/NamingConventions/ConstructorNameSniff.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php index 75fbd2264e..adb1d6ba77 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php @@ -94,16 +94,18 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop return; } - $parentClassName = strtolower($phpcsFile->findExtendedClassName($currScope)); + $parentClassName = $phpcsFile->findExtendedClassName($currScope); if ($parentClassName === false) { return; } + $parentClassNameLc = strtolower($parentClassName); + $endFunctionIndex = $tokens[$stackPtr]['scope_closer']; $startIndex = $stackPtr; while (($doubleColonIndex = $phpcsFile->findNext(T_DOUBLE_COLON, $startIndex, $endFunctionIndex)) !== false) { if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING - && strtolower($tokens[($doubleColonIndex + 1)]['content']) === $parentClassName + && strtolower($tokens[($doubleColonIndex + 1)]['content']) === $parentClassNameLc ) { $error = 'PHP4 style calls to parent constructors are not allowed; use "parent::__construct()" instead'; $phpcsFile->addError($error, ($doubleColonIndex + 1), 'OldStyleCall');