nextras / orm-intellij

Nextras ORM Plugin for PhpStorm
https://plugins.jetbrains.com/plugin/7912-nextras-orm
MIT License
13 stars 2 forks source link

IModel->getRepository() support #18

Closed mabar closed 3 years ago

mabar commented 3 years ago

Would it please be possible to add support for $userRepository = $model->getRepository(UserRepository::class);? Currently it's just IRepository for phpstorm.

I can't add annotations to an extended Model class and find myself quite often in situation with 3-6 repositories where replacing them with model would reduce boilerplate significantly.

Thank you for consideration :)

mabar commented 3 years ago

I tried to solve it via .phpstorm.meta.php which resolves warning when assigning to private UserRepository $userRepository;, but phpstorm still thinks it is UserRepository|IRepository and acts like with IRepository

<?php declare(strict_types = 1);

namespace PHPSTORM_META;

override(
    \Nextras\Orm\Model\IModel::getRepository(0),
    type(0)
);
hrach commented 3 years ago

What does it mean to "act slike with IRepository"? It works for me. Also try please

override(\Nextras\Orm\Model\IModel::getRepository(0), map(['' => '@']));

which may be more correct.

mabar commented 3 years ago

What does it mean to "act slike with IRepository"?

It's the type phpstorm shows when I am inspecting property type via ctrl and mouse pointer over $this->userRepository

Check the following example - in comments are types which phpstorm thinks variables have

use Nextras\Orm\Model\IModel;
use OriCMF\Core\User\UserRepository;

final class Test
{

    private IModel $model;

    private UserRepository $userRepository;

    public function __construct(IModel $model)
    {
        $this->model = $model;
        $this->userRepository = $model->getRepository(UserRepository::class);
    }

    public function test(): void
    {
        // $this->userRepository is IRepository|UserRepository
        // $user1 is IEntity
        $user1 = $this->userRepository->getByIdChecked(1);

        // $collection is ICollection
        $collection = $this->userRepository->findAll();
        // $user is mixed
        foreach ($collection as $user) {

        }
    }

}

Your syntax has the same problem. I think it should even be equal, mine is just syntax sugar from newer phpstorm release.

mabar commented 3 years ago

phpstorm meta just helped me to get rid of warning Incompatible types: Expected property of type '\OriCMF\Core\User\UserRepository', '\Nextras\Orm\Repository\IRepository' provided and got the repository-specific methods to work, but calling any findBy* and getBy* methods result only into IEntity (even to mixed in ICollection) and also the conditions autocomplete in findBy() and getBy() does not work. I guess the plugin does not play well with the union type? But I understand it's phpstorm error, because the IRepository type in property with UserRepository type is impossible.

hrach commented 3 years ago

Thanks for detailed description, the failing resolution over class property was plugin's bug.

hrach commented 3 years ago

Released as 0.8.0. Either wait for the review from JB, or use the zip in the release: https://github.com/nextras/orm-intellij/releases/tag/v0.8.0

mabar commented 3 years ago

Thanks, it works as expected, in combination with .phpstorm.meta.php

mabar commented 3 years ago

I just found that one minor issue is still left, assignment of repository to variable is needed.

With variable assignment, $user is User

$userRepository = $this->model->getRepository(UserRepository::class);
$user = $userRepository->getByIdChecked(1);
assert($user instanceof User);

Without variable assignment, $user is IEntity

$user = $this->model->getRepository(UserRepository::class)->getByIdChecked(1);
assert($user instanceof IEntity);
hrach commented 3 years ago

Create new issue please.

hrach commented 3 years ago

Phpstorm meta added to repo: https://github.com/nextras/orm/commit/f0d67ae46b7ff9868382d5046fe2d62946e0ca8d