Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Undefined metod – intelephense on vscode

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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())) {
    ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading