phpstan / phpstan-doctrine

Doctrine extensions for PHPStan
MIT License
585 stars 96 forks source link

"$id is never written, only read" - DoctrineODM #316

Open den-kuz opened 2 years ago

den-kuz commented 2 years ago

PHP 8, Symfony 5.4, Doctrine ODM 4.4:

extensions installed:

image

objectManagerLoader provided:

image

tests/object-manager.php:

image

Still getting this error:

image

Some debug observations:

PHPStan\Type\Doctrine\ObjectMetadataResolver::getClassMetadata:

try {
    if ($objectManager === null) {
        $metadataFactory = $this->getMetadataFactory();
        if ($metadataFactory === null) {
            return null;
        }

        $metadata = $metadataFactory->getMetadataFor($className);
    } else {
        $metadata = $objectManager->getClassMetadata($className);
        if('App\Document\Campaign' === $className) {
            var_dump($className);
            var_dump(get_class($metadata));
            var_dump($metadata instanceof ClassMetadataInfo);
            die;
        }
    }
} catch (\Doctrine\Persistence\Mapping\MappingException | MappingException | AnnotationException $e) {
    return null;
}

if (!$metadata instanceof ClassMetadataInfo) {
    return null;
}
image
VincentLanglet commented 2 years ago

This is an expected behavior of phpstan if you have only getter and no setter in a class.

class Foo
{
     private int $id;

     public function getId() { return $id; }
}

There is no specific behavior implemented in phpstan-doctrine for this in document/entities class I think.

If you want to get rid of this error

ondrejmirtes commented 2 years ago

@VincentLanglet Of course there is: https://phpstan.org/blog/detecting-unused-private-properties-methods-constants#what-if-my-code-is-%E2%80%9Cspecial%E2%80%9D%3F

VincentLanglet commented 2 years ago

Oh I see the issue is https://github.com/phpstan/phpstan-doctrine/blob/ecc4aecaaf34871a2961c4c7a046bc2e092b0300/src/Rules/Doctrine/ORM/PropertiesExtension.php#L46-L55

I could try a PR.

But currently the PropertiesExtension is in a Doctrine/ORM folder. If we have support for ODM, moving the class could make sens.