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)
Thu, Nov 21, 3:35 AM
Unknown Object (File)
Sun, Nov 17, 11:42 AM
Unknown Object (File)
Fri, Nov 8, 8:46 PM
Unknown Object (File)
Fri, Nov 8, 7:57 PM
Unknown Object (File)
Fri, Nov 8, 7:57 PM
Unknown Object (File)
Fri, Nov 8, 7:57 PM
Unknown Object (File)
Oct 20 2024, 5:20 AM
Unknown Object (File)
Oct 15 2024, 7:24 AM
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
Branch
master
Lint
Lint Passed
Unit
Test Failures
Build Status
Buildable 9024
Build 10603: Run Core Tests
Build 10602: arc lint + arc unit

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.