Open b1rdex opened 4 years ago
This is not limited to relations. I am facing this issue with simple methods returning yii\db\ActiveQuery
, such as:
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 ActiveQuery
.
However, saving it to a variable is still a work-in-progress (because I do not know how to access the MyModel
part here):
$query = MyModel::findBySql("");
$query->all();
If anyone has any knowledge, there's an open question at phpstan.
I solved like this
/** @var null $query */
$query = Transfer::find()
/** @var Transfer $transfer */
$transfer = $query->one(); // @phpstan-ignore-line
@marmichalski Hey! Are you repeating this error? A simple example that shows the current problem and is very annoying:
<?php
namespace myNameSpace;
use yii\db\ActiveQuery;
class MyQuery extends ActiveQuery
{
public function test(array $ids): ActiveQuery
{
return $this->andWhere(['not in', 'id', $ids]);
}
}
------ ----------------------------------------------------------------------------------------------
Line MyQuery.php
------ ----------------------------------------------------------------------------------------------
Internal error: Unexpected type PHPStan\Type\ThisType during method call andWhere at line 19
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:
Internal error: Unexpected type PHPStan\Type\ThisType during method call andWhere...
Anybody have a solution?
I solved like this
/** @var null $query */ $query = Transfer::find() /** @var Transfer $transfer */ $transfer = $query->one(); // @phpstan-ignore-line
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.