Skip to content

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

Open
@bluedasher13

Description

@bluedasher13
<?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

Activity

changed the title [-]Not working with type hints of "Union Types"[/-] [+]Not working with type hints of "Union Types" and "Intersection Types"[/+] on Feb 20, 2023
marabesi

marabesi commented on Feb 25, 2023

@marabesi
Owner

thanks for reporting @bluedasher13

marabesi

marabesi commented on Feb 25, 2023

@marabesi
Owner

@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

bluedasher13 commented on Feb 26, 2023

@bluedasher13
Author

@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

"php-parser": "^3.1.3"

https://github.com/nikic/PHP-Parser#php-parser

marabesi

marabesi commented on Feb 26, 2023

@marabesi
Owner

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: glayzzle/php-parser#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

bluedasher13 commented on Mar 1, 2023

@bluedasher13
Author

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

marabesi commented on Mar 4, 2023

@marabesi
Owner

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @marabesi@bluedasher13

        Issue actions

          Not working with type hints of "Union Types" and "Intersection Types" · Issue #481 · marabesi/php-import-checker