From 178ed0f38d0653c2654d357200faea48a8f4cb98 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 13 Feb 2025 00:43:48 +0100 Subject: [PATCH 1/2] GetDeclarationNameTest: move parse error test to separate file --- .../GetDeclarationNameParseError1Test.inc | 5 +++ .../GetDeclarationNameParseError1Test.php | 37 +++++++++++++++++++ tests/Core/File/GetDeclarationNameTest.inc | 4 -- tests/Core/File/GetDeclarationNameTest.php | 4 -- 4 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 tests/Core/File/GetDeclarationNameParseError1Test.inc create mode 100644 tests/Core/File/GetDeclarationNameParseError1Test.php 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/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() From 604b7a02654aa0aacb50dae5006ca379ede10011 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 13 Feb 2025 00:52:43 +0100 Subject: [PATCH 2/2] File::getDeclarationName(): prevent incorrect result during live coding The `function` keyword for a closure is normally tokenized as `T_CLOSURE`, but will be tokenized as `T_FUNCTION` in case of an unfinished closure declaration (no scope opener/closer). For an unfinished closure with typed parameters, the `File::getDeclarationName()` method would incorrectly return the contents of the first `T_STRING` in the type declaration as if it were the name of the function. Fixed now. Includes test. --- src/Files/File.php | 11 +++++- .../GetDeclarationNameParseError2Test.inc | 6 +++ .../GetDeclarationNameParseError2Test.php | 37 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/Core/File/GetDeclarationNameParseError2Test.inc create mode 100644 tests/Core/File/GetDeclarationNameParseError2Test.php 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/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