Skip to content

Commit a22a1cb

Browse files
authored
Merge pull request #652 from PHPCSStandards/feature/generic-constructorname-bug-fix-x2
Generic/ConstructorName: bug fix x 2
2 parents 01e247b + 7b98484 commit a22a1cb

File tree

3 files changed

+66
-13
lines changed

3 files changed

+66
-13
lines changed

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

Lines changed: 21 additions & 8 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
{
@@ -90,7 +91,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
9091
}
9192

9293
// Stop if the constructor doesn't have a body, like when it is abstract.
93-
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
94+
if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) {
9495
return;
9596
}
9697

@@ -102,17 +103,29 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
102103
$parentClassNameLc = strtolower($parentClassName);
103104

104105
$endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
105-
$startIndex = $stackPtr;
106-
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
106+
$startIndex = $tokens[$stackPtr]['scope_opener'];
107+
while (($doubleColonIndex = $phpcsFile->findNext(T_DOUBLE_COLON, ($startIndex + 1), $endFunctionIndex)) !== false) {
108+
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($doubleColonIndex + 1), null, true);
109+
if ($tokens[$nextNonEmpty]['code'] !== T_STRING
110+
|| strtolower($tokens[$nextNonEmpty]['content']) !== $parentClassNameLc
111+
) {
112+
$startIndex = $nextNonEmpty;
113+
continue;
114+
}
115+
116+
$prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($doubleColonIndex - 1), null, true);
117+
if ($tokens[$prevNonEmpty]['code'] === T_PARENT
118+
|| $tokens[$prevNonEmpty]['code'] === T_SELF
119+
|| $tokens[$prevNonEmpty]['code'] === T_STATIC
120+
|| ($tokens[$prevNonEmpty]['code'] === T_STRING
121+
&& strtolower($tokens[$prevNonEmpty]['content']) === $parentClassNameLc)
109122
) {
110123
$error = 'PHP4 style calls to parent constructors are not allowed; use "parent::__construct()" instead';
111-
$phpcsFile->addError($error, ($doubleColonIndex + 1), 'OldStyleCall');
124+
$phpcsFile->addError($error, $nextNonEmpty, 'OldStyleCall');
112125
}
113126

114-
$startIndex = ($doubleColonIndex + 1);
115-
}
127+
$startIndex = $nextNonEmpty;
128+
}//end while
116129

117130
}//end processTokenWithinScope()
118131

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,36 @@ 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+
MyParent/*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+
}
115+
116+
// Bug: calling a method with the same name as the class being extended on another class, should not be flagged.
117+
class HierarchyKeywordBeforeColon extends MyParent
118+
{
119+
public function HierarchyKeywordBeforeColon() {
120+
parent::MyParent();
121+
MyParent::MyParent();
122+
SomeOtherClass::MyParent();
123+
}
124+
125+
public function __construct() {
126+
self::MyParent();
127+
static::MyParent();
128+
SomeOtherClass::MyParent();
129+
}
130+
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@ 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,
42+
120 => 1,
43+
121 => 1,
44+
126 => 1,
45+
127 => 1,
3946
];
4047

4148
}//end getErrorList()

0 commit comments

Comments
 (0)