Skip to content

Commit ad45d8a

Browse files
committed
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`.
1 parent 8a7ee56 commit ad45d8a

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,18 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
9494
return;
9595
}
9696

97-
$parentClassName = strtolower($phpcsFile->findExtendedClassName($currScope));
97+
$parentClassName = $phpcsFile->findExtendedClassName($currScope);
9898
if ($parentClassName === false) {
9999
return;
100100
}
101101

102+
$parentClassNameLc = strtolower($parentClassName);
103+
102104
$endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
103105
$startIndex = $stackPtr;
104106
while (($doubleColonIndex = $phpcsFile->findNext(T_DOUBLE_COLON, $startIndex, $endFunctionIndex)) !== false) {
105107
if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING
106-
&& strtolower($tokens[($doubleColonIndex + 1)]['content']) === $parentClassName
108+
&& strtolower($tokens[($doubleColonIndex + 1)]['content']) === $parentClassNameLc
107109
) {
108110
$error = 'PHP4 style calls to parent constructors are not allowed; use "parent::__construct()" instead';
109111
$phpcsFile->addError($error, ($doubleColonIndex + 1), 'OldStyleCall');

0 commit comments

Comments
 (0)