Skip to content
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

Type->getReferencedClasses() returns a class-string #2970

Closed
wants to merge 1 commit into from

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Mar 14, 2024

declaring the more precise types makes working in phpstan extension easier, as I can use more precise typing in e.g. method parameters to which I pass the result of getReferencedClasses() like

class myext {
  function doFoo() {
        foreach($classType->getReferencedClasses() as $referencedClass) {
            $this->myMethod($referencedClass);
        }
  }

  /** @param class-string $class */
  private function myMethod($class) {}
}

we could do similar stuff for Type->getObjectClassNames()

@@ -1303,6 +1303,11 @@ parameters:
count: 3
path: src/Type/ObjectType.php

-
message: "#^Method PHPStan\\\\Type\\\\ObjectType\\:\\:getReferencedClasses\\(\\) should return array\\<class\\-string\\> but returns array\\<int, string\\>\\.$#"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

baselined this error, as I would otherwise have to add a @param class-string to ObjectType->construct(), which would be a BC break

@ondrejmirtes
Copy link
Member

You can't rely on this method to return an array of class-string, precisely because of the error you baselined. That is never going away. TypeNodeResolver will happily create ObjectType for non-existent classes, and there are rules that iterate over getReferencedClasses and check if they exist or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants