marabesi / php-import-checker

php-import-checker helps you know when a given class is imported but not used, providing a easy way to keep your code clean and organized.
https://marketplace.visualstudio.com/items?itemName=marabesi.php-import-checker
MIT License
9 stars 5 forks source link

Not working with type hints of "Union Types" and "Intersection Types" #481

Open bluedasher13 opened 1 year ago

bluedasher13 commented 1 year ago
<?php

use App\Models\Bar1;
use App\Models\Bar2;
use App\Models\Bar3;

class Foo
{
    public function __construct(array|Bar1 $bar = null)
    {
    }
}

"Using type hints of "Union Types" or "Intersection Types" such as array|Bar1 or Bar1&Bar2 can cause the highlighting of the entire PHP file to malfunction."

image image image

marabesi commented 1 year ago

thanks for reporting @bluedasher13

marabesi commented 1 year ago

@bluedasher13 does it happens if you use the option next version?

Here goes an example of the setting:

"php.import.highlight": {
  "use_next_version": true
  "ignore_comments": true
}
bluedasher13 commented 1 year ago

@marabesi With no luck. It seems the problem still exists, even with the use_next_version and ignore_comments flags added.

Furthermore, I have also discovered that "named arguments" can also cause errors. Both "union types" and "named arguments" are new features of PHP 8.0, so perhaps the PHP Parser version is too old, as PHP Parser 3.1.3 seems to support up to PHP 7.2, while PHP Parser 4.x currently supports PHP 8.x.

<?php

namespace App\Services\Project;

use App\Models\Bar1;
use App\Models\Bar2;
use App\Models\Bar3;

class Foo
{
    public function __construct(?Bar2 $bar = null)
    {
        return json_encode(value: '', flags: 0);
    }
}

image image

https://github.com/marabesi/php-import-checker/blob/4e3669ad0fe3cdfac26bd3ac80523919483e2696/package.json#L58 https://github.com/nikic/PHP-Parser#php-parser

marabesi commented 1 year ago

Just a small hint on the php-parser version, this library is in typescript so there is no direct connection with the parser from nikita.

This is the correct library: https://github.com/glayzzle/php-parser/issues/486. The PHP version is still under development though.

From the samples you shared it seems that the library is working as expected (see the pipeline with the new test cases added here.

Nevertheless, the example you shared with json_encode seems to be working as expected as well, this is the version I have:

image

I found an issue when trying to combine the type of parameters though:

image

But this one will require an update from the library this project is using (I am not sure at the moment).

I am not sure if it is possible to have a union type of array and an optional Bar2. Is it possible?

For the time being what is exactly the outcome you expect from the examples you shared? For me here goes the output:

Example 1: should highlight 2 unused imports - this seems to break in your example Example 2: should highlight 2 unused imports - this is working without the json function

bluedasher13 commented 1 year ago

Thanks for the replies. Today, I updated the extension from v0.10.0 to v0.10.2, and some issues seem to have been resolved. The code mentioned above, it seems that everything is functioning properly.

However, there is still an (new?) issue where commented-out code is not being marked as unused imports. Additionally, the behavior of the ignore_comments option seems to be behaving strangely with both comments and PHPDoc.


Bar4 is commented-out PHPDoc. Bar5 and Bar6 are commented-out codes.

Scene 1: Bar4, Bar5, and Bar6 have been commented-out, but they are still not highlighted as unused imports. image

Scene 2: After adding use_next_version and ignore_comments flags, Bar4 is highlighted as unused, but Bar5 and Bar6 are still not highlighted. image

marabesi commented 1 year ago

@bluedasher13 can you paste there the code you are using instead of printing it? It helps me to adds those as test cases in the pipeline.