Skip to content

Diagnostics: iterable wrongly identified as "non-existing class" #1101

Closed
phpactor/worse-reflection
#72

Description

@dantleech

image

It is present in the phpstorm stubs, so should be identified here.

Activity

  1. camilledejoye commented on Oct 11, 2020

    @camilledejoye
    Contributor

    I think that's the reason we got the error, the stub consider it (or the use we made of the stubs) as a class.
    I remember that at some point phpactor would add some use iterable; when implementing contracts and stuff like that, but I can't reproduce so that must have been fixed since.

    Do you know what tool generate this error ?

  2. dantleech commented on Oct 11, 2020

    @dantleech
    CollaboratorAuthor

    Phpactor generates this error. The reason is because it's searching for the name using the index, but the stubs are not indexed.

  3. camilledejoye commented on Oct 17, 2020

    @camilledejoye
    Contributor

    What about excluding this case like it's done here: https://github.com/phpactor/code-transform/blob/41071cee6da50937e68e8a6391d03719e2e649c7/lib/Adapter/WorseReflection/Helper/WorseUnresolvableClassNameFinder.php#L83
    Something like:

    if ('iterable' === (string) $name) {
        return $unresolvedNames
    }

    Since iterable is a reserved keyword I think it's OK to handle it this way.

    If it's OK for you I will do the PR, it's a really quick fix and adding a test is just as simple.

  4. dantleech commented on Oct 17, 2020

    @dantleech
    CollaboratorAuthor

    iterable is part of the Jetbrains PHPStorm stubs. The problem is that these are not currently indexed and the diagnostics use the indexer exclusively (it would be too slow otherwise).

    So the solution is to ensure that the stubs are indexed.

    Apparently the option to configure stubs this has been implemented here.

    So it might be as simple as including vendor/jetbrains/phpstorm-stubs directory by default here

  5. camilledejoye commented on Oct 17, 2020

    @camilledejoye
    Contributor

    Does it make sense to put the stubs in the index though ?
    Would it not make more sense to query the index, as it is done currently, and if not found in the index then check the stub ?
    Or aggregate the result of the index with the content of the stubs in some way.

    No matter all that, I try your idea and it didn't worked.
    I defined the parameter with %applicaiton_root%/vendor/jetbrains/phpstorm-strubs and rebuild my index.
    I still got the error, then I tried to take a quick look at how the parameter was used and it seems like it will only be used to define glob patterns that must be match in order for a file to be returned by the FilesystemFileListProvider.
    But the Filesystem created for the index, take a SimpleFileListeProvider which only provide the files from the project root.

    So I'm not sure if I didn't understood how it works or if the PARAM_STUB_PATHS is not used properly...

  6. camilledejoye commented on Nov 1, 2020

    @camilledejoye
    Contributor

    There is some progress :D
    image

    iterable is resolved to being in the current namespace, so when we arrive to the IndexerClassSourceLocator or no matter which one actually it fails.
    So either we consider the pseudo type iterable like a specific case because it's actually not a QualifiedName and we treat it like self and others.
    Or we have to change the TolerantQualifiedNameResolver (if another PR is validated it will be used) to not resolve iterable to the current namespace.
    Or even both, the check is done in both the name resolver and the filter method.

    I also need to investigate to check why we don't have the same issue with object...

  7. dantleech commented on Nov 1, 2020

    @dantleech
    CollaboratorAuthor

    "Object" is specialed case in WorseReflection, probably enough to update WR to include iterable as a reserved name too.

  8. camilledejoye commented on Nov 1, 2020

    @camilledejoye
    Contributor

    object is not considered a QualifiedName by the parser if I understood correctly what was going on.
    But I don't see what you mean by updating WR, for me this is what is happening (ignoring functions and constant cases):

    1. We use Toleran Parser (TP) to retrieve all nodes of type QualifiedName
    2. We resolve the name with TP
    3. If the resolved name is static, self or parent we consider it OK
    4. If the name was unresolved by TP it's resolved as being in the current namespace
    5. We check that the resolved name is found with the Reflector (StubSourceLocator, IndexerClassSourceLocator, etc.)

    So the only time WR is involved is at stage 5. which is already too late because TP would have resolved the name to Current\Namespace\iterable at stage 1.

    Therfore what needs to be fixed is the name resolution, we could use the TolerantQualifiedNameResolver and update it to resolve iterable to iterable or even \iterable.
    That would fix the issue and I think it our best option since it means that we would have a "correct" name resolution anywhere for iterable.

    But I still don't get why TR consider a pseudo-type as a qualified name ><

  9. dantleech commented on Nov 2, 2020

    @dantleech
    CollaboratorAuthor

    Probably worth submitting a PR to TolerantParser, in the meantime one way forward might be to add a test on WR and fix it there.

  10. camilledejoye commented on Nov 2, 2020

    @camilledejoye
    Contributor

    I started to work on the TolerantParser, the changes are quite easy, but the implications of returning a token instead of a node might slow things down.
    There already was an issue about that, since 2017...
    microsoft/tolerant-php-parser#348

  11. camilledejoye commented on Nov 7, 2020

    @camilledejoye
    Contributor

    in the meantime one way forward might be to add a test on WR and fix it there.

    How do you see that exactly ?
    You want to ignore the namespace if it ends by iterable in the StubSourceLocator so that it would recognize it ?
    This would be valid right now but in PHP 8 we will be able to use reserved keyword in the namespaces so it won't work.

    I will prepare a PR with a fix in the TolerantQualifiedNameResolver because it makes more sens to me and that would allow me to not have those errors locally.
    I will update the PR once I understand how you want to solve this in WR.

    I just realized that the TolerantQualifiedNameResolver is in the WR package ><
    I guess that explains why I didn't get why you were talking about fixing it WR 😄

  12. dantleech commented on Nov 7, 2020

    @dantleech
    CollaboratorAuthor
  13. camilledejoye commented on Nov 7, 2020

    @camilledejoye
    Contributor

    In the PR I did that but in the TolerantQualifiedNameResolver, this way we will benefit from it in multiple places.

  14. dantleech commented on Nov 7, 2020

    @dantleech
    CollaboratorAuthor

    ok, makes sense I think 👍

  15. added a commit that references this issue on Nov 15, 2020
  16. added a commit that references this issue on Feb 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions