Page MenuHomePhabricator

Add a linter rule for unnecessary symbol aliases
ClosedPublic

Authored by joshuaspence on Nov 23 2015, 8:03 PM.
Tags
None
Referenced Files
Unknown Object (File)
Wed, Sep 4, 4:00 AM
Unknown Object (File)
Sat, Aug 31, 12:19 PM
Unknown Object (File)
Fri, Aug 30, 7:21 AM
Unknown Object (File)
Wed, Aug 28, 11:11 PM
Unknown Object (File)
Wed, Aug 28, 7:46 PM
Unknown Object (File)
Sun, Aug 25, 9:04 PM
Unknown Object (File)
Sun, Aug 25, 3:53 AM
Unknown Object (File)
Fri, Aug 23, 8:17 PM
Subscribers
Tokens
"Like" token, awarded by avivey.

Details

Summary

Add a linter rule to detect symbols which are aliased with the same name, such as use X\Y\Z as Z. This is unnecessary and is more-simply expressed as use X\Y\Z.

Test Plan

Added unit tests.

Diff Detail

Repository
rARC Arcanist
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

joshuaspence retitled this revision from to Add a linter rule for unnecessary symbol aliases.
joshuaspence updated this object.
joshuaspence edited the test plan for this revision. (Show Details)
joshuaspence added a reviewer: epriestley.
epriestley edited edge metadata.

Just to clarify, if I:

use A;
use B;

new X();

...where A\X and B\X are valid classes, what is PHP's behavior?

I know it's not an error (like "symbol X is ambiguous"), because that's never PHP's behavior.

And if I then add use B\X as X;, how does PHP's behavior change?

I know it changes, because it's PHP.

This revision is now accepted and ready to land.Nov 23 2015, 8:08 PM

(My knowledge of PHP is stuck in history at 5.2.3.)

If A and B are namespaces, then use A and use B actually do nothing at all. Actually, these statements will cause a warning to be emitted:

PHP Warning:  The use statement with non-compound name 'X' has no effect in /home/josh/workspace/github.com/phacility/libphutil/test.php on line 3
PHP Warning:  The use statement with non-compound name 'Y' has no effect in /home/josh/workspace/github.com/phacility/libphutil/test.php on line 4

StackOverflow has a good explanation, but generally I'd avoiding use to "import" a namespace.

A better example would be:

<?php

use A\X;
use B\X;

new X();

This will actually fail though:

PHP Fatal error:  Cannot use B\X as X because the name is already in use in /home/josh/workspace/github.com/phacility/libphutil/test.php on line 4

I'm impressed... PHP even recognises this as being invalid:

use X\Y;

class Y {}
This revision was automatically updated to reflect the committed changes.