19

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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Christophe Dubois
  • 283
  • 1
  • 3
  • 12

4 Answers4

36

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.

user3539970
  • 493
  • 4
  • 5
14

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Pablo Câmara
  • 149
  • 1
  • 5
  • This simple solution also works when you have something to do with Codeception / Stub::make(). I need to state @var \Models\MyClass $theClass before I use $theClass = Stub::make(MyClass::class, ....) – Prabowo Murti Jan 06 '23 at 21:02
0

The first error seems to be that $iterator is not an ArrayIterator. You will need to check its type and if it's not ArrayIterator (or a class inherited from it), then that's the reason of the first error. On a subjective note, your situation seems to be some general problem with your dev environment.

Since PriceRepository has the method which your second error complains about, this seems to be the case that different Price and PriceRepository classes might be implemented in different namespaces and the wrong being in use. However, to determine whether this is true you will need to edit your question and provide the full definition of PriceRepository, Price and the use commands in the file where you have the problem.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • 1
    Thank you very much for your help. I added the asked code to my post. – Christophe Dubois Dec 22 '19 at 13:29
  • @ChristopheDubois thank you. Can you also add class PriceRepository? – Lajos Arpad Dec 22 '19 at 13:33
  • @ChristopheDubois no. You have a single line of it and from that we are unable to determine whether the right price is being in use. – Lajos Arpad Dec 22 '19 at 13:36
  • I again formatted the post to better see the PriceRepository code. Do I must add anything else? – Christophe Dubois Dec 22 '19 at 13:43
  • @ChristopheDubois no, now we can see that ```App\Repository\advert``` is the namespace in use. What happens if you execute compose install or compose update? – Lajos Arpad Dec 22 '19 at 13:53
  • @Larjos Arpad Sorry, I didn't see your message. I executed composer update and I have this error : Cannot autowire service "App\Repository\address\AddressRepository": argument "$registry" of method "__construct()" references interface "Symfony\Bridge\Doctrine\RegistryInterface" but no such service exists. Try changing the type-hint to "Doctrine\Persistence\ManagerRegistry" instead. – Christophe Dubois Dec 23 '19 at 18:04
  • @Larjos Arpad In each Repository, I changed Use Symfony\Bridge\Doctrine\RegistryInterface; by use use Doctrine\Persistence\ManagerRegistry; and public function __construct(RegistryInterface $registry) by public function __construct(ManagerRegistry $registry). I don't have anymore server errors but I always have the Intelephense error about the not existing getAdvertMinPrice function. – Christophe Dubois Dec 23 '19 at 20:01
0

Solved by uninstalling and reinstalling Visual Studio Code extensions.

Christophe Dubois
  • 283
  • 1
  • 3
  • 12