Run Rector on your code to see what it can do for you:

 <?php
 
 final class DemoFile
 {
-	private Bar $foo;
-	
-	public function __construct(?Bar $foo = null) {
-		$this->foo = $foo ?? new Bar();
-	}
+	public function __construct(private readonly ?Bar $foo = new Bar())
+    {
+    }
     
     public function work(): string
     {
PHP snippet to change

Applied Rules:

Not a change you expect?

Config  rector.php
Rector version: 036cfd - released at 2026-02-24 16:36