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
F14029512: D14557.diff
Fri, Nov 8, 8:46 PM
F14029188: D14557.id35221.diff
Fri, Nov 8, 7:57 PM
F14029187: D14557.id35212.diff
Fri, Nov 8, 7:57 PM
F14029186: D14557.id.diff
Fri, Nov 8, 7:57 PM
F13983283: D14557.id35212.diff
Sun, Oct 20, 5:20 AM
F13961981: D14557.id.diff
Tue, Oct 15, 7:24 AM
Unknown Object (File)
Sep 25 2024, 1:20 PM
Unknown Object (File)
Sep 21 2024, 4:27 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.