Search code examples
symfonydoctrinephpstorm

Symfony User entity (PhpStorm warning)


I have Symfony entity with auto-generated setUser() method:

    public function setUser(?User $user): self
    {
        $this->user = $user;

        return $this;
    }

In controller I use built-in getUser() method, which returns UserInterface object. And when I pass that UserInterface object to setUser() method, PhpStorm complains that:

Expected parameter of type '\App\Entity\User', 'object|\Symfony\Component\Security\Core\User\UserInterface' provided

I'd like to write code without such PhpStorm warnings. Should I create new User() to pass it to setUser() method or just ignore that?


Solution

  • Solution: (thanks to Jakumi comment)

    public function __invoke() {
        /** @var $user App\Entity\User */
        $user = $this->getUser();
        
        $entity = new Entity();
        $entity->setUser($user);
        
        //...
    }