Skip to content

Incorrect behavior of MoveCurrentDateTimeDefaultInEntityToConstructorRector #7996

Description

@toastedghost

Bug Report

Subject Details
Rector version 0.17.1
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/0edaff43-96f2-413b-952c-c0c8197f999b

<?php

use DateTime;
use Doctrine\ORM\Mapping as ORM;

final class DemoFile
{
    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    protected ?DateTime $date = null;
    
    public function __construct()
    {
    }    
}

Responsible rules

  • MoveCurrentDateTimeDefaultInEntityToConstructorRector

Actual Behavior

<?php

use DateTime;
use Doctrine\ORM\Mapping as ORM;

final class DemoFile
{
    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
-    protected ?DateTime $date = null;
+    protected ?DateTime $date;
    
    public function __construct()
    {
    }    
}

Expected Behavior

My expectation is that the code remains unchanged because nullable typed properties without default value, in this case null, leads to the error "Typed property ... must not be accessed before initialization". Or at least adds initialization with null in the constructor.

Metadata

Metadata

Assignees

Labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions