From 79a385f0841d9d5ddb3e8b60c17ed3ddd6c6f41b Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 23 Oct 2025 10:03:26 +0200 Subject: [PATCH 1/3] tidy up similar fixtures to make tests more readble --- .../Fixture/pass_non_dynamic_arg.php.inc | 4 +-- .../Fixture/pass_non_dynamic_arg2.php.inc | 4 +-- .../Fixture/pass_non_dynamic_arg3.php.inc | 29 ---------------- .../Fixture/pass_non_dynamic_arg4.php.inc | 33 ------------------- .../Fixture/pass_non_dynamic_arg5.php.inc | 29 ---------------- .../Fixture/pass_non_dynamic_arg6.php.inc | 29 ---------------- .../Fixture/skip_if_no_default_null.php.inc | 17 ++++++++++ .../Fixture/skip_no_param_construct.php.inc | 10 ------ .../Fixture/skip_pass_dynamic_arg.php.inc | 17 ---------- .../Fixture/skip_pass_dynamic_arg4.php.inc | 15 --------- .../Source/Logger/DefaultLogger.php | 8 +++++ .../Source/Logger/NullLogger.php | 8 +++++ 12 files changed, 37 insertions(+), 166 deletions(-) delete mode 100644 rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg3.php.inc delete mode 100644 rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg4.php.inc delete mode 100644 rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg5.php.inc delete mode 100644 rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg6.php.inc create mode 100644 rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_if_no_default_null.php.inc delete mode 100644 rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_no_param_construct.php.inc delete mode 100644 rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_pass_dynamic_arg.php.inc delete mode 100644 rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_pass_dynamic_arg4.php.inc create mode 100644 rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Source/Logger/DefaultLogger.php create mode 100644 rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Source/Logger/NullLogger.php diff --git a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg.php.inc b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg.php.inc index 204eee13924..770f8e03170 100644 --- a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg.php.inc +++ b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg.php.inc @@ -5,7 +5,7 @@ namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; use DateTime; use DateTimeZone; -class PassNonDynamicArg +final class PassNonDynamicArg { private DateTime $dateTime; @@ -25,7 +25,7 @@ namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; use DateTime; use DateTimeZone; -class PassNonDynamicArg +final class PassNonDynamicArg { public function __construct(private ?DateTime $dateTime = new DateTime('now', new DateTimeZone('Asia/Jakarta'))) { diff --git a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg2.php.inc b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg2.php.inc index 27ac7f33177..c78cd27296a 100644 --- a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg2.php.inc +++ b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg2.php.inc @@ -2,7 +2,7 @@ namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; -class SomeClass +final class PassNonDynamicArg2 { private Logger $logger; @@ -19,7 +19,7 @@ class SomeClass namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; -class SomeClass +final class PassNonDynamicArg2 { public function __construct(private ?Logger $logger = new NullLogger(['a' => 'b'])) { diff --git a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg3.php.inc b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg3.php.inc deleted file mode 100644 index 7fbd3e0f988..00000000000 --- a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg3.php.inc +++ /dev/null @@ -1,29 +0,0 @@ -logger = $logger ?? new NullLogger(['b']); - } -} - -?> ------ - diff --git a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg4.php.inc b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg4.php.inc deleted file mode 100644 index 2b2750b27bd..00000000000 --- a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg4.php.inc +++ /dev/null @@ -1,33 +0,0 @@ -logger = $logger ?? new NullLogger([new stdClass]); - } -} - -?> ------ - diff --git a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg5.php.inc b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg5.php.inc deleted file mode 100644 index a0be185f3eb..00000000000 --- a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg5.php.inc +++ /dev/null @@ -1,29 +0,0 @@ -logger = $logger ?? new NullLogger([]); - } -} - -?> ------ - diff --git a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg6.php.inc b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg6.php.inc deleted file mode 100644 index 9d95351b780..00000000000 --- a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg6.php.inc +++ /dev/null @@ -1,29 +0,0 @@ -logger = $logger ?? new NullLogger(new Db()); - } -} - -?> ------ - diff --git a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_if_no_default_null.php.inc b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_if_no_default_null.php.inc new file mode 100644 index 00000000000..69cbaf4ca7a --- /dev/null +++ b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_if_no_default_null.php.inc @@ -0,0 +1,17 @@ +logger = $logger ?? new NullLogger(); + } +} diff --git a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_no_param_construct.php.inc b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_no_param_construct.php.inc deleted file mode 100644 index 310912b4c0d..00000000000 --- a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_no_param_construct.php.inc +++ /dev/null @@ -1,10 +0,0 @@ -dateTime = $dateTime ?? new DateTime($variable); - } -} diff --git a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_pass_dynamic_arg4.php.inc b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_pass_dynamic_arg4.php.inc deleted file mode 100644 index f1ec2bcb0df..00000000000 --- a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_pass_dynamic_arg4.php.inc +++ /dev/null @@ -1,15 +0,0 @@ -logger = $logger ?? new NullLogger([$x => 'a']); - } -} diff --git a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Source/Logger/DefaultLogger.php b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Source/Logger/DefaultLogger.php new file mode 100644 index 00000000000..c302764d272 --- /dev/null +++ b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Source/Logger/DefaultLogger.php @@ -0,0 +1,8 @@ + Date: Thu, 23 Oct 2025 10:05:55 +0200 Subject: [PATCH 2/3] skip nullable property without null default, as null is always passed from outside to trigger --- .../ClassMethod/NewInInitializerRector.php | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/rules/Php81/Rector/ClassMethod/NewInInitializerRector.php b/rules/Php81/Rector/ClassMethod/NewInInitializerRector.php index ef27b692a5b..f2b942979c4 100644 --- a/rules/Php81/Rector/ClassMethod/NewInInitializerRector.php +++ b/rules/Php81/Rector/ClassMethod/NewInInitializerRector.php @@ -81,11 +81,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node->stmts === null || $node->stmts === []) { - return null; - } - - if ($node->isAbstract() || $node->isAnonymous()) { + if ($this->shouldSkipClass($node)) { return null; } @@ -104,6 +100,10 @@ public function refactor(Node $node): ?Node foreach ($params as $param) { $paramName = $this->getName($param); + if ($param->type instanceof NullableType && $param->default === null) { + continue; + } + $coalesce = $this->coalescePropertyAssignMatcher->matchCoalesceAssignsToLocalPropertyNamed( $stmt, $paramName @@ -215,4 +215,15 @@ private function matchConstructorParams(ClassMethod $classMethod): array return $params; } + + private function shouldSkipClass(Class_ $class): bool + { + if ($class->stmts === []) { + return true; + } + if ($class->isAbstract()) { + return true; + } + return $class->isAnonymous(); + } } From f331c0e1944b5e5b30d99a670bc327b25a9a6acd Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 23 Oct 2025 09:14:10 +0000 Subject: [PATCH 3/3] [ci-review] Rector Rectify --- rules/Php81/Rector/ClassMethod/NewInInitializerRector.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rules/Php81/Rector/ClassMethod/NewInInitializerRector.php b/rules/Php81/Rector/ClassMethod/NewInInitializerRector.php index f2b942979c4..e3db19e0437 100644 --- a/rules/Php81/Rector/ClassMethod/NewInInitializerRector.php +++ b/rules/Php81/Rector/ClassMethod/NewInInitializerRector.php @@ -221,9 +221,11 @@ private function shouldSkipClass(Class_ $class): bool if ($class->stmts === []) { return true; } + if ($class->isAbstract()) { return true; } + return $class->isAnonymous(); } }