7

I am using PHP8, symfony5 and doctrine2 with phpstan and getting these errors:

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

The code:

<?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;
    }
}

The code is correct which is also acknowledged by PHPStan as described here: https://phpstan.org/blog/detecting-unused-private-properties-methods-constants#what-if-my-code-is-%E2%80%9Cspecial%E2%80%9D%3F

So how can i solve these error message in PHPStan?

I tried installing https://github.com/phpstan/phpstan-doctrine and enabled this in my phpstan.neon file:

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

But the error still persists.

ivoba
  • 5,780
  • 5
  • 48
  • 55

3 Answers3

4

I am using phpstan ignore errors (https://phpstan.org/user-guide/ignoring-errors). To narrow the scope, my path is set to entity directory only.

# ./phpstan.neon

parameters:
    ignoreErrors:
        -
          message: '#Property [a-zA-Z0-9\\_]+::\$id is never written, only read.#'
          path: src/Entity/*
Wolly Wombat
  • 99
  • 1
  • 1
  • 9
2

You need to configure objectManagerLoader so that the extension can see the entity metadata. This will allow DQL validation, and the correct entity repositoryClass to be inferred when accessing $entityManager->getRepository(). Add to your phpstan.neon:

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

Example for 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();

From the documentation: https://github.com/phpstan/phpstan-doctrine#configuration

Borys
  • 46
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 19 '21 at 16:28
  • This works indeed :). – ivoba Nov 22 '21 at 16:42
  • i accept the answer but it would probably be better if you copy the necessary parts from the docs to your answer. – ivoba Nov 22 '21 at 16:44
0

Since this RFC is implemented, PHPStan implemented this feature in the 1.4 version. Unforthunully, there is no @readonly attribute (at least for the moment) that can be used with PHP < 8. For more detail, feel free to see this issue.

Houssem ZITOUN
  • 644
  • 1
  • 8
  • 23