PHPCSStandards / PHPCSUtils

A suite of utility functions for use with PHP_CodeSniffer
https://phpcsutils.com/
GNU Lesser General Public License v3.0
53 stars 7 forks source link

UseStatements::splitImportUseStatement(): improve handling of leading backslash #590

Closed jrfnl closed 4 months ago

jrfnl commented 4 months ago

Import use statements should not be declared with a leading backslash and doing so is discouraged by PHP itself:

Note that for namespaced names (fully qualified namespace names containing namespace separator, such as Foo\Bar as opposed to global names that do not, such as FooBar), the leading backslash is unnecessary and not recommended, as import names must be fully qualified, and are not processed relative to the current namespace.

Ref: https://www.php.net/manual/en/language.namespaces.importing.php

However, it is not a parse error to declare an import with a leading backslash. Until now, the behaviour of this method in such a case was undefined, which, in practice, meant that the leading backslash would be included in the full name.

For consistency, however, this is undesirable and makes the names found in the return value more awkward to handle by sniffs using the method.

This commit now explicitly defines the behaviour around leading backslashes in import use statements and prevents these from being included in the names in the return value.

Includes unit tests.