-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug: False positive on used imports when docblock includes it with mismatching case #6909
Conversation
In my opinion import usage should be limited to actual types or phpDocs (signature related stuff), annotations etc. Words used in description, params' comments etc. shouldn't be considered as "import usage". Looking at your example, I wouldn't consider |
Oh, for sure, I don't consider any plain-text as a reference to a class either. I've just retained the case-sensitive portion as:
Happy to adjust this PR to remove comments matching class names entirely (i.e. even case-sensitive) if that's the preferred approach. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe re-working NoUnusedImportsFixer
the way we discussed is too hard to continue under bug PR, and I am not even sure if we can consider current approach as a bug (rather improper feature?). I really would like to introduce better way of cleaning imports, but probably we just can't change it just like that and we need hide new approach behind opt-in configuration flag, which should be done in separate PR.
On the other hand, changed case-sensitivity like it was done here IMHO can be considered as bug fix, because I am sure words with different casing shouldn't be treated as class "usage" (even if PHP itself is case-insensitive in that regard).
Anyway, I won't merge it, I would like to hear opinion from @kubawerlos, @keradus 🙂.
Thank you @bradietilley 🍻 |
@mvorisek can you provide examples why it's risky? I tried to get familiar with the issue and previous discussion when I did a review and at that point I didn't see the potential problem. I don't have time to re-read it again. I believe we can just observe how it goes 🙂. |
repro https://3v4l.org/DCEmg - the |
Making it risky will be a BC break if people have the rule in their configuration together with the option |
Yes, revert now, release 3.18.1 and reintroduce the CS variant as another fixer (as one fixer cannot be risky only sometimes). |
Decision (and the potential patch release) is up to @keradus 🙂. |
Hey folks, please avoid setting me under the wall like this. |
@keradus you said the other day you test everything before release, so I believe you're familiar enough with the change 😉. All that needs to be decided here is if we should consider usage described above as something we want to support (keep it safe). Merged PR solves other scenarios, so it is valuable for other people. Those with such ancient code that uses If it was for me, I would keep this fixer as-is (in v3.18), and observe the real impact, not theoretical. |
The problem is with any code, not just |
In contrary, I believe this change can help people spot problems with wrong casing in the imports/usage and make their code better, while fixing the bug for other people (keeping unneeded imports because of case-insensitive match). That's why I would keep it as-is and would like to hear @keradus opinion about that. |
@Wirone definitely, I am for it, but I am saying it MUST be marked as risky (or introduced as another fixer marked as risky). |
… with mismatching case (PHP-CS-Fixer#6909)" This reverts commit 35acd14.
Partially addressing #2814.
I see this issue crop up about once a week if not more.
There are two tests in
PHP-CS-Fixer
, one that asserted case-sensitive matching in docblocks, and one that asserted case-insensitive matching in docblock. The result from both is that the imports are retained, albeit unused.I understand the correct capitalisation of a class name should be treated as a direct reference to the class, and thus it is used. However, the incorrect capitalisation of a class name should not be treated as a direct reference to the class, and thus it should be removed as an import.
Frequency
Single-word class names are extremely common in PHP. For example
Illuminate\Support\Collection
,Illuminate\Http\Request
,Illuminate\Http\Response
,Illuminate\Support\Facades\File
,App\Models\User
,App\Models\Role
,App\Models\Product
,App\Models\Invoice
.Mentioning these single-word class names in their abstract sense will be extremely common if you're planning on (or are) using the class in your code. For example, "do something if the invoice has no product attached" references
Invoice
andProduct
as of currently.If you never end up using the class(es) that you imported, or you end up refactoring the code and remove those classes but retain lowercase reference to the class(es) in docblock comments, then you'll be faced with this issue.
So it's quite easy: First you add a single-word class name, then talk about the abstract idea, then remove (or never add the class to your code) then run php-cs-fixer.
Example Issue + Example Fix
Before this PR, the above would have no change applied. The
User
import would remain imported asuser
is found in the doblock.After this PR, the below would occur:
<?php - use App\Models\User; use App\Models\Role; class Something { /** * Do something as the authorised user depending on their role * * See Role for more info */ public function doSomething(): void { // } }
Note: correct capitalisation of
Role
which is likely to be a reference to theRole
class is retained.Bug or feature?
I don't know about the thousands of other developers who use php-cs-fixer, but I would expect the import to be removed as it is in no way used in the code. If you want it retained, use the correct capitalisation.
Impact
Low impact as this only affects files where the imported class is ONLY referenced using incorrect capitalisation. So, the class is not used in any code, for any type hints, and is only referenced in a capitalisation that really doesn't correlate to the class at all.