Skip to content

Deprecate MoveCurrentDateTimeDefaultInEntityToConstructorRector, drop force_nullable option - #504

Merged
TomasVotruba merged 1 commit into
mainfrom
deprecate-move-datetime-default
Aug 7, 2026
Merged

Deprecate MoveCurrentDateTimeDefaultInEntityToConstructorRector, drop force_nullable option#504
TomasVotruba merged 1 commit into
mainfrom
deprecate-move-datetime-default

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

1) Deprecate MoveCurrentDateTimeDefaultInEntityToConstructorRector

The database-level default and the PHP-level default are not interchangeable. The rule moved the default into the constructor, which changes when the value is created — the database default is evaluated on INSERT by the database, the constructor default on object creation by PHP. That is a behavior change the rule cannot verify, and it needs manual care per entity.

 use Doctrine\ORM\Mapping as ORM;

 /**
  * @ORM\Entity()
  */
 class User
 {
     /**
      * @var DateTimeInterface
-     * @ORM\Column(type="datetime", nullable=false, options={"default"="now()"})
+     * @ORM\Column(type="datetime", nullable=false)
      */
-    private $when = 'now()';
+    private $when;
+
+    public function __construct()
+    {
+        $this->when = new \DateTime();
+    }
 }

The rule is removed from the doctrine-code-quality set and now throws on use, like the other deprecated rules.

Its 3 exclusive helper services are removed as well: ValueAssignFactory, ConstructorManipulator, ConstructorAssignPropertyAnalyzer.

2) Drop force_nullable option of TypedPropertyFromToOneRelationTypeRector

We never know how the entity is used — it can always be constructed before the relation is set, so the property must be nullable. The option defaulted to true anyway; the false branch produced a non-nullable typed property that fatals on read before assignment.

 use Doctrine\ORM\Mapping as ORM;

 class SimpleColumn
 {
     /**
      * @ORM\OneToOne(targetEntity="App\Company\Entity\Company")
      * @ORM\JoinColumn(nullable=false)
      */
-    private $company;
+    private ?\App\Company\Entity\Company $company = null;
 }

ToOneRelationPropertyTypeResolver loses the nullable detection it no longer needs.

3) Cleanup

Removed leftover empty test config dir of the already-removed AddReturnDocBlockToCollectionPropertyGetterByToManyAnnotationRector.

… force_nullable option

The database-level default and the PHP-level default are not interchangeable,
so moving the default into the constructor can produce buggy code.

Also drop the force_nullable option of TypedPropertyFromToOneRelationTypeRector,
as the relation can always be unset while the entity is being built.
@TomasVotruba
TomasVotruba merged commit 1d348d3 into main Aug 7, 2026
7 checks passed
@TomasVotruba
TomasVotruba deleted the deprecate-move-datetime-default branch August 7, 2026 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant