Skip to content

Commit dc1f23e

Browse files
committed
BCFile::findExtendedClassName(): sync with PHPCS 4.0 / handling of namespace relative parent classes
The PHPCS `File::findExtendedClassName()` method did not handle namespace relative parent classes correctly prior to PHPCS 4.0. The PHPCSUtils native `ObjectDeclarations::findExtendedClassName()` method **_did_** already handle this correctly. This commit adds the PHPCS 4.x version of the `findExtendedClassName()` method to the `BCFile` class. Includes moving related tests from the "Diff" test file to the "normal" test file and updating the docs to annotate this is no longer a difference between the PHPCS native and PHPCSUtils versions of the method.
1 parent a542524 commit dc1f23e

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

PHPCSUtils/BackCompat/BCFile.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ public static function getCondition(File $phpcsFile, $stackPtr, $type, $first =
905905
*
906906
* Changelog for the PHPCS native function:
907907
* - Introduced in PHPCS 1.2.0.
908-
* - The upstream method has received no significant updates since PHPCS 3.13.0.
908+
* - PHPCS 4.0.0: Handling of the namespace relative parent class using the namespace keyword as operator.
909909
*
910910
* @see \PHP_CodeSniffer\Files\File::findExtendedClassName() Original source.
911911
* @see \PHPCSUtils\Utils\ObjectDeclarations::findExtendedClassName() PHPCSUtils native improved version.
@@ -920,7 +920,42 @@ public static function getCondition(File $phpcsFile, $stackPtr, $type, $first =
920920
*/
921921
public static function findExtendedClassName(File $phpcsFile, $stackPtr)
922922
{
923-
return $phpcsFile->findExtendedClassName($stackPtr);
923+
$tokens = $phpcsFile->getTokens();
924+
925+
// Check for the existence of the token.
926+
if (isset($tokens[$stackPtr]) === false) {
927+
return false;
928+
}
929+
930+
if ($tokens[$stackPtr]['code'] !== T_CLASS
931+
&& $tokens[$stackPtr]['code'] !== T_ANON_CLASS
932+
&& $tokens[$stackPtr]['code'] !== T_INTERFACE
933+
) {
934+
return false;
935+
}
936+
937+
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
938+
return false;
939+
}
940+
941+
$classOpenerIndex = $tokens[$stackPtr]['scope_opener'];
942+
$extendsIndex = $phpcsFile->findNext(T_EXTENDS, $stackPtr, $classOpenerIndex);
943+
if ($extendsIndex === false) {
944+
return false;
945+
}
946+
947+
$find = Collections::namespacedNameTokens();
948+
$find[] = T_WHITESPACE;
949+
950+
$end = $phpcsFile->findNext($find, ($extendsIndex + 1), ($classOpenerIndex + 1), true);
951+
$name = $phpcsFile->getTokensAsString(($extendsIndex + 1), ($end - $extendsIndex - 1));
952+
$name = trim($name);
953+
954+
if ($name === '') {
955+
return false;
956+
}
957+
958+
return $name;
924959
}
925960

926961
/**

PHPCSUtils/Utils/ObjectDeclarations.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ public static function getClassProperties(File $phpcsFile, $stackPtr)
242242
* - Bugs fixed:
243243
* - Handling of PHPCS annotations.
244244
* - Handling of comments.
245-
* - Handling of the namespace keyword used as operator.
246245
* - Improved handling of parse errors.
247246
* - The returned name will be clean of superfluous whitespace and/or comments.
248247
* - Support for PHP 8.0 tokenization of identifier/namespaced names, cross-version PHP & PHPCS.

Tests/BackCompat/BCFile/FindExtendedClassNameTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class testFECNNamespacedClass extends \PHP_CodeSniffer\Tests\Core\File\testFECNC
1515
/* testExtendsPartiallyQualifiedClass */
1616
class testFECNQualifiedClass extends Core\File\RelativeClass {}
1717

18+
/* testExtendsNamespaceRelativeClass */
19+
class testWithNSOperator extends namespace\Bar {}
20+
1821
/* testNonExtendedInterface */
1922
interface testFECNInterface {}
2023

Tests/BackCompat/BCFile/FindExtendedClassNameTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ public static function dataExtendedClass()
121121
'identifier' => '/* testExtendsPartiallyQualifiedClass */',
122122
'expected' => 'Core\File\RelativeClass',
123123
],
124+
'class extends namespace relative class' => [
125+
'identifier' => '/* testExtendsNamespaceRelativeClass */',
126+
'expected' => 'namespace\Bar',
127+
],
124128
'interface does not extend' => [
125129
'identifier' => '/* testNonExtendedInterface */',
126130
'expected' => false,

Tests/Utils/ObjectDeclarations/FindExtendedClassNameDiffTest.inc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,5 @@ class testDeclarationWithComments
66
// phpcs:ignore Stnd.Cat.Sniff -- For reasons.
77
\Package\SubDir /* comment */ \ /* comment */ SomeClass /* comment */ {}
88

9-
/* testExtendedClassUsingNamespaceOperator */
10-
class testWithNSOperator extends namespace\Bar {}
11-
129
/* testExtendedClassStrayComma */
1310
class testExtendedClassStrayComma extends , testClass {} // Intentional parse error.

Tests/Utils/ObjectDeclarations/FindExtendedClassNameDiffTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ public static function dataFindExtendedClassName()
6060
'testMarker' => '/* testDeclarationWithComments */',
6161
'expected' => '\Package\SubDir\SomeClass',
6262
],
63-
'namespace-operator' => [
64-
'testMarker' => '/* testExtendedClassUsingNamespaceOperator */',
65-
'expected' => 'namespace\Bar',
66-
],
6763
'parse-error-stray-comma' => [
6864
'testMarker' => '/* testExtendedClassStrayComma */',
6965
'expected' => 'testClass',

0 commit comments

Comments
 (0)