Home > Software engineering >  Undefined metod - intelephense on vscode
Undefined metod - intelephense on vscode

Time:04-11

I have the following error in VS code,

Undefined method 'verifyIfEmailIsUnique'.intelephense(1013)

I already tried to reinstall intelephense extension but it doesn't work.

I work with symfony 6

do you have any solution?

<?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à !';
            }
        }

CodePudding user response:

It makes sense that VSCode or Intelephense don't know about that method.

When you do:

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

you're expecting VSCode to know what class instance this will return

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

, while it can return different instances depending on what you pass in. VSCode doesn't run the code to see what it actually returns. It depends on return types defined in the code and docblocks.

If you want VSCode to be able to know what you get, then tell it what it is:

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

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