From c32f10d064ef4cebf6a0454e3d6997d32ae94280 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 9 Sep 2020 23:58:24 +0200 Subject: [PATCH] UtilityMethodTestCase: new `usesPhp8NameTokens()` method 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](https://github.com/squizlabs/PHP_CodeSniffer/issues/3041) * [Open PR for the PHPCS 3.x branch to "undo" the PHP 8 tokenization](https://github.com/squizlabs/PHP_CodeSniffer/pull/3063) --- .../TestUtils/UtilityMethodTestCase.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/PHPCSUtils/TestUtils/UtilityMethodTestCase.php b/PHPCSUtils/TestUtils/UtilityMethodTestCase.php index a5b7449f..3e42f916 100644 --- a/PHPCSUtils/TestUtils/UtilityMethodTestCase.php +++ b/PHPCSUtils/TestUtils/UtilityMethodTestCase.php @@ -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. *