-
Notifications
You must be signed in to change notification settings - Fork 662
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
ReflectionParameter::hasType() should assert return type of ReflectionParameter::getType() #5258
Comments
I found these snippets: https://psalm.dev/r/257f4e884d<?php
static function () : void {
$method = new ReflectionMethod(stdClass::class);
$parameters = $method->getParameters();
foreach ($parameters as $parameter) {
if (!$parameter->hasType()) {
throw new RuntimeException('No type found.');
}
echo($parameter->getType()->__toString());
}
};
|
@weirdan If you have some pointers for where to look.. I can attempt a PR myself ;) |
Well, I don't think of any other way other than creating a new ReturnTypeProvider for this. The complexity here is that the return type depends on the result of another method rather than the parameters of the method. I looked in Psalm and in Psalm's plugin and it doesn't appear to have already been done anywhere. However, I think we could exploit method memoization setting (MethodCallAnalyzer line 204) that store the result of previous methods in the context. By retrieving $event->getContext()->vars_in_scope in the ReturnTypeProvider, you should be able to know the type returned by hasType when calling getType. As I said, I don't think it has ever been done and it's exploiting a not totally relevant setting and feature to work. I would have no remorse doing that in one of my plugins, but I'm not sure it's good enough to be added in core. Maybe instead of using memoization setting, Psalm could have a list of methods that should be memoized anyway for this kind of feature |
Is this not do-able with a For example: class ReflectionParameter {
/*
* @var ReflectionType|null
*/
private $type;
/*
* @psalm-assert-if-true !null $type
*/
public function hasType() : bool
{
return $this->type !== null;
}
public function getType() : ?ReflectionType
{
return $this->type;
}
} |
Nope, Psalm currently has no knowledge of the internal state of an object (except for it's type, and potential templates EDIT: also except array-like objects). That means you can't convey datas between calls on the same object. (it has been suggested and refused before) That's why I suggested using data from the context that has called the methods. |
Interesting. I will take a look later and see if I can produce a result. Thank you for your input @orklah |
Doesn't Psalm already do this for mutation-free methods? Actually |
I don't have much practice with purity in Psalm, but it looks right. There's some code that looks like that in MethodCallPurityAnalyzer line 144 |
It turns out Psalm already had pretty much everything necessary – the only blocker was type-insensitivity of assertions on |
If
ReflectionParameter::hasType()
returnstrue
, thenReflectionParameter::getType()
will always returnReflectionType
.If
ReflectionParameter::hasType()
returnsfalse
, thenReflectionParameter::getType()
will always returnnull
.This is not currently understood by psalm.
https://psalm.dev/r/257f4e884d
The text was updated successfully, but these errors were encountered: