Treat namespaced names as single token (reduced version) by nikic · Pull Request #5827 · php/php-src

@nikic nikic mentioned this pull request

Jul 15, 2020

TysonAndre

@nikic

@nikic

@nikic nikic mentioned this pull request

Jul 22, 2020

This was referenced

Aug 4, 2020

fabpot added a commit to symfony/symfony that referenced this pull request

Aug 10, 2020
…errabus)

This PR was merged into the 3.4 branch.

Discussion
----------

[ClassLoader][Routing] Fix namespace parsing on php 8

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | N/A
| License       | MIT
| Doc PR        | N/A

The way namespace declarations are parsed has changed in php 8 (see php/php-src#5827). This PR should fix the corresponding issues in the ClassLoader and Routing components.

Note that Doctrine Annotations suffers from the same issue (doctrine/annotations#339). I had to run the Routing tests locally with doctrine/annotations#344 applied.

Commits
-------

3d293b2 [ClassLoader][Routing] Fix namespace parsing on php 8.

jrfnl added a commit to PHPCompatibility/PHPCompatibility that referenced this pull request

Nov 1, 2022
…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

jrfnl added a commit to PHPCompatibility/PHPCompatibility that referenced this pull request

Nov 1, 2022
…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

@jrfnl jrfnl mentioned this pull request

Nov 1, 2022

jrfnl added a commit to PHPCompatibility/PHPCompatibility that referenced this pull request

Nov 1, 2022
…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