Skip to content

Commit 0714850

Browse files
committed
Generic/ConstructorName: bug fix - allow for unconventional spacing and comments
When determining whether or not a "parent" constructor was being called using a call to a PHP4-style constructor, the sniff would only look at the _next_ token after a double colon, while the next token may be whitespace or a comment, which should be ignored. Fixed now by making the sniff check for the _next non empty_ token instead. Includes tests.
1 parent 960262c commit 0714850

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use PHP_CodeSniffer\Files\File;
1717
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
18+
use PHP_CodeSniffer\Util\Tokens;
1819

1920
class ConstructorNameSniff extends AbstractScopeSniff
2021
{
@@ -104,14 +105,15 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
104105
$endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
105106
$startIndex = $stackPtr;
106107
while (($doubleColonIndex = $phpcsFile->findNext(T_DOUBLE_COLON, $startIndex, $endFunctionIndex)) !== false) {
107-
if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING
108-
&& strtolower($tokens[($doubleColonIndex + 1)]['content']) === $parentClassNameLc
108+
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($doubleColonIndex + 1), null, true);
109+
if ($tokens[$nextNonEmpty]['code'] === T_STRING
110+
&& strtolower($tokens[$nextNonEmpty]['content']) === $parentClassNameLc
109111
) {
110112
$error = 'PHP4 style calls to parent constructors are not allowed; use "parent::__construct()" instead';
111-
$phpcsFile->addError($error, ($doubleColonIndex + 1), 'OldStyleCall');
113+
$phpcsFile->addError($error, $nextNonEmpty, 'OldStyleCall');
112114
}
113115

114-
$startIndex = ($doubleColonIndex + 1);
116+
$startIndex = $nextNonEmpty;
115117
}
116118

117119
}//end processTokenWithinScope()

src/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.inc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,20 @@ interface CustomChildCreator
9595
{
9696
public function customChildCreator($elementName, Project $project);
9797
}
98+
99+
// Bug: unconventional spacing and unexpected comments were not handled correctly.
100+
class UnconventionalSpacing extends MyParent
101+
{
102+
public function UnconventionalSpacing() {
103+
self :: MyParent();
104+
static/*comment*/::/*comment*/MyParent();
105+
}
106+
107+
public function __construct() {
108+
parent
109+
//comment
110+
::
111+
// phpcs:ignore Stnd.Cat.SniffName -- for reasons.
112+
MyParent();
113+
}
114+
}

src/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ final class ConstructorNameUnitTest extends AbstractSniffUnitTest
3131
public function getErrorList()
3232
{
3333
return [
34-
6 => 1,
35-
11 => 1,
36-
47 => 1,
37-
62 => 1,
38-
91 => 1,
34+
6 => 1,
35+
11 => 1,
36+
47 => 1,
37+
62 => 1,
38+
91 => 1,
39+
103 => 1,
40+
104 => 1,
41+
112 => 1,
3942
];
4043

4144
}//end getErrorList()

0 commit comments

Comments
 (0)