Skip to content

Commit ce20d6a

Browse files
authored
Merge pull request PHPCSStandards#814 from PHPCSStandards/feature/generic-constructorname-more-defensive-coding
Generic/ConstructorName: more defensive coding / prevent fatal error
2 parents f947742 + 3a76062 commit ce20d6a

File tree

4 files changed

+44
-17
lines changed

4 files changed

+44
-17
lines changed

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,13 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
7878
$this->currentClass = $className;
7979
}
8080

81-
$methodName = strtolower($phpcsFile->getDeclarationName($stackPtr));
81+
$methodName = $phpcsFile->getDeclarationName($stackPtr);
82+
if ($methodName === null) {
83+
// Live coding or parse error. Bow out.
84+
return;
85+
}
8286

87+
$methodName = strtolower($methodName);
8388
if ($methodName === $className) {
8489
if (in_array('__construct', $this->functionList, true) === false) {
8590
$error = 'PHP4 style constructors are not allowed; use "__construct()" instead';
@@ -164,7 +169,13 @@ protected function loadFunctionNamesInScope(File $phpcsFile, $currScope)
164169
continue;
165170
}
166171

167-
$this->functionList[] = trim(strtolower($phpcsFile->getDeclarationName($i)));
172+
$methodName = $phpcsFile->getDeclarationName($i);
173+
if ($methodName === null) {
174+
// Live coding or parse error. Ignore.
175+
continue;
176+
}
177+
178+
$this->functionList[] = trim(strtolower($methodName));
168179

169180
if (isset($tokens[$i]['scope_closer']) !== false) {
170181
// Skip past nested functions and such.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
// Intentional parse error (missing method name).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered.
6+
7+
class My_Class {
8+
public function {}
9+
}

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

+22-15
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,31 @@ final class ConstructorNameUnitTest extends AbstractSniffUnitTest
2626
* The key of the array should represent the line number and the value
2727
* should represent the number of errors that should occur on that line.
2828
*
29+
* @param string $testFile The name of the test file to process.
30+
*
2931
* @return array<int, int>
3032
*/
31-
public function getErrorList()
33+
public function getErrorList($testFile='')
3234
{
33-
return [
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,
46-
];
35+
switch ($testFile) {
36+
case 'ConstructorNameUnitTest.1.inc':
37+
return [
38+
6 => 1,
39+
11 => 1,
40+
47 => 1,
41+
62 => 1,
42+
91 => 1,
43+
103 => 1,
44+
104 => 1,
45+
112 => 1,
46+
120 => 1,
47+
121 => 1,
48+
126 => 1,
49+
127 => 1,
50+
];
51+
default:
52+
return [];
53+
}//end switch
4754

4855
}//end getErrorList()
4956

0 commit comments

Comments
 (0)