Namespaces/ReservedNames: various improvements by jrfnl · Pull Request #1729 · PHPCompatibility/PHPCompatibility
…sions
PECL extensions are PHP extensions which are not bundled with PHP releases.
Extensions are often initially developed on as PECL extensions, but if they are widely adopted, stable and maintained they can be moved to PHP src.
Along the same lines, unmaintained extensions may be unbundled from PHP and moved to PECL.
Userland packages using a top-level namespace in use by a PECL extension could run into trouble as:
1. The server the software runs on may have the extension enabled.
This can lead to nasty bugs and unexpected runtime behaviour as PHP will favour the code in the extension over any userland code, so if there is a same-named class, function, constant, the autoloader for the userland package will not kick in and the extension version will be used instead.
Additionally, if the code is loaded without an autoloader, it can lead to fatal "class already declared" errors and such.
2. PECL extensions may at some point be moved to and bundled with PHP itself, at which point, the problem outlined in [1] becomes even more widespread.
With this in mind and with the "Namespaces in bundled extensions" policy RFC having been accepted during the PHP 8.1 dev cycle and actively encouraging use of namespaces in PHP extensions, I propose we also start flagging top-level namespace names in use by PECL extensions.
Though, as it is not a given that this use will be problematic, I propose flagging these only with a "warning".
The error code for the warning is modular and will include the name flagged, allowing for selective exclusion of the warnings for just one particular namespace name.
The error code suffix - `PECLReserved` - is also different from the error code suffix for other errors/warnings thrown by this sniff (`Found`). This is deliberate to ensure that if a PECL extension becomes a bundled extension and a user of PHPCompatibility would have excluded the warning, they will still get to see the _error_ once the extension has been moved to PHP.
Includes unit tests.
Note: this commit will be easier to review while ignoring whitespace.
Open questions:
1. Should we check flag namespaces used by both the PECL extensions listed in the PHP manual as well as the RFC ? Or only those listed in the manual ?
2. Should the warning be thrown at severity 5 (current implementation) ? Or should we lower the severity for these warnings to, for instance, 3 ?
Lowering the severity would mean that people would not see the warning by default and would need to explicitly set `--severity=3` to see them, making the warning less effective.
For now, I've implemented the feature with the normal severity of `5`, but with modular error codes, which means that selectively ignoring a particular warning from a ruleset or via an inline ignore annotation is straight-forward.
Ref:
* https://wiki.php.net/rfc/namespaces_in_bundled_extensions
Related to 1299