未定义的方法 - vscode 上的 intelephense

Undefined metod - intelephense on vscode

我在 VS 代码中出现以下错误,

Undefined method 'verifyIfEmailIsUnique'.intelephense(1013)

我已经尝试重新安装 intelephense 扩展,但它不起作用。

我使用 symfony 6

你有什么解决办法吗?

<?php

namespace App\Controller;

use App\Entity\User;
use App\Form\RegisterType;
use Doctrine\ORM\EntityManagerInterface;
use Proxies\__CG__\App\Entity\User as EntityUser;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;

class RegisterController extends AbstractController
{
    private EntityManagerInterface $entityManager;

    /**
     * @param EntityManagerInterface $entityManager
     */
    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    #[Route('/register', name: 'app_register')]
    public function index(UserPasswordHasherInterface $passwordHasher, Request $request): Response
    {
        if ($this->getUser()) {
            return $this->redirectToRoute('app_home');
        }
        
        $user = new User();
        $error = "";
        $form = $this->createForm(RegisterType::class, $user);

        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $user = $form->getData();
            if ($this->entityManager->getRepository(User::class)->verifyIfEmailIsUnique($user->getEmail())) {
                $user->setPassword($passwordHasher->hashPassword($user, $user->getPassword()));
                $this->entityManager->persist($user);
                $this->entityManager->flush();
                return $this->redirectToRoute('app_login');
            } else {
                $error = 'L\'adresse mail '. $user->getEmail() . ' existe déjà !';
            }
        }

VSCode 或 Intelephense 不知道该方法是有道理的。

当你这样做时:

$this->entityManager->getRepository(User::class)->verifyIfEmailIsUnique(...)

您希望 VSCode 知道 class 这将 return

$this->entityManager->getRepository(User::class)

,虽然它可以 return 不同的实例,具体取决于您传入的内容。VSCode 没有 运行 代码来查看它 实际上 returns。这取决于代码和文档块中定义的 return 类型。

如果你想让VSCode能够知道你得到了什么,那就告诉它是什么:

/**
 * @var User
 */
$userEntity = $this->entityManager->getRepository(User::class);

if ($userEntity->verifyIfEmailIsUnique($user->getEmail())) {
    ...