Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code PHP Intelephense gets errors which aren't

I'm working on a Symfony 4 project using Visual Studio Code with Intelephense.

Intelephense gets errors which aren't. There are some examples:

Undefined method 'uasort'.

This error corresponding to this code:

// Collection creation of seasons used in the periods 
$seasons = new ArrayCollection();
$sortedSeasons = new ArrayCollection();
    
//Search seasons used by periods
foreach ($advert->getPeriods() as $period) 
{
    $season = $period->getSeason();
    if (! $seasons->contains($season)) 
    {
        $seasons->add($season);
    }
}
    
// Sort seasons by ascending cost 
$iterator = $seasons->getIterator();
$iterator->uasort(function ($a, $b) {
    return $a->getCost() <=> $b->getCost();
});

An other example:

Undefined method 'getAdvertMinPrice'.

$minPrices = $this->getDoctrine()->getRepository(Price::class)->getAdvertMinPrice($advert);

However, the method exists in the PriceRepository:

<?php
namespace App\Repository\advert;

use App\Entity\advert\Price;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
    
/**
 * @method Price|null find($id, $lockMode = null, $lockVersion = null)
 * @method Price|null findOneBy(array $criteria, array $orderBy = null)
 * @method Price[]    findAll()
 * @method Price[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class PriceRepository extends ServiceEntityRepository
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, Price::class);
    }
    
   /**
    * Get the minimum price from an advert
    *
    * @param [type] $advert
    * @return Price[]
    */ 
    public function getAdvertMinPrice($advert): array
    {
        return $this->createQueryBuilder('p')
            ->andWhere('p.price = (
                            SELECT MIN(p2.price)
                            FROM ' . $this->_entityName . ' p2
                            WHERE p2.advert = :val
                        )'
                       )
            ->setParameter('val', $advert)
            ->getQuery()
            ->getResult()
            ;
        }
    }

There is the Price name space:

<?php

namespace App\Entity\advert;

use App\Entity\advert\Advert;
use App\Entity\advert\Period;
use App\Entity\backend\Season;
use App\Entity\backend\Duration;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity(repositoryClass="App\Repository\advert\PriceRepository")
 * 
 * @UniqueEntity(
 *     fields={"duration", "season"},
 *     message="A price already exists for this season and this duration."
 * )
 */
class Price
{

And the use command in the file where I have the problem:

<?php

namespace App\Controller\advert;

use App\Entity\backend\VAT;
use App\Entity\advert\Price;

I don't understand where is the problem. I have searched for days without result.

Somebody would have an idea about this problem origin?

like image 780
Christophe Dubois Avatar asked Dec 22 '19 12:12

Christophe Dubois


People also ask

Why php is not working in VS Code?

Go to File->Preferences->settings->User settings tab->extensions->from the drop down select php->on the right pane under PHP › Validate: Executable Path select edit in settings. json. Found this solution from php not found visual studio. Save this answer.

Why is my VS Code not showing errors?

Show activity on this post. This is because the C/C++ IntelliSense, debugging, and code browsing extension does not know about the current project. Navigate to View | Command Palette, enter and select C/C++ Build and debug active file: Select Project, and then select the correct project that you want to work with.

Why Autocomplete is not working in VS Code?

Why is VS Code suggestions not working? If you're coding in JavaScript or TypeScript and finds that VSCode IntelliSenseIntelliSenseIntelligent code completion is a context-aware code completion feature in some programming environments that speeds up the process of coding applications by reducing typos and other common mistakes.https://en.wikipedia.org › wiki › Intelligent_code_completionIntelligent code completion - Wikipedia does work but does not behave properly, it's likely that you've selected the wrong language mode. TypeScript and JavaScript share the same language service, so you need to select the right language.


2 Answers

Simply reloading the window (VSCode) often solves the problem. No need to reinstall the extension then.

To reload vscode open the command palette (F1) and select Developer: Reload Window. This is equal to restarting the editor.

like image 62
user3539970 Avatar answered Oct 01 '22 02:10

user3539970


I have found a solution for this:

Here is some code from a project of mine:

/** @var ActivityRepository */
$activityRepo = $this->getDoctrine()->getRepository(Activity::class);
return $this->json($activityRepo->search($searchData['text']));

My "->search" was giving me error, then added the PHPDoc comment above my variable:

/** @var ActivityRepository */

and now it works!

Also check this out:

How to declare the type for local variables using PHPDoc notation?

like image 42
Pablo Câmara Avatar answered Oct 01 '22 03:10

Pablo Câmara