From 90e3190abd12d6addad544100093a4e37fb0efc7 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 12 Feb 2025 17:54:03 +0100 Subject: [PATCH 1/2] Generic/ConstructorName: rename test case file ... to allow for adding additional test case files for parse error tests. --- ...Test.inc => ConstructorNameUnitTest.1.inc} | 0 .../ConstructorNameUnitTest.php | 37 +++++++++++-------- 2 files changed, 22 insertions(+), 15 deletions(-) rename src/Standards/Generic/Tests/NamingConventions/{ConstructorNameUnitTest.inc => ConstructorNameUnitTest.1.inc} (100%) diff --git a/src/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.inc b/src/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.1.inc similarity index 100% rename from src/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.inc rename to src/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.1.inc diff --git a/src/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.php b/src/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.php index 9df7f37930..10589c5b90 100644 --- a/src/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.php +++ b/src/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.php @@ -26,24 +26,31 @@ final class ConstructorNameUnitTest extends AbstractSniffUnitTest * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * + * @param string $testFile The name of the test file to process. + * * @return array */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 6 => 1, - 11 => 1, - 47 => 1, - 62 => 1, - 91 => 1, - 103 => 1, - 104 => 1, - 112 => 1, - 120 => 1, - 121 => 1, - 126 => 1, - 127 => 1, - ]; + switch ($testFile) { + case 'ConstructorNameUnitTest.1.inc': + return [ + 6 => 1, + 11 => 1, + 47 => 1, + 62 => 1, + 91 => 1, + 103 => 1, + 104 => 1, + 112 => 1, + 120 => 1, + 121 => 1, + 126 => 1, + 127 => 1, + ]; + default: + return []; + }//end switch }//end getErrorList() From 3a76062b59f7831ece7d56126b7ffeaa3e6f1b5b Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 12 Feb 2025 18:06:42 +0100 Subject: [PATCH 2/2] Generic/ConstructorName: more defensive coding This sniff could throw the following fatal errors when it encountered parse errors/during live coding: ``` Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in path/to/PHPCS/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php on line 83 in path/to/PHPCS/src/Runner.php on line 624 Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in path/to/PHPCS/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php on line 167 in path/to/PHPCS/src/Runner.php on line 624 ``` Fixed now by adding more defensive coding. Includes test safeguarding the fix. --- .../NamingConventions/ConstructorNameSniff.php | 15 +++++++++++++-- .../ConstructorNameUnitTest.2.inc | 9 +++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 src/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.2.inc diff --git a/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php index 48e7659e3c..e87f066b3f 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php @@ -78,8 +78,13 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop $this->currentClass = $className; } - $methodName = strtolower($phpcsFile->getDeclarationName($stackPtr)); + $methodName = $phpcsFile->getDeclarationName($stackPtr); + if ($methodName === null) { + // Live coding or parse error. Bow out. + return; + } + $methodName = strtolower($methodName); if ($methodName === $className) { if (in_array('__construct', $this->functionList, true) === false) { $error = 'PHP4 style constructors are not allowed; use "__construct()" instead'; @@ -164,7 +169,13 @@ protected function loadFunctionNamesInScope(File $phpcsFile, $currScope) continue; } - $this->functionList[] = trim(strtolower($phpcsFile->getDeclarationName($i))); + $methodName = $phpcsFile->getDeclarationName($i); + if ($methodName === null) { + // Live coding or parse error. Ignore. + continue; + } + + $this->functionList[] = trim(strtolower($methodName)); if (isset($tokens[$i]['scope_closer']) !== false) { // Skip past nested functions and such. diff --git a/src/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.2.inc b/src/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.2.inc new file mode 100644 index 0000000000..bfd1cfe25e --- /dev/null +++ b/src/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.2.inc @@ -0,0 +1,9 @@ +