PHPStan 和 Doctrine:$id 永远不会被写入,只能读取

ivo*_*oba 9 php doctrine phpstan

我将 PHP8、symfony5 和doctrine2 与 phpstan 一起使用并收到以下错误:

Property App\Entity\User::$id is never written, only read.  
Run Code Online (Sandbox Code Playgroud)

代码:

<?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;
    }
}
Run Code Online (Sandbox Code Playgroud)

代码是正确的,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
Run Code Online (Sandbox Code Playgroud)

但错误仍然存​​在。

Wol*_*bat 6

我正在使用 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/*
Run Code Online (Sandbox Code Playgroud)


Bor*_*rys 3

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

parameters:
    doctrine:
        objectManagerLoader: tests/object-manager.php
Run Code Online (Sandbox Code Playgroud)

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();
Run Code Online (Sandbox Code Playgroud)

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