vscode-intelephense icon indicating copy to clipboard operation
vscode-intelephense copied to clipboard

Undefined method when method is defined

Open StevenCarre opened this issue 3 years ago • 13 comments

Describe the bug trying to access method 'getCohortes' of class User, it is highlighted for undefined method. However, the method exists.

To Reproduce

[...]
class User implements UserInterface, \Serializable, EquatableInterface
[...]
    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Provenance", inversedBy="users")
     * @MaxDepth(1)
     */
    private $cohortes;
[...]
    /**
     * @return Collection|Provenance[]
     */
    public function getCohortes(): Collection
    {
        $listeCohortes = $this->cohortes ;
        $listeId = array() ;
        foreach($this->cohortes as $cohorte) {
            $listeId[] = $cohorte->getId() ;
        }
        if ($this->missions) {
            foreach($this->missions as $mission) {
                if ($mission->getCohortes()) {
                    foreach($mission->getCohortes() as $cohorte) {
                        if (!in_array($cohorte->getId(), $listeId)) {$listeCohortes[] = $cohorte ;}
                    }
                }
            }
        }
        //array_unique($listeCohortes) ;
        return $listeCohortes ;
    }

    public function addCohorte(Provenance $cohorte): self
    {
        if (!$this->cohortes->contains($cohorte)) {
            $this->cohortes[] = $cohorte;
        }

        return $this;
    }
[...]

// in a Form class (AccompagnementType.php):
foreach ($user->getCohortes() as $mission)

Expected behavior getCohortes get highlighted Undefined method 'getCohortes'.intelephense(1013)

Screenshots If applicable, add screenshots to help explain your problem.

Platform and version OS and Intelephense version. Ubuntu 20.04.2 LTS Intelephense v1.7.1

StevenCarre avatar May 17 '21 08:05 StevenCarre

I think this is a duplicate of #1776

bmewburn avatar May 17 '21 09:05 bmewburn

I think this is a duplicate of #1776

I fail to see the connection These look like two different issues

StevenCarre avatar May 17 '21 17:05 StevenCarre

I saw Collection|Provenance[] and assumed it was the same. Your example does not show how $user is instantiated. Not enough information is provided to understand the problem.

bmewburn avatar May 17 '21 21:05 bmewburn

Thanks for reopening the issue Best regards

Steven Carré

Le 17 mai 2021 à 23:09, Ben Mewburn @.***> a écrit :

 I saw Collection|Provenance[] and assumed it was the same. Your example does not show how $user is instantiated. Not enough information is provided to understand the problem.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

StevenCarre avatar May 18 '21 05:05 StevenCarre

Hi,

I get more and more 'undefined method' notices where there is no reason! here's a blatant example:

$entityManager = $this->getDoctrine()->getManager(); $amont = $entityManager->getRepository(Amont::class) ->find($idAmont);

    $emile = $entityManager->getRepository(Emile::class)
                ->find($idEmile);

    $queryBuilder = $entityManager->createQueryBuilder()
            ->select('a')
            ->from(Amont::class, 'a')
            ->where('a.emile = :emile')
            ->setParameter('emile', $emile)
            ->orderBy('a.dateInscription', 'ASC')
            ;

how is createQueryBuilder() not a method of EntityManagerInterface?

Best regards, SC

StevenCarre avatar Jun 01 '21 15:06 StevenCarre

OK, what infos do you need?

StevenCarre avatar Jun 03 '21 20:06 StevenCarre

A reproducible example. You are just copying some bit of code from your project which uses some framework (symfony?) and expecting an answer.

Your first example doesn't show how $user is created. What type is $user? When you hover over it is it the type you expect? Does this type declare the function you are calling? Same for your second example. Are you sure $entityManager is of type EntityManagerInterface?

bmewburn avatar Jun 04 '21 00:06 bmewburn

Hi Ok thanks for your reply. I will send you more info this weekend or monday Best regards

Steven Carré

Le 4 juin 2021 à 02:54, Ben Mewburn @.***> a écrit :

 A reproducible example. You are just copying some bit of code from your project which uses some framework (symfony?) and expecting an answer.

Your first example doesn't show how $user is created. What type is $user? When you hover over it is it the type you expect? Does this type declare the function you are calling? Same for your second example. Are you sure $entityManager is EntityManagerInterface?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

StevenCarre avatar Jun 04 '21 17:06 StevenCarre

Hi, here is another example. $entityManager is indeed of EntityManagerInterfce type, as you can see. getRepository is emphasized as undefined method.

use Doctrine\ORM\EntityManagerInterface; class ImportController extends AbstractController

/**
 * @Route("/import-orcod/{accelerate}", name="import_orcod")
 */
public function importOrcod(StringTool $stringTool, HttpClientInterface $client, EntityManagerInterface $entityManager, bool $accelerate = false)
{
    if (!$this->getUser()) {
        return $this->redirectToRoute('app_login');
    }
    if (!$this->isGranted('ROLE_SUPER_ADMIN'))
    {
        throw $this->createAccessDeniedException();
    }
   ...
    $missionOrcod = $entityManager->getRepository(Mission::class)->findOneBy(["url" => 'orcodin-clichy']);
    if(!$missionOrcod) {
        $missionOrcod = new Mission;
        $missionOrcod->setNom("ORCODIN Clichy-sous-bois");
        $missionOrcod->setUrl("orcodin-clichy");
    }
   ...

}

Best regards,

Steven Carré

StevenCarre avatar Jun 16 '21 07:06 StevenCarre

I've seen this due to inheritance (Note return type of Car::setColour):

class Car {
    protected string $colour;

    public function __construct(string $colour) {
        $this->colour = $colour;
    }

    public function setColour(string $colour): self {
        $this->colour = $colour;

        return $this;
    }

    public function getColour(): string
    {
        return $this->colour;
    }
}

class RegisteredCar extends Car {
    protected string $registrationNumber;

    public function __construct(string $colour, string $registrationNumber) {
        parent::__construct($colour);

        $this->registrationNumber = $registrationNumber;
    }

    public function setRegistrationNumber(string $registrationNumber): self {
        $this->registrationNumber = $registrationNumber;

        return $this;
    }

    public function getRegistrationNumber(): string
    {
        return $this->registrationNumber;
    }
}

$registeredCar = new RegisteredCar('blue', 'CAR123');
// re-spray and re-register with personalized plate
$registeredCar
    ->setColour('red')
    ->setRegistrationNumber('PERSONALIZED') // Undefined method 'setRegistrationNumber'.intelephense(1013)
;

Platform and version OS and Intelephense version. Ubuntu 21.04 Intelephense v1.7.1

kralos avatar Jul 13 '21 06:07 kralos

I am seeing exactly the same reproduced issues as well @bmewburn.

Is this going under investigation? How can I help?

IvoPereira avatar Nov 05 '21 08:11 IvoPereira

I get this a lot on 1.7.1 and Ubuntu 20 for no discernible reason.

Laravel Cashier's useCustomerModel is simultaneously not defined and defined (you can see the definition on hover). I've grown accustomed to ignoring 1013 because it is more than likely a defined method. Reloading the window doesn't fix the problem. In fact, I just typed useCustomerModel into another file and tab completed it, and the original 1013 went away. Upon reloading the vscode window it came back.

useCustomerModel is public and static with no return type, but a docblock return type of void.

image

markkimsal avatar Jan 02 '22 15:01 markkimsal

This has also been reported on stackoverflow: https://stackoverflow.com/a/68216025/5078765

adjenks avatar Jul 22 '22 17:07 adjenks