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

Property is never written on inversed side of OneToOne relation properties

Open alexpozzi opened this issue 3 years ago • 6 comments

Hello, First of all thank you for all your work, it's great!

We recently upgraded to the latest phpstan/phpstan and phpstan/phpstan-doctrine and we started to have some errors reported on inversed side of OneToOne relationship properties.

For example, I have two entities:

class Signature {
  // ...

  /**
   * @ORM\OneToOne(targetEntity="App\Entity\Author", inversedBy="signature")
   * @ORM\JoinColumn(onDelete="SET NULL")
   *
   * @var Author|null
   */
  private $author;

  // ...
}

class Author {
  // ...

  /**
   * @ORM\OneToOne(targetEntity="App\Entity\Signature", mappedBy="author")
   *
   * @var Signature|null
   */
  private $signature;

  // ...
}

Both entities have a constructor and they are both non read-only entities. When I run phpstan I received the error Property App\Entity\Author::$signature is never written, only read. 💡 See: https://phpstan.org/developing-extensions/always-read-written-properties. Following the rules explained in https://phpstan.org/developing-extensions/always-read-written-properties it's correct that this error is raised but, I was wondering, if this is really a wanted behavior for this particular case. If it's not, I'd be happy to try to find a fix for it and submit a PR.

phpstan/phpstan version: 1.3.0 phpstan/phpstan-doctrine version: 1.0.2

alexpozzi avatar Jan 04 '22 14:01 alexpozzi

did you configure the object manager in the phpstan extension?

pscheit-lillydoo avatar Mar 11 '22 12:03 pscheit-lillydoo

Yes

alexpozzi avatar Mar 16 '22 07:03 alexpozzi

I'm seeing the same behaviour here, with a properly configured object manager.

jwpage avatar May 24 '22 02:05 jwpage

Did anybody find a solution or workaround? I experienced the same with one-to-one relation and properly configured object manager.

Btw, It works fine with $id fields of entities which is never written too. But not with one-to-one

leefant avatar Jun 01 '22 12:06 leefant

Seeing same issue with self referenced parent - child table.

fliespl avatar Jul 15 '23 19:07 fliespl