diff --git a/src/Files/File.php b/src/Files/File.php index 29b15c0e08..e6c7e608ce 100644 --- a/src/Files/File.php +++ b/src/Files/File.php @@ -1304,8 +1304,17 @@ public function getDeclarationName($stackPtr) return $this->tokens[$stackPtr]['content']; } + $stopPoint = $this->numTokens; + if (isset($this->tokens[$stackPtr]['parenthesis_opener']) === true) { + // For functions, stop searching at the parenthesis opener. + $stopPoint = $this->tokens[$stackPtr]['parenthesis_opener']; + } else if (isset($this->tokens[$stackPtr]['scope_opener']) === true) { + // For OO tokens, stop searching at the open curly. + $stopPoint = $this->tokens[$stackPtr]['scope_opener']; + } + $content = null; - for ($i = $stackPtr; $i < $this->numTokens; $i++) { + for ($i = $stackPtr; $i < $stopPoint; $i++) { if ($this->tokens[$i]['code'] === T_STRING) { $content = $this->tokens[$i]['content']; break; diff --git a/tests/Core/File/GetDeclarationNameParseError1Test.inc b/tests/Core/File/GetDeclarationNameParseError1Test.inc new file mode 100644 index 0000000000..451d4dfb82 --- /dev/null +++ b/tests/Core/File/GetDeclarationNameParseError1Test.inc @@ -0,0 +1,5 @@ + + * @copyright 2025 PHPCSStandards Contributors + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Tests\Core\File; + +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; + +/** + * Tests for the \PHP_CodeSniffer\Files\File:getDeclarationName method. + * + * @covers \PHP_CodeSniffer\Files\File::getDeclarationName + */ +final class GetDeclarationNameParseError1Test extends AbstractMethodUnitTest +{ + + + /** + * Test receiving "null" in case of a parse error. + * + * @return void + */ + public function testGetDeclarationNameNull() + { + $target = $this->getTargetToken('/* testLiveCoding */', T_FUNCTION); + $result = self::$phpcsFile->getDeclarationName($target); + $this->assertNull($result); + + }//end testGetDeclarationNameNull() + + +}//end class diff --git a/tests/Core/File/GetDeclarationNameParseError2Test.inc b/tests/Core/File/GetDeclarationNameParseError2Test.inc new file mode 100644 index 0000000000..e9a61cf411 --- /dev/null +++ b/tests/Core/File/GetDeclarationNameParseError2Test.inc @@ -0,0 +1,6 @@ + + * @copyright 2025 PHPCSStandards Contributors + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Tests\Core\File; + +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; + +/** + * Tests for the \PHP_CodeSniffer\Files\File:getDeclarationName method. + * + * @covers \PHP_CodeSniffer\Files\File::getDeclarationName + */ +final class GetDeclarationNameParseError2Test extends AbstractMethodUnitTest +{ + + + /** + * Test receiving "null" in case of a parse error. + * + * @return void + */ + public function testGetDeclarationNameNull() + { + $target = $this->getTargetToken('/* testLiveCoding */', T_FUNCTION); + $result = self::$phpcsFile->getDeclarationName($target); + $this->assertNull($result); + + }//end testGetDeclarationNameNull() + + +}//end class diff --git a/tests/Core/File/GetDeclarationNameTest.inc b/tests/Core/File/GetDeclarationNameTest.inc index 14902245bb..a7a53c9a1a 100644 --- a/tests/Core/File/GetDeclarationNameTest.inc +++ b/tests/Core/File/GetDeclarationNameTest.inc @@ -96,7 +96,3 @@ function &self() {} /* testFunctionReturnByRefWithReservedKeywordStatic */ function &static() {} - -/* testLiveCoding */ -// Intentional parse error. This has to be the last test in the file. -function // Comment. diff --git a/tests/Core/File/GetDeclarationNameTest.php b/tests/Core/File/GetDeclarationNameTest.php index 19c798f50e..eb665681be 100644 --- a/tests/Core/File/GetDeclarationNameTest.php +++ b/tests/Core/File/GetDeclarationNameTest.php @@ -84,10 +84,6 @@ public static function dataGetDeclarationNameNull() 'testMarker' => '/* testAnonClassExtendsWithoutParens */', 'targetType' => T_ANON_CLASS, ], - 'live-coding' => [ - 'testMarker' => '/* testLiveCoding */', - 'targetType' => T_FUNCTION, - ], ]; }//end dataGetDeclarationNameNull()