Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BCFile/various methods: compatibility with the PHP 8 identifier name tokenization #213

Merged
merged 1 commit into from
Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions PHPCSUtils/BackCompat/BCFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ public static function getDeclarationName(File $phpcsFile, $stackPtr)
*
* @since 1.0.0
* @since 1.0.0-alpha2 Added BC support for PHP 7.4 arrow functions.
* @since 1.0.0-alpha4 Added support for PHP 8.0 identifier name tokens.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position in the stack of the function token
Expand Down Expand Up @@ -381,7 +382,10 @@ public static function getMethodParameters(File $phpcsFile, $stackPtr)
}
break;
case 'T_STRING':
// This is a string, so it may be a type hint, but it could
case 'T_NAME_QUALIFIED':
case 'T_NAME_FULLY_QUALIFIED':
case 'T_NAME_RELATIVE':
// This is an identifier name, so it may be a type hint, but it could
// also be a constant used as a default value.
$prevComma = false;
for ($t = $i; $t >= $opener; $t--) {
Expand Down Expand Up @@ -987,6 +991,7 @@ public static function getClassProperties(File $phpcsFile, $stackPtr)
*
* @since 1.0.0
* @since 1.0.0-alpha2 Added BC support for PHP 7.4 arrow functions.
* @since 1.0.0-alpha4 Added support for PHP 8.0 identifier name tokens.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the `T_BITWISE_AND` token.
Expand Down Expand Up @@ -1074,7 +1079,11 @@ public static function isReference(File $phpcsFile, $stackPtr)
if ($tokens[$tokenAfter]['code'] === T_VARIABLE) {
return true;
} else {
$skip = Tokens::$emptyTokens;
$skip = Tokens::$emptyTokens;

// BC for PHP 8 in combination with PHPCS < 3.5.7.
$skip += Collections::nameTokens();

$skip[] = T_NS_SEPARATOR;
$skip[] = T_SELF;
$skip[] = T_PARENT;
Expand Down Expand Up @@ -1457,6 +1466,7 @@ public static function getCondition(File $phpcsFile, $stackPtr, $type, $first =
* @see \PHPCSUtils\Utils\ObjectDeclarations::findExtendedClassName() PHPCSUtils native improved version.
*
* @since 1.0.0
* @since 1.0.0-alpha4 Added support for PHP 8.0 identifier name tokens.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The stack position of the class or interface.
Expand Down Expand Up @@ -1491,11 +1501,14 @@ public static function findExtendedClassName(File $phpcsFile, $stackPtr)
}

$find = [
T_NS_SEPARATOR,
T_STRING,
T_WHITESPACE,
T_NS_SEPARATOR => T_NS_SEPARATOR,
T_STRING => T_STRING,
T_WHITESPACE => T_WHITESPACE,
];

// BC for PHP 8 in combination with PHPCS < 3.5.7.
$find += Collections::nameTokens();

$end = $phpcsFile->findNext($find, ($extendsIndex + 1), ($classOpenerIndex + 1), true);
$name = $phpcsFile->getTokensAsString(($extendsIndex + 1), ($end - $extendsIndex - 1));
$name = trim($name);
Expand All @@ -1520,6 +1533,7 @@ public static function findExtendedClassName(File $phpcsFile, $stackPtr)
* @see \PHPCSUtils\Utils\ObjectDeclarations::findImplementedInterfaceNames() PHPCSUtils native improved version.
*
* @since 1.0.0
* @since 1.0.0-alpha4 Added support for PHP 8.0 identifier name tokens.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The stack position of the class.
Expand Down Expand Up @@ -1553,12 +1567,15 @@ public static function findImplementedInterfaceNames(File $phpcsFile, $stackPtr)
}

$find = [
T_NS_SEPARATOR,
T_STRING,
T_WHITESPACE,
T_COMMA,
T_NS_SEPARATOR => T_NS_SEPARATOR,
T_STRING => T_STRING,
T_WHITESPACE => T_WHITESPACE,
T_COMMA => T_COMMA,
];

// BC for PHP 8 in combination with PHPCS < 3.5.7.
$find += Collections::nameTokens();

$end = $phpcsFile->findNext($find, ($implementsIndex + 1), ($classOpenerIndex + 1), true);
$name = $phpcsFile->getTokensAsString(($implementsIndex + 1), ($end - $implementsIndex - 1));
$name = trim($name);
Expand Down
3 changes: 3 additions & 0 deletions Tests/BackCompat/BCFile/IsReferenceTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ functionCall($something, &\SomeNS\SomeClass::$somethingElse);
/* testPassByReferenceJ */
functionCall($something, &namespace\SomeClass::$somethingElse);

/* testPassByReferencePartiallyQualifiedName */
functionCall($something, &Sub\Level\SomeClass::$somethingElse);

/* testNewByReferenceA */
$foobar2 = &new Foobar();

Expand Down
4 changes: 4 additions & 0 deletions Tests/BackCompat/BCFile/IsReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ public function dataIsReference()
'/* testPassByReferenceJ */',
true,
],
[
'/* testPassByReferencePartiallyQualifiedName */',
true,
],
[
'/* testNewByReferenceA */',
true,
Expand Down
3 changes: 0 additions & 3 deletions Tests/Utils/Operators/IsReferenceDiffTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,3 @@ $fn = fn($param, &...$moreParams) => 1;

/* testArrowFunctionNonReferenceInDefault */
$fn = fn( $one = E_NOTICE & E_STRICT) => 1;

/* testPassByReferencePartiallyQualifiedName */
functionCall($something, &Sub\Level\SomeClass::$somethingElse);
4 changes: 0 additions & 4 deletions Tests/Utils/Operators/IsReferenceDiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ public function dataIsReference()
'/* testArrowFunctionNonReferenceInDefault */',
false,
],
'pass-by-ref-partially-qualified-name-param-type' => [
'/* testPassByReferencePartiallyQualifiedName */',
true,
],
];
}
}