-
Notifications
You must be signed in to change notification settings - Fork 21
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
Problem with relations #25
Comments
This is not limited to relations. I am facing this issue with simple methods returning public static function findBySomething(string $something): \yii\db\ActiveQuery
{
return self::find()
->where(['something' => $something]);
} |
I've currently got a working version for MyModel::findBySql("")->all(); and anything that returns an However, saving it to a variable is still a work-in-progress (because I do not know how to access the $query = MyModel::findBySql("");
$query->all(); If anyone has any knowledge, there's an open question at phpstan. Modified code - click to expandpublic function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
$methodName = $methodReflection->getName();
$calledOnType = $scope->getType($methodCall->var);
if (!$calledOnType instanceof ActiveQueryObjectType) {
if (!($calledOnType instanceof ObjectType) || $calledOnType->getClassName() !== ActiveQuery::class) {
throw new ShouldNotHappenException(sprintf('Unexpected type %s during method call %s at line %d', \get_class($calledOnType), $methodReflection->getName(), $methodCall->getLine()));
}
$var = $methodCall->var;
if ($var instanceof StaticCall) {
/**
* @example
* MyModel::findBySql("")->all();
*/
$calledOnType = new ActiveQueryObjectType($var->class->toString(), $methodName !== 'one');
} else if ($var instanceof Variable) {
/**
* @example :
* $query = MyModel::findBySql("");
* $query->all();
*/
// TODO $calledOnType = new ActiveQueryObjectType('??????????', $methodName === 'one');
} else {
throw new ShouldNotHappenException(sprintf('Unable to find ActiveRecord type for %s during method call %s at line %d', \get_class($calledOnType), $methodReflection->getName(), $methodCall->getLine()));
}
}
if ($methodName === 'asArray') {
$argType = isset($methodCall->args[0]) ? $scope->getType($methodCall->args[0]->value) : new ConstantBooleanType(true);
if (!$argType instanceof ConstantBooleanType) {
throw new ShouldNotHappenException(sprintf('Invalid argument provided to asArray method at line %d', $methodCall->getLine()));
}
return new ActiveQueryObjectType($calledOnType->getModelClass(), $argType->getValue());
}
if (!\in_array($methodName, ['one', 'all'], true)) {
return new ActiveQueryObjectType($calledOnType->getModelClass(), $calledOnType->isAsArray());
}
if ($methodName === 'one') {
return TypeCombinator::union(
new NullType(),
$calledOnType->isAsArray() ? new ArrayType(new StringType(), new MixedType()) : new ObjectType($calledOnType->getModelClass())
);
}
return new ArrayType(
new IntegerType(),
$calledOnType->isAsArray() ? new ArrayType(new StringType(), new MixedType()) : new ObjectType($calledOnType->getModelClass())
);
} |
I solved like this
|
i fix this #48 |
@marmichalski Hey! Are you repeating this error? A simple example that shows the current problem and is very annoying:
I tried changing the validation myself, but my current knowledge is a bit lacking. I will try to figure it out further, but maybe you have a solution to this problem? |
I am trying this out and also getting this error a lot:
Anybody have a solution? |
No, you did not solve, you put the issue under the carpet. |
Code in the controller:
Model:
getPaymentTransfer()
method return type provided in PhpDoc or using native return type set toActiveQuery
leads to extension exception:Internal error: Unexpected type PHPStan\Type\ObjectType during method call all at line 142
(line 142 is$transfer->getPaymentTransfer()->all()
call). That error comes from\Proget\PHPStan\Yii2\Type\ActiveQueryDynamicMethodReturnTypeExtension::getTypeFromMethodCall
. Here is what's in the$calledOnType
:If I remove return type from
getPaymentTransfer()
everything works fine. But that looks odd and that's easy to break if someone adds return type.The text was updated successfully, but these errors were encountered: