PHP 8.0 | ForbiddenNames: don't report reserved names in namespace declarations by jrfnl · Pull Request #1402 · PHPCompatibility/PHPCompatibility
…clarations
As of PHP 8.0, reserved keywords can be used in namespace declarations, with the exception of the `namespace` keyword at the start of a namespace name.
> In the interest of consistency, the namespace declaration will accept any name, including isolated reserved keywords:
>
> ```php
> namespace iter\fn; // Legal
> namespace fn; // Legal
> ```
> This is to avoid a discrepancy where defining symbols like `iter\fn\operator` is allowed, but `fn\operator` is not. The only restriction is that the namespace name cannot start with a `namespace` segment:
>
> ```php
> namespace namespace; // Illegal
> namespace namespace\x; // Illegal
> ```
This commit:
* Adjusts the `ForbiddenNames` sniff to prevent triggering an error for reserved keywords being used in a namespace name when the `testVersion` has a minimum version of PHP `8.0` or higher.
* Adjusts the `ForbiddenNames` sniff to be cross-version compatible with different PHP/PHPCS versions for the new "namespaced names as single token" tokens.
This addresses issue 1226 for this sniff.
* Adjusts the testcase generation file to exclude the testcase with a namespace declaration using `namespace` at the start of the name, including a regenerated test case file as the behaviour is different compared to the other keywords.
* Special cases the "namespace declaration" tests.
These will now trigger an error when PHP < 8.0 needs to be supported, but will no longer trigger an error when PHP 8.0 is the minimum supported version according to the `testVersion` setting.
* Adds separate test cases to the `ForbiddenNamesUnitTest.3.inc` test file to verify the correct handling of a namespace name starting with `namespace`.
* Annotates that the testcase on line 8 in the `ForbiddenNamesUnitTest.3.inc` test case file will only trigger errors when PHP < 8 is part of the `testVersion` setting and adjusts the unit test to make sure a `testVersion` is passed for that test.
Ref:
* https://wiki.php.net/rfc/namespaced_names_as_token
* php/php-src#5827
* php/php-src@7a3dcc3