phpstan-doctrine icon indicating copy to clipboard operation
phpstan-doctrine copied to clipboard

Always-read and written properties

Open AnjaLiebermann opened this issue 4 years ago • 7 comments

I use doctrine and have a table/entity "RecipientType" , which is only read. The table provides an information used in another table/entity called "GroupRecipient".

I have installed

        "phpstan/extension-installer":  "^1.1.0",
        "phpstan/phpstan":  "^1.4.2",
        "phpstan/phpstan-doctrine":  "^1.1.1"

and run a symfony5.4 on php 8.0.13

see here

When I run phpstan on level 5 I get the message:

 ------ ----------------------------------------------------------------------------------
  Line   src/Entity/RecipientType.php
 ------ ----------------------------------------------------------------------------------
  45     Property App\Entity\RecipientType::$groupRecipients is never written, only read.
         💡 See: https://phpstan.org/developing-extensions/always-read-written-properties
 ------ ----------------------------------------------------------------------------------

While this Url explains that this case should be coevred via the installed doctrine extension. "Properties for Doctrine ORM are already covered by the official PHPStan extension. "

What is wrong?

AnjaLiebermann avatar Jan 20 '22 19:01 AnjaLiebermann

Follow the README regarding the objectManagerLoader: https://github.com/phpstan/phpstan-doctrine#configuration

ondrejmirtes avatar Jan 20 '22 19:01 ondrejmirtes

I had that already in place:

parameters:
    doctrine:
        objectManagerLoader: tests/object-manager.php
    level: 5
    paths:
        - src/
        - tests/
    excludePaths:
        - %rootDir%/../../../migrations/*
        - %rootDir%/../../../src/Domain/Item/Event/AbstractItemEvent.php
        - %rootDir%/../../../tests/bootstrap.php
        - %rootDir%/../../../tests/object-manager.php
    checkGenericClassInNonGenericObjectType: false
    checkMissingIterableValueType: false
    inferPrivatePropertyTypeFromConstructor: true
    ignoreErrors:
    #   - '#Call to an undefined method [a-zA-Z0-9\\_]+::countByType\(\).#'
       - '/Call to an undefined method Doctrine\\Persistence\\ObjectRepository::count\(\)./'
       - '/If condition is always true./'
    #   - checkGenericClassInNonGenericObjectType: false
    #   - checkMissingIterableValueType: false

and I use the Symfony 5 Version of the objectmanager file, which makes no difference to the symfony 4 version in the end result:

<?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();

"symfony/dotenv": "5.4.*", is installed as well. Did I miss something else?

AnjaLiebermann avatar Jan 21 '22 07:01 AnjaLiebermann

In that case please show the entity definition.

ondrejmirtes avatar Jan 21 '22 07:01 ondrejmirtes

This is the entity throwing the error:

<?php

namespace App\Entity;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\RecipientTypeRepository")
 * @ORM\HasLifecycleCallbacks()
 * @codeCoverageIgnore
 */
class RecipientType
{
    use Active;

    public const GSM = 'gsm';
    public const VKL = 'vkl';  // Nur für Altdaten zugelassen, neu immer CM
    public const CM = 'cm'; // heißen jetzt RSM, nicht aktiv, wegen unsicherer Bestimmung der Adressaten
    public const TA = 'ta';
    public const TD = 'td';
    public const GF = 'gf';
    public const ZW = 'zw';  // entspricht AL
    public const VBL = 'vbl'; // Verbundbetriebsleitung nicht aktiv
    public const PNO = 'pno';

    /**
     * @ORM\Id
     * @ORM\Column(name="id", type="string", length=10, unique=true, nullable=false)
     */
    private string $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private string $name;

    /**
     * One RecipientType has many GroupRecipients. This is the inverse side.
     *
     * @ORM\OneToMany(targetEntity="GroupRecipient", mappedBy="recipientType")
     *
     * @var Collection<GroupRecipient>
     */
    private Collection $groupRecipients;

    public function __construct(string $id, string $name, bool $active = true)
    {
        $this->id = $id;
        $this->name = $name;
        $this->active = $active;
    }

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

    public function setId(string $id): self
    {
        $this->id = $id;

        return $this;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }
}

line 45, where the error is thrown is this one:

private Collection $groupRecipients;

AnjaLiebermann avatar Jan 21 '22 07:01 AnjaLiebermann

Typically OneToMany associations need new ArrayCollection() to be assigned to them in the constructor.

ondrejmirtes avatar Jan 21 '22 07:01 ondrejmirtes

Thank you so much! That was the problem!

AnjaLiebermann avatar Jan 21 '22 08:01 AnjaLiebermann

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Feb 22 '22 00:02 github-actions[bot]