Skip to content

Commit 79abb87

Browse files
authored
Merge pull request #494 from PHPCSStandards/backcompat/bcfile-getdeclarationname-sync-with-upstream
BCFile::getDeclarationName(): sync with upstream
2 parents 3466697 + d16f825 commit 79abb87

File tree

5 files changed

+61
-23
lines changed

5 files changed

+61
-23
lines changed

PHPCSUtils/BackCompat/BCFile.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* any affected utility functions:
5151
* - `readonly` classes.
5252
* - Constructor property promotion with `readonly` without visibility.
53+
* - OO methods called `self`, `parent` or `static`.
5354
*
5455
* Most functions in this class will have a related twin-function in the relevant
5556
* class in the `PHPCSUtils\Utils` namespace.
@@ -75,7 +76,7 @@ final class BCFile
7576
*
7677
* Changelog for the PHPCS native function:
7778
* - Introduced in PHPCS 0.0.5.
78-
* - The upstream method has received no significant updates since PHPCS 3.7.1.
79+
* - PHPCS 3.8.0: OO methods called `self`, `parent` or `static` are now correctly recognized.
7980
*
8081
* @see \PHP_CodeSniffer\Files\File::getDeclarationName() Original source.
8182
* @see \PHPCSUtils\Utils\ObjectDeclarations::getName() PHPCSUtils native improved version.
@@ -97,7 +98,44 @@ final class BCFile
9798
*/
9899
public static function getDeclarationName(File $phpcsFile, $stackPtr)
99100
{
100-
return $phpcsFile->getDeclarationName($stackPtr);
101+
$tokens = $phpcsFile->getTokens();
102+
$tokenCode = $tokens[$stackPtr]['code'];
103+
104+
if ($tokenCode === T_ANON_CLASS || $tokenCode === T_CLOSURE) {
105+
return null;
106+
}
107+
108+
if ($tokenCode !== T_FUNCTION
109+
&& $tokenCode !== T_CLASS
110+
&& $tokenCode !== T_INTERFACE
111+
&& $tokenCode !== T_TRAIT
112+
&& $tokenCode !== T_ENUM
113+
) {
114+
throw new RuntimeException('Token type "' . $tokens[$stackPtr]['type'] . '" is not T_FUNCTION, T_CLASS, T_INTERFACE, T_TRAIT or T_ENUM');
115+
}
116+
117+
if ($tokenCode === T_FUNCTION
118+
&& strtolower($tokens[$stackPtr]['content']) !== 'function'
119+
) {
120+
// This is a function declared without the "function" keyword.
121+
// So this token is the function name.
122+
return $tokens[$stackPtr]['content'];
123+
}
124+
125+
$content = null;
126+
for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
127+
if ($tokens[$i]['code'] === T_STRING
128+
// BC: PHPCS < 3.8.0.
129+
|| $tokens[$i]['code'] === T_SELF
130+
|| $tokens[$i]['code'] === T_PARENT
131+
|| $tokens[$i]['code'] === T_STATIC
132+
) {
133+
$content = $tokens[$i]['content'];
134+
break;
135+
}
136+
}
137+
138+
return $content;
101139
}
102140

103141
/**

Tests/BackCompat/BCFile/GetDeclarationNameTest.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ enum Suit: int implements Colorful, CardGame {}
8888
/* testFunctionReturnByRefWithReservedKeywordEach */
8989
function &each() {}
9090

91+
/* testFunctionReturnByRefWithReservedKeywordParent */
92+
function &parent() {}
93+
94+
/* testFunctionReturnByRefWithReservedKeywordSelf */
95+
function &self() {}
96+
97+
/* testFunctionReturnByRefWithReservedKeywordStatic */
98+
function &static() {}
99+
91100
/* testLiveCoding */
92101
// Intentional parse error. This has to be the last test in the file.
93102
function // Comment.

Tests/BackCompat/BCFile/GetDeclarationNameTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,18 @@ public static function dataGetDeclarationName()
196196
'/* testFunctionReturnByRefWithReservedKeywordEach */',
197197
'each',
198198
],
199+
'function-return-by-reference-with-reserved-keyword-parent' => [
200+
'/* testFunctionReturnByRefWithReservedKeywordParent */',
201+
'parent',
202+
],
203+
'function-return-by-reference-with-reserved-keyword-self' => [
204+
'/* testFunctionReturnByRefWithReservedKeywordSelf */',
205+
'self',
206+
],
207+
'function-return-by-reference-with-reserved-keyword-static' => [
208+
'/* testFunctionReturnByRefWithReservedKeywordStatic */',
209+
'static',
210+
],
199211
];
200212
}
201213
}

Tests/Utils/ObjectDeclarations/GetNameDiffTest.inc

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,6 @@ interface switch{ // Intentional parse error.
1212
public function someFunction();
1313
}
1414

15-
/* testFunctionReturnByRefWithReservedKeywordParent */
16-
function &parent() {}
17-
18-
/* testFunctionReturnByRefWithReservedKeywordSelf */
19-
function &self() {}
20-
21-
/* testFunctionReturnByRefWithReservedKeywordStatic */
22-
function &static() {}
23-
2415
/* testLiveCoding */
2516
// Intentional parse error. Redundancy testing.
2617
abstract class

Tests/Utils/ObjectDeclarations/GetNameDiffTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,6 @@ public static function dataGetName()
117117
'testMarker' => '/* testInvalidInterfaceName */',
118118
'expected' => 'switch',
119119
],
120-
'function-return-by-reference-with-reserved-keyword-parent' => [
121-
'/* testFunctionReturnByRefWithReservedKeywordParent */',
122-
'parent',
123-
],
124-
'function-return-by-reference-with-reserved-keyword-self' => [
125-
'/* testFunctionReturnByRefWithReservedKeywordSelf */',
126-
'self',
127-
],
128-
'function-return-by-reference-with-reserved-keyword-static' => [
129-
'/* testFunctionReturnByRefWithReservedKeywordStatic */',
130-
'static',
131-
],
132120
];
133121
}
134122
}

0 commit comments

Comments
 (0)