Visual Studio Code PHP Intelephense gets errors which aren't

Visual Studio Code PHP Intelephense gets errors which aren't

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Visual Studio Code PHP Intelephense gets errors which aren't

Intelephense, the language server powering PHP diagnostics in Visual Studio Code, is an excellent tool for catching genuine type errors and syntax mistakes. However, it frequently flags methods as "undefined" even when they clearly exist in your codebase. This behavior often stems from a misunderstanding of how Intelephense analyzes types versus how Doctrine generates dynamic method signatures at runtime.

When you encounter an error like Undefined method 'uasort' on a collection or Undefined method 'getAdvertMinPrice' in a repository, the issue is rarely that the method is missing. Instead, it usually indicates that Intelephense has failed to infer the correct class type for the object you are operating on.

The ArrayCollection Trap

Consider the first example where $seasons is instantiated as an ArrayCollection:

$seasons = new ArrayCollection();
$sortedSeasons = new ArrayCollection();
    
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();
});

Intelephense analyzes the line $iterator->uasort(...). Since you are explicitly calling getIterator() on an ArrayCollection, the return type is technically a Traversable interface or, in many implementations, a specific internal iterator class. While uasort is available on standard PHP arrays and some collection wrappers, Intelephense might not have the full implementation details of ArrayCollection's iterator loaded in its local context, or it may be treating the result as a generic Traversable.

The solution isn't to ignore the error, but to understand that this is a static analysis limitation. The code runs perfectly fine because PHP dynamically resolves the method at runtime. If you must suppress this specific false positive without breaking linting for other files, consider checking your Intelephense configuration settings regarding how it handles third-party class implementations or simply accepting that ArrayCollection diagnostics are sometimes noisy in VS Code.

Doctrine Repository Method Resolution

The second example illustrates a more common scenario involving Doctrine repositories:

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

Here, Intelephense complains that getAdvertMinPrice is undefined, even though it exists in PriceRepository. The root cause is the lack of a static type definition for custom repository methods.

Doctrine's ServiceEntityRepository class uses PHPDoc magic comments to auto-generate method signatures:

/**
 * @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)
 */

These comments allow IDEs to autocomplete methods like find or findAll. However, any custom method you define manually—such as getAdvertMinPrice—does not automatically get registered in the static analysis model of Intelephense unless explicitly documented.

To resolve this, add a PHPDoc block directly above your custom method within the repository class:

/**
 * Get the minimum price from an advert
 *
 * @param Price $advert The advert entity to query
 * @return Price[] Returns an array of Price entities
 */ 
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()
        ;
}

Custom Entity Type Hints

Sometimes the issue is deeper: Intelephense doesn't know that Price::class resolves to a specific class, or it doesn't understand the relationship between your repository and entity. If your project uses a complex namespace structure like App\Repository\advert\PriceRepository, ensure that the corresponding Price entity is properly imported in the file where you call the method.

Intelephense relies heavily on local context. It does not automatically crawl your entire project to infer that getAdvertMinPrice exists on a repository associated with Price. By adding the docblock, you are essentially telling Intelephense, "Hey, this method returns an array of Prices." Once parsed, the IDE will stop flagging it as undefined.

Configuration and Limitations

If these manual fixes feel cumbersome across your entire Symfony 4 project, there are configuration options to consider. You can adjust the intelephense.php settings to be more lenient with type checking or to ignore specific classes that Intelephense struggles with.

However, the most robust approach is to align your code with what the linter expects. If you want Intelephense to understand custom repository methods without adding docblocks every time, you might consider using a library like phpstan-doctrine alongside Intelephense for stricter static analysis later in the pipeline. For now, documenting your custom repository methods remains the standard pattern for maintaining a clean Visual Studio Code experience.

If you are managing multiple Symfony projects and find yourself fighting these type inference issues constantly, tools that provide better entity relationship mapping or integrated PHPDoc generation can significantly reduce noise. There are resources available, such as those discussed at https://laravelcompany.com, that often cover advanced IDE configuration for PHP frameworks.

Our network