PHPCSStandards / PHPCSExtra

A collection of code standards for use with PHP_CodeSniffer
GNU Lesser General Public License v3.0
91 stars 8 forks source link

Sniff to detect useless alias names #233

Closed jrfnl closed 1 year ago

jrfnl commented 1 year ago

Is your feature request related to a problem?

It would be nice to have a sniff which would detect and flag when an alias is the same as the original name.

PHP will allow this just fine - https://3v4l.org/DIT7X#veol / https://3v4l.org/Gd4M7#veol - , but it is useless to do so.

Describe the solution you'd like

A new sniff to detect useless aliases.

// OK.
use Vendor\Package\ClassName as OtherClassName;
use function Vendor\Package\functionName as otherFunctionName;
use const Vendor\Package\CONSTANT_NAME as OTHER_CONSTANT_NAME;

// Not OK.
use Vendor\Package\ClassName as ClassName;
use function Vendor\Package\functionName as functionName;
use const Vendor\Package\CONSTANT_NAME as CONSTANT_NAME;

// Should also be checked for trait `use` statements
class Aliased_Talker {
    use A {
        A::bigTalk as talk; // OK
        A::smallTalk as smallTalk; // Not OK.
    }
}

Additional context (optional)

Aliasing to the same name, but in a different case should also be flagged, though maybe with a separate error code.

Namespace, OO and function names in PHP are case-insensitive, so aliasing to the same name in a different case is just as useless.

Note: constants are generally case-sensitive, except when explicitly declared as case-insensitive using define(). Probably still a good idea to flag that even so.

jrfnl commented 1 year ago

Note: PR #244 does not address useless aliases for trait use statements yet. This can be addressed at a later point in time.