Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property is never written, only read - Misleading message when constructor is given for readonly entity #601

Open
ServerExe opened this issue Jul 18, 2024 · 0 comments

Comments

@ServerExe
Copy link

ServerExe commented Jul 18, 2024

I have a readonly entity:

<?php
declare(strict_types=1);

namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\UuidV7;

#[ORM\Entity(repositoryClass: UserRepository::class, readOnly: true)]
class User
{
    #[ORM\Id]
    #[ORM\Column(type: 'uuid')]
    private UuidV7 $id;

    #[ORM\Column]
    private string $name;

    public function __construct(UuidV7 $id = null)
    {
        $this->id = $id ?? new UuidV7();
    }

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

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

PHPStan (Doctrine Extension) is throwing analyse errors:

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

Although my entity is marked as readonly: true PHPStan is still throwing these errors. Reason is, because I have added a constructor which sets the property with an initial ID. If I remove the constructor, everything is fine.

Question might be now:
"Why do you even set any property if your entity is readonly?"

Reason:
This entity is used inside a project which should not be able to persist entity data. I copied this entity from my other project but removed all setters. I kept the constructor to create entities inside my unit tests. But yeah, you would be right here, it still does not really make sense.

Suggestion:
Instead of this misleading error message, which took me hours to realize it probably would be better to write something like:

Entity is marked as readonly but property App\Entity\User::$id is written

What do you think?

@ServerExe ServerExe changed the title Bug: Property is never written, only read - Misleading message when constructor is given Bug: Property is never written, only read - Misleading message when constructor is given for readonly entity Jul 18, 2024
@ServerExe ServerExe changed the title Bug: Property is never written, only read - Misleading message when constructor is given for readonly entity Property is never written, only read - Misleading message when constructor is given for readonly entity Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant