-
-
Notifications
You must be signed in to change notification settings - Fork 863
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
More precise types in class constants #3530
Comments
So I looked into this and the root issue isn't about CONSTANT_SCALAR_UNION_THRESHOLD, but rather about how we're resolving class constant values: https://github.com/phpstan/phpstan-src/blob/cdf5b8518b9d04277a1489b8859d4a40ef068d26/src/Reflection/ClassConstantReflection.php#L60-L63 The problem is that we have a string value without any context whether it was created as Just to verify - when I hardcoded the second argument to To do this right, we'd have to inspect the AST of the class constant value and not just read the value from the reflection. Whici is possible with the static reflection engine - but it isn't currently deployed for classes that are loaded via autoloader, only for classes that aren't known to registered autoloaders (and are discovered via analysed paths and scanFiles/scanDirectories). So it'd work only in some situations. Another solution could be to ask for arbitrary strings, whether they are an existing class or not, and set isClassString based on that, but some users' registered autoloaders do not like being asked about nonexistent classes, so I want to avoid this (there were many issues submitted about that). I see two parts of this task:
|
@jbafford After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
-46: Parameter #1 $class of method HelloWorld::getRepository() expects class-string, string given.
+No errors |
|
Ref phpstan/phpstan#4793 Ref phpstan/phpstan#5062 Ref phpstan/phpstan#5447 Ref phpstan/phpstan#5454 Ref phpstan/phpstan#3366 Ref phpstan/phpstan#5072 Ref phpstan/phpstan#3530 Ref phpstan/phpstan#5530 Ref phpstan/phpstan#1861 Ref phpstan/phpstan#4843 Ref phpstan/phpstan#4602 Ref phpstan/phpstan#4499 Ref phpstan/phpstan#2142
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Bug report
An array of class names is not properly detected as
class-string
.Code snippet that reproduces the problem
https://phpstan.org/r/e84c46b3-64d2-4326-aa7a-7881f44b2c25
phpstan reports that a method that expects
class-string
is givenstring
.Expected output
phpstan should return no error via either of the two following mechanisms:
class-string<someinterface>
via an annotation, which phpstan ignores (class-string not enforced in constants #2798)class-string
, which phpstan can normally infer, but because it has 10 elements, which exceedsCONSTANT_SCALAR_UNION_THRESHOLD
, phpstan does not make the inference.While I would prefer this fixed via fixing #2798 (which would fix this general case entirely), if increasing
CONSTANT_SCALAR_UNION_THRESHOLD
is the solution you prefer, I should be able to make a PR that adds a configuration option to set it.The text was updated successfully, but these errors were encountered: