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

Incorrect behavior of NewInInitializerRector #8971

Closed
mathieudz opened this issue Jan 13, 2025 · 2 comments · Fixed by rectorphp/rector-src#6793
Closed

Incorrect behavior of NewInInitializerRector #8971

mathieudz opened this issue Jan 13, 2025 · 2 comments · Fixed by rectorphp/rector-src#6793
Labels

Comments

@mathieudz
Copy link

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/2dc71679-38db-4a2b-be63-51889497e171

<?php

use DateTime;

final class DemoFile
{
    private DateTime $dateTime;
    
    public function __construct(?DateTime $dateTime = null)
    {
        $this->dateTime = $dateTime ?? new DateTime();
    }
}

$demo = new DemoFile(null);

Responsible rules

  • NewInInitializerRector

Expected Behavior

Don't create compilation errors: the constructor cannot be called with a null parameter anymore.

@mathieudz mathieudz added the bug label Jan 13, 2025
@samsonasik
Copy link
Member

I think you can simply skip the rule :)

possible improvement is we need to improve the rule to add New_ node to remove null arg when param is new initializer based on target class.

@samsonasik
Copy link
Member

samsonasik commented Mar 7, 2025

for note, make nullable doesn't make it work since the fallback is if it not have arg at all, ref

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants