andy_s
(Arekandrei)
February 25, 2014, 9:11am
1
I’m using the latest version of PhpStorm on Windows 8.1, but for some reason it doesn’t recognize the return type of the ActiveRecord’s find() method:
Sample code from the guide: https://github.com/yiisoft/yii2/blob/master/docs/guide/controller.md
class BlogController extends Controller
{
public function actionUpdate($id)
{
$post = Post::find($id);
if (!$post) {
throw new NotFoundHttpException;
}
if (\Yii::$app->request->isPost) {
$post->load(Yii::$app->request->post());
if ($post->save()) {
return $this->redirect(['view', 'id' => $post->id]);
}
}
return $this->render('update', ['post' => $post]);
}
}
Yii2 BaseActiveRecord.php line 105:
* @return ActiveQuery|static|null
If I ctrl+click on $post->load, then I got an error "Can not find declaration to go to".
But if I change the return type to just "static", then I can navigate to the load() method and other Post class methods.
I wonder if I’m the only one who has this issue. Currently I don’t have an opportunity to test it on another machines.
samdark
(Alexander Makarov)
February 25, 2014, 10:49am
2
PhpStorm 7.1.3 build 133.925. Windows 8.1. master of Yii2.
samdark
(Alexander Makarov)
February 25, 2014, 10:50am
3
Autocomplete for your case doens’t work since it can return both query and model instance and it seems IDE prefers query.
andy_s
(Arekandrei)
February 25, 2014, 10:56am
4
It works in case of ::find()->one(), but not for ::find(pk) or ::find(pk)->one(). The last one just throws an error. I really wouldn’t like to return to /** @var User $user **/ comments
samdark
(Alexander Makarov)
February 25, 2014, 11:03am
5
Well, I see no way to make it possible without splitting find() and find(args) into two separate methods.
andy_s
(Arekandrei)
February 25, 2014, 11:29am
6
I guess it’s too complex task for IDE to know the returned type by looking at method arguments. Good old findByPk() would come handy there
samdark
(Alexander Makarov)
February 25, 2014, 2:42pm
7
Well, it’s still not too late to create an issue at github and adjust API a bit
andy_s
(Arekandrei)
February 25, 2014, 4:29pm
8
Unfortunately my issue was closed within 5 minutes
Do Yii developers prefer notepad++? Or it’s only the PhpStorm issue?
samdark
(Alexander Makarov)
February 25, 2014, 6:58pm
9
It probably can be solved by PhpStorm team. It’s not possible to solve it at code level w/o method separation that is unwanted by many users.
andy_s
(Arekandrei)
February 25, 2014, 7:01pm
10
I hope so, because even the older version of Yii with it’s model() method works better when it comes to typehinting