Skip to content

Commit

Permalink
UtilityMethodTestCase: new usesPhp8NameTokens() method
Browse files Browse the repository at this point in the history
In PHP 8.0 identifier name tokenization will change as outline in the [accepted RFC "Treat namespaced names as single token"](https://wiki.php.net/rfc/namespaced_names_as_token).

When the PHP 8.0 identifier name tokenization is used, the target token to find for some tests will need to be a different token - for instance: `T_STRING` vs `T_FULLY_QUALIFIED_NAME` -.
Along the same lines, the expected token positions in the return value of various functions will also be different when the PHP < 8.0 tokenization is used as certain tokens will be "squashed" into one token.

This adds a test helper method to allow tests to "know" whether or not to expect the PHP 8.0 identifier name tokenization, so the test setup/expectations can be adjusted based on the expected tokenization.

The method is based on the _current reality_.
At this time the PHP 8 tokenization should be expected on all PHPCS versions when run on PHP 8.

Refs:
* https://wiki.php.net/rfc/namespaced_names_as_token
* [Proposal for handling this PHP 8 change in PHPCS](squizlabs/PHP_CodeSniffer#3041)
* [Open PR for the PHPCS 3.x branch to "undo" the PHP 8 tokenization](squizlabs/PHP_CodeSniffer#3063)
  • Loading branch information
jrfnl committed Sep 13, 2020
1 parent be015ea commit c32f10d
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions PHPCSUtils/TestUtils/UtilityMethodTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,27 @@ public static function resetTestFile()
self::$phpcsFile = null;
}

/**
* Check whether or not the PHP 8.0 identifier name tokens will be in use.
*
* The expected token positions/token counts for certain tokens will differ depending
* on whether the PHP 8.0 identifier name tokenization is used or the PHP < 8.0
* identifier name tokenization.
*
* Tests can use this method to determine which flavour of tokenization to expect and
* to set test expectations accordingly.
*
* @codeCoverageIgnore Nothing to test.
*
* @since 1.0.0
*
* @return bool
*/
public static function usesPhp8NameTokens()
{
return \version_compare(\PHP_VERSION_ID, '80000', '>=') === true;
}

/**
* Get the token pointer for a target token based on a specific comment.
*
Expand Down

0 comments on commit c32f10d

Please sign in to comment.