bmewburn / vscode-intelephense

PHP intellisense for Visual Studio Code
https://intelephense.com
Other
1.62k stars 94 forks source link

VS Code Symfony undefined method when exists #2168

Closed BernardA closed 2 years ago

BernardA commented 2 years ago

Describe the bug Shows error undefined method when method actually exists.

To Reproduce

On App\Stage\DeserializeStage

    if (!is_object($user = $token->getUser())) {
        return;
    }
    if ($resourceClass === 'App\Entity\BlockedUser' && $operationName === 'create') {
        $deserializeObject->setBlocker($user);
    } elseif ($resourceClass === 'App\Entity\Ad' && $operationName === 'create') {
        $deserializeObject->setUser($user);
        $deserializeObject->setIsActive(true);
        // when user creating ad offer, add providedCategory if not already exists
        if ($deserializeObject->getIsOffer()) {
            // check if user providedCategory already include ad category
            if (!$user->getProvidedCategories()->contains($deserializeObject->getCategory()) ) { **** ERROR *****
                $user->addProvidedCategory($deserializeObject->getCategory());    **** ERROR *****
            }
        }
    }

On App\Entity\User

 /**
 * @return Collection|Category[]
 */
public function getProvidedCategories(): Collection
{
    return $this->providedCategories;
}

public function addProvidedCategory(Category $providedCategory): self
{
    if (!$this->providedCategories->contains($providedCategory)) {
        $this->providedCategories[] = $providedCategory;
    }

    return $this;
}

Expected behavior Should not error, as the methods does exist.

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

Screenshot 2022-03-10 at 11 00 05

Platform and version Mac OS intelephense v 1.8.2

Rezyan commented 2 years ago

Hi @BernardA,

I'm not sure but this issue could probably be related to mine: [Bug] Broken parse of property types since v1.8.1 (worked fine under v1.8.0). Check if you still have the false positive error while downgrading the version of intelephense to 1.8.0.

If you get the following results, I think our issues are the same: Intelephense version Bug
1.8.0 No
1.8.1 Yes
1.8.2 Yes

How downgrade a VS Code extension to a previous version? Just like this:

vscodeextension

Regards.

BernardA commented 2 years ago

I went backwards to 1.8.0 and the problem was already there. Nice try though.

BernardA commented 2 years ago

It would seem that intelephense is not smart enough to know that User is referring to the User::class.

I needed to add type hint as below:

    /**
     * @var User $user
    */

    if (!is_object($user = $token->getUser())) {
        return $deserializeObject;
    }

   ...