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 /**
0 commit comments