3

我在 phpstan 中使用 PHP8、symfony5 和教义 2 并收到以下错误:

Property App\Entity\User::$id is never written, only read.  

编码:

<?php

namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @ORM\Table(name="`user`")
 * @ORM\Entity(repositoryClass=UserRepository::class)
 * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
 */
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    public const ROLE_USER = 'ROLE_USER';
    public const ROLE_ADMIN = 'ROLE_ADMIN';

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=180, unique=true)
     */
    private $email;

    /**
     * @ORM\Column(type="json")
     */
    private $roles = [];

    /**
     * @var string The hashed password
     * @ORM\Column(type="string")
     */
    private $password;

    /**
     * @ORM\Column(type="boolean")
     */
    private $isVerified = false;

    public function __construct()
    {
        $this->audioSessions = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    /**
     * A visual identifier that represents this user.
     *
     * @see UserInterface
     */
    public function getUserIdentifier(): string
    {
        return (string) $this->email;
    }

    /**
     * @see UserInterface
     */
    public function getRoles(): array
    {
        $roles = $this->roles;
        // guarantee every user at least has ROLE_USER
        $roles[] = 'ROLE_USER';

        return array_unique($roles);
    }

    public function setRoles(array $roles): self
    {
        $this->roles = $roles;

        return $this;
    }

    /**
     * @see PasswordAuthenticatedUserInterface
     */
    public function getPassword(): string
    {
        return $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

    public function isVerified(): bool
    {
        return $this->isVerified;
    }

    public function setIsVerified(bool $isVerified): self
    {
        $this->isVerified = $isVerified;

        return $this;
    }
}

代码是正确的,PHPStan 也承认,如下所述:https ://phpstan.org/blog/detecting-unused-private-properties-methods-constants#what-if-my-code-is-%E2%80 %9C特殊%E2%80%9D%3F

那么我该如何解决 PHPStan 中的这些错误消息呢?

我尝试安装https://github.com/phpstan/phpstan-doctrine并在我的 phpstan.neon 文件中启用它:

includes:
    - vendor/phpstan/phpstan-doctrine/extension.neon
    - vendor/phpstan/phpstan-doctrine/rules.neon

但错误仍然存​​在。

4

3 回答 3

0

您需要进行配置objectManagerLoader,以便扩展程序可以看到实体元数据。这将允许 DQL 验证,并repositoryClass在访问$entityManager->getRepository(). 添加到您的phpstan.neon

parameters:
    doctrine:
        objectManagerLoader: tests/object-manager.php

Symfony 5 的示例:

// tests/object-manager.php

use App\Kernel;
use Symfony\Component\Dotenv\Dotenv;

require __DIR__ . '/../vendor/autoload.php';

(new Dotenv())->bootEnv(__DIR__ . '/../.env');

$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$kernel->boot();
return $kernel->getContainer()->get('doctrine')->getManager();

从文档: https ://github.com/phpstan/phpstan-doctrine#configuration

于 2021-11-19T15:38:46.017 回答
0

我正在使用 phpstan 忽略错误(https://phpstan.org/user-guide/ignoring-errors)。为了缩小范围,我的路径仅设置为实体目录。

# ./phpstan.neon

parameters:
    ignoreErrors:
        -
          message: '#Property [a-zA-Z0-9\\_]+::\$id is never written, only read.#'
          path: src/Entity/*
于 2021-12-21T14:03:47.883 回答
0

由于实现了这个RFC,PHPStan 在 1.4 版本中实现了这个特性。不幸的是,没有可用于 PHP < 8 的 @readonly 属性(至少目前如此)。有关更多详细信息,请随时查看此问题

于 2022-02-21T14:15:59.543 回答