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

Nullsafe operator: sync with phpcs #183

Merged
merged 2 commits into from
Sep 2, 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
22 changes: 11 additions & 11 deletions PHPCSUtils/Tokens/Collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -585,14 +585,14 @@ public static function functionDeclarationTokensBC()
* Note: this is a method, not a property as the `T_NULLSAFE_OBJECT_OPERATOR` token may not exist.
*
* Sister-method to the {@see Collections::objectOperatorsBC()} method.
* This method supports PHP 8.0 and up.
* The {@see Collections::objectOperatorsBC()} method supports PHP < 8.0.
* This method supports PHPCS 3.5.7 and up.
* The {@see Collections::objectOperatorsBC()} method supports PHPCS 2.6.0 and up.
*
* This method can also safely be used if the token collection is only used when looking back
* via `$phpcsFile->findPrevious()` as in that case, a non-backfilled nullsafe object operator
* will still match the "normal" object operator.
*
* @see \PHPCSUtils\Tokens\Collections::objectOperatorsBC() Related method (PHP < 8.0).
* @see \PHPCSUtils\Tokens\Collections::objectOperatorsBC() Related method (PHPCS 2.6.0+).
*
* @since 1.0.0-alpha4
*
Expand All @@ -606,7 +606,7 @@ public static function objectOperators()
];

if (\defined('T_NULLSAFE_OBJECT_OPERATOR') === true) {
// PHP 8.0.
// PHP >= 8.0 or PHPCS >= 3.5.7.
$tokens[\T_NULLSAFE_OBJECT_OPERATOR] = \T_NULLSAFE_OBJECT_OPERATOR;
}

Expand All @@ -619,15 +619,15 @@ public static function objectOperators()
* Note: this is a method, not a property as the `T_NULLSAFE_OBJECT_OPERATOR` token may not exist.
*
* Sister-method to the {@see Collections::objectOperators()} method.
* The {@see Collections::objectOperators()} method supports PHP 8.0 and up.
* This method supports PHP < 8.0.
* The {@see Collections::objectOperators()} method supports PHPCS 3.5.7 and up.
* This method supports PHPCS 2.6.0 and up.
*
* Notable difference:
* - This method accounts for tokens which may be encountered when the `T_NULLSAFE_OBJECT_OPERATOR` token
* doesn't exist.
*
* It is recommended to use the {@see Collections::objectOperators()} method instead of
* this method if a standard does not need to support PHP < 8.0.
* this method if a standard does not need to support PHPCS < 3.5.7.
*
* The {@see Collections::objectOperators()} method can also safely be used if the token collection
* is only used when looking back via `$phpcsFile->findPrevious()` as in that case, a non-backfilled
Expand All @@ -637,11 +637,11 @@ public static function objectOperators()
* method needs to be used on potential nullsafe object operator tokens to verify whether it really
* is a nullsafe object operator or not.
*
* @see \PHPCSUtils\Tokens\Collections::objectOperators() Related method (PHP 8.0+).
* @see \PHPCSUtils\Tokens\Collections::objectOperators() Related method (PHPCS 3.5.7+).
* @see \PHPCSUtils\Tokens\Collections::nullsafeObjectOperatorBC() Tokens which can represent a
* nullsafe object operator.
* @see \PHPCSUtils\Utils\Operators::isNullsafeObjectOperator() Nullsafe object operator detection for
* PHP < 8.0.
* PHPCS < 3.5.7.
*
* @since 1.0.0-alpha4
*
Expand Down Expand Up @@ -671,7 +671,7 @@ public static function objectOperatorsBC()
* is a nullsafe object operator or not.
*
* @see \PHPCSUtils\Utils\Operators::isNullsafeObjectOperator() Nullsafe object operator detection for
* PHP < 8.0.
* PHPCS < 3.5.7.
*
* @since 1.0.0-alpha4
*
Expand All @@ -680,7 +680,7 @@ public static function objectOperatorsBC()
public static function nullsafeObjectOperatorBC()
{
if (\defined('T_NULLSAFE_OBJECT_OPERATOR') === true) {
// PHP 8.0.
// PHP >= 8.0 / PHPCS >= 3.5.7.
return [
\T_NULLSAFE_OBJECT_OPERATOR => \T_NULLSAFE_OBJECT_OPERATOR,
];
Expand Down
6 changes: 5 additions & 1 deletion PHPCSUtils/Utils/Operators.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ public static function isTypeUnion(File $phpcsFile, $stackPtr)
*
* Helper method for PHP < 8.0 in combination with PHPCS versions in which the
* `T_NULLSAFE_OBJECT_OPERATOR` token is not yet backfilled.
* PHPCS backfills the token as of PHPCS 3.5.7.
*
* @since 1.0.0-alpha4
*
Expand All @@ -332,7 +333,10 @@ public static function isNullsafeObjectOperator(File $phpcsFile, $stackPtr)
return false;
}

// Safeguard in case this method is used on PHP 8 and the nullsafe object operator would be passed.
/*
* Safeguard in case this method is used on PHP >= 8.0 or PHPCS >= 3.5.7
* and the nullsafe object operator would be passed.
*/
if ($tokens[$stackPtr]['type'] === 'T_NULLSAFE_OBJECT_OPERATOR') {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace PHPCSUtils\Tests\Tokens\Collections;

use PHPCSUtils\BackCompat\Helper;
use PHPCSUtils\Tokens\Collections;
use PHPUnit\Framework\TestCase;

Expand All @@ -32,8 +33,10 @@ class NullsafeObjectOperatorBCTest extends TestCase
*/
public function testNullsafeObjectOperatorBC()
{
$version = Helper::getVersion();
$expected = [];
if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true
|| \version_compare($version, '3.5.7', '>=') === true
) {
$expected = [
\T_NULLSAFE_OBJECT_OPERATOR => \T_NULLSAFE_OBJECT_OPERATOR,
Expand Down
3 changes: 3 additions & 0 deletions Tests/Tokens/Collections/ObjectOperatorsBCTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace PHPCSUtils\Tests\Tokens\Collections;

use PHPCSUtils\BackCompat\Helper;
use PHPCSUtils\Tokens\Collections;
use PHPUnit\Framework\TestCase;

Expand All @@ -32,12 +33,14 @@ class ObjectOperatorsBCTest extends TestCase
*/
public function testObjectOperatorsBC()
{
$version = Helper::getVersion();
$expected = [
\T_OBJECT_OPERATOR => \T_OBJECT_OPERATOR,
\T_DOUBLE_COLON => \T_DOUBLE_COLON,
];

if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true
|| \version_compare($version, '3.5.7', '>=') === true
) {
$expected[\T_NULLSAFE_OBJECT_OPERATOR] = \T_NULLSAFE_OBJECT_OPERATOR;
} else {
Expand Down
3 changes: 3 additions & 0 deletions Tests/Tokens/Collections/ObjectOperatorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace PHPCSUtils\Tests\Tokens\Collections;

use PHPCSUtils\BackCompat\Helper;
use PHPCSUtils\Tokens\Collections;
use PHPUnit\Framework\TestCase;

Expand All @@ -32,12 +33,14 @@ class ObjectOperatorsTest extends TestCase
*/
public function testObjectOperators()
{
$version = Helper::getVersion();
$expected = [
\T_OBJECT_OPERATOR => \T_OBJECT_OPERATOR,
\T_DOUBLE_COLON => \T_DOUBLE_COLON,
];

if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true
|| \version_compare($version, '3.5.7', '>=') === true
) {
$expected[\T_NULLSAFE_OBJECT_OPERATOR] = \T_NULLSAFE_OBJECT_OPERATOR;
}
Expand Down