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
PHPStan: Fixed handling of union types in ConfigReturnTypeExtension #11312
Conversation
// for compat with old phpstan versions, we use a deprecated phpstan method. | ||
$strings = TypeUtils::getConstantStrings($keyType); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refactored to use the deprecated TypeUtils::getConstantStrings
method, since composer itself does not specify a min-phpstan version requirement.
using the suggested Type->getConstantStrings()
variant is available as of PHPStan 1.9.4
0c74446
to
edab4cc
Compare
if ($keyType instanceof ConstantStringType) { | ||
if (isset($this->properties[$keyType->getValue()])) { | ||
return $this->properties[$keyType->getValue()]; | ||
// for compat with old phpstan versions, we use a deprecated phpstan method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this should do a version check or a method_exists
check to use the new API when available.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, fixed
if ($strings !== []) { | ||
$types = []; | ||
foreach($strings as $string) { | ||
if (isset($this->properties[$string->getValue()])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any string not being known here should force skipping the more precise type, falling back to the basic method signature
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adressed
@@ -64,18 +65,32 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method | |||
{ | |||
$args = $methodCall->getArgs(); | |||
|
|||
$defaultReturn = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in case we would have a min phpstan version of 1.5.0 or higher, the method could just return null and the phpstan-core would take care of determining the default-return type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to assume latest PHPStan and bump the require if you like :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this won't work. it depends on the phpstan version used by the project which depends on composer.
the extension is meant for re-use in plugin projects. this means I would either need to declare a non-dev dependency on phpstan or add a conflict. both things I don't like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right I forgot that aspect, let's leave as is then 👍🏻
Thanks |
see https://phpstan.org/blog/why-is-instanceof-type-wrong-and-getting-deprecated
after this fix the PHPStan extension supports cases like
which it did not before