Visual Studio Code PHP Intelephense 出现错误,但事实并非如此

问题描述 投票:0回答:5

我正在使用 Visual Studio Code 和 Intelephense 开发 Symfony 4 项目。

Intelepense 会出现错误,但事实并非如此。有一些例子:

未定义方法“uasort”。

此错误对应此代码:

// 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();
});

另一个例子:

未定义方法“getAdvertMinPrice”。

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

但是,该方法存在于 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()
            ;
        }
    }

有价格名称空间:

<?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
{

以及我遇到问题的文件中的使用命令:

<?php

namespace App\Controller\advert;

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

我不明白问题出在哪里。我找了好几天都没有结果。

有人知道这个问题的根源吗?

php error-handling symfony4
5个回答
36
投票

只需重新加载窗口(VSCode)通常就能解决问题。无需重新安装扩展。

要重新加载 vscode,请打开命令面板 (

F1
) 并选择
Developer: Reload Window
。这相当于重新启动编辑器。


14
投票

我已经找到了解决方案:

这是我的项目中的一些代码:

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

我的“->搜索”给了我错误, 然后在我的变量上方添加 PHPDoc 注释:

/** @var ActivityRepository */

现在可以了!

另请检查一下:

如何使用 PHPDoc 表示法声明局部变量的类型?


1
投票

通过卸载并重新安装 Visual Studio Code 扩展解决。


0
投票

第一个错误似乎是

$iterator
不是 ArrayIterator。您需要检查它的类型,如果它不是
ArrayIterator
(或从它继承的
class
),那么这就是第一个错误的原因。从主观角度来看,您的情况似乎是您的开发环境存在一些普遍问题。

由于

PriceRepository
具有您的第二个错误所抱怨的方法,因此似乎不同的
Price
PriceRepository
类可能在不同的命名空间中实现,并且使用错误。但是,要确定这是否属实,您需要编辑问题并提供
PriceRepository
、价格的完整定义以及出现问题的文件中的使用命令。


0
投票

hola tengo estos 问题 致命错误:错误无检测:Error al abrir 'C:\wamp64\www\CI4../app/Config/Paths.php' (include_path='.;C:\php\pear') en C:\wamp64\ www\CI4\index.php 位于 34 行

(!) 错误:错误 abrir 'C:\wamp64\www\CI4../app/Config/Paths.php' (include_path='.;C:\php\pear') en C:\wamp64\www \CI4\index.php 位于 34 行

© www.soinside.com 2019 - 2024. All rights reserved.