Closed sirbrillig closed 6 years ago
Is this sniff from other coding standard or written from scratch?
I think this one should do the same: https://github.com/slevomat/coding-standard/blob/master/SlevomatCodingStandard/Sniffs/Namespaces/UnusedUsesSniff.php
Is this sniff from other coding standard or written from scratch?
It's written from scratch. I figured one would exist somewhere but I couldn't find it. Clearly you are better at looking than I am. 😁 Theirs looks much more... complete? But mine is much shorter! 😛 Looking through their version I'm trying to come up with a case where mine would fail, but I'm having trouble doing that. I'm inclined to keep my version unless I can come up with a failing test.
If I understand the difference correctly, this is the case where theirs sniff would detect it, but the shorter one would not detect it:
<?php
use First;
use Second;
new \Third\Second\First();
We have 4 kind of names in PHP:
C
,A\B\C
,namespace\C
,\A\B\C
.I would rename the rule as:
New code MUST NOT use fully-qualified names.
(with a note to remind the kind of names in PHP maybe?)
But the main point of my comment is: It's easier to detect fully-qualified names, than comparing aliases (use
) with names. See my comment https://github.com/Automattic/phpcs-neutron-standard/pull/3#discussion_r156016787.
New code MUST NOT use fully-qualified names.
Situations when I think fully qualified names are better (all fall into category of using a library/wp classes):
\Stripe\Charge::create
express much better that we are using the official library there and not our wrappernew \WP_Error
- I know for sure when I see it it is the global WP_Error
, I don't have to worry about if there is an import on topOh sorry, I guess I misinterprete the whole goal of the rule… I thought it was to forbidden fully-qualified names, but it's not :-p.
If we create a separate repo which is a configuration of different standards (see #13), we could just include the sniff that Jan found there, rather than trying to duplicate it here.
https://github.com/Automattic/phpcs-neutron-ruleset/issues/1 will take care of this.
Add a sniff that prevents
use
statements that import classes, functions, or constants which are not used.For example, if I have the line
use My\Classname;
butClassname
is not used anywhere in the file, this would trigger an error.This adds the rule:
New code MUST NOT import a class, function, or constant without using that import.