New rule: forbid replacing a persistent Doctrine collection field instead of mutating the collection

Open
#505 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
php
Domain
databases, tooling

Research direction

No implementation file or test is named. Start by reviewing the existing phpstan-doctrine rules and their tests, then study how Doctrine represents to-many collections and how setter assignments are analyzed. Done means the analyzer reliably flags replacing a persistent collection while allowing mutation of the existing collection.

Written by the indexing model from the issue text.

Description

When a field is a toMany relation, Doctrine relies on a special Collection implementation to track changes.

A common mistake is a write a setter that replaces the collection entirely instead of writing into the existing collection. This will make things that your collection field went from an empty list to its new content when checking for changes (as it lost all tracking for the existing content), which will do very bad things for the database data (in the best case, it triggers an error due to duplicate data in a unique index. In the worse case, it silently corrupts your data).

It would be great if phpstan-doctrine could prevent such mistakes for people using it.

Dominant language
PHP
Stars
678
Forks
122
Avg merge
5d 18h
Merged PRs (30d)
3

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from phpstan/phpstan-doctrine

All issues in phpstan/phpstan-doctrine

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.