From c6e344ab0f78fd298d01f661c8e26a296e9ef075 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 21 Feb 2022 11:16:54 +0700 Subject: [PATCH 1/7] [Php81] Add ConstFetch and ClassConstFetch support on NewInInitializerRector --- .../pass_class_const_fetch_arg.php.inc | 35 ++++++++++++++++++ .../Fixture/pass_const_fetch_arg.php.inc | 37 +++++++++++++++++++ ...lass_const_fetch_arg_dynamic_class.php.inc | 18 +++++++++ .../Source/SomeValueObject.php | 10 +++++ .../Php81/NodeAnalyzer/ComplexNewAnalyzer.php | 12 ++++++ 5 files changed, 112 insertions(+) create mode 100644 rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_class_const_fetch_arg.php.inc create mode 100644 rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_const_fetch_arg.php.inc create mode 100644 rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_pass_class_const_fetch_arg_dynamic_class.php.inc create mode 100644 rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Source/SomeValueObject.php diff --git a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_class_const_fetch_arg.php.inc b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_class_const_fetch_arg.php.inc new file mode 100644 index 00000000000..3bb04a4a1af --- /dev/null +++ b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_class_const_fetch_arg.php.inc @@ -0,0 +1,35 @@ +dateTime = $dateTime ?? new DateTime(SomeValueObject::NOW); + } +} + +?> +----- + diff --git a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_const_fetch_arg.php.inc b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_const_fetch_arg.php.inc new file mode 100644 index 00000000000..9ab86eead13 --- /dev/null +++ b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_const_fetch_arg.php.inc @@ -0,0 +1,37 @@ +dateTime = $dateTime ?? new DateTime(NOW); + } +} + +?> +----- + diff --git a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_pass_class_const_fetch_arg_dynamic_class.php.inc b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_pass_class_const_fetch_arg_dynamic_class.php.inc new file mode 100644 index 00000000000..27e4476b283 --- /dev/null +++ b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_pass_class_const_fetch_arg_dynamic_class.php.inc @@ -0,0 +1,18 @@ +dateTime = $dateTime ?? new DateTime($class::NOW); + } +} diff --git a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Source/SomeValueObject.php b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Source/SomeValueObject.php new file mode 100644 index 00000000000..3004b30e9fe --- /dev/null +++ b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Source/SomeValueObject.php @@ -0,0 +1,10 @@ +class instanceof Name) { + continue; + } + return true; } From 9deffbda6733520e47a386f0ec7520fb1572b8e6 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Mon, 21 Feb 2022 04:18:36 +0000 Subject: [PATCH 2/7] [ci-review] Rector Rectify --- rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php | 1 - 1 file changed, 1 deletion(-) diff --git a/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php b/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php index 87ff351c0f7..f9e0561dbe0 100644 --- a/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php +++ b/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php @@ -10,7 +10,6 @@ use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\New_; -use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Scalar; From dd74d667dd8f4b0584717037053d0d082bf66521 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Mon, 21 Feb 2022 04:20:57 +0000 Subject: [PATCH 3/7] [ci-review] Rector Rectify --- rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php b/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php index f9e0561dbe0..20bd06cbb2d 100644 --- a/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php +++ b/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php @@ -49,11 +49,13 @@ public function isDynamic(New_ $new): bool continue; } - if ($value instanceof ClassConstFetch && $value->class instanceof Name) { - continue; + if (! $value instanceof ClassConstFetch) { + return true; } - return true; + if (! $value->class instanceof Name) { + return true; + } } return false; From 42070f49e7ca2ed865592a93e867cc24907fa0d8 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 21 Feb 2022 11:23:55 +0700 Subject: [PATCH 4/7] phpstan --- rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php b/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php index 20bd06cbb2d..a55481e0cc3 100644 --- a/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php +++ b/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php @@ -10,6 +10,7 @@ use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\New_; +use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Scalar; @@ -53,7 +54,7 @@ public function isDynamic(New_ $new): bool return true; } - if (! $value->class instanceof Name) { + if (! $value->class instanceof Name || ! $value->name instanceof Identifier) { return true; } } From aa8e86d9632b64fa3a8c9487e488717ed172099a Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Mon, 21 Feb 2022 04:27:10 +0000 Subject: [PATCH 5/7] [ci-review] Rector Rectify --- rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php b/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php index a55481e0cc3..7a18428aa6d 100644 --- a/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php +++ b/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php @@ -53,8 +53,10 @@ public function isDynamic(New_ $new): bool if (! $value instanceof ClassConstFetch) { return true; } - - if (! $value->class instanceof Name || ! $value->name instanceof Identifier) { + if (! $value->class instanceof Name) { + return true; + } + if (! $value->name instanceof Identifier) { return true; } } From cbe960707f2f6eda47396c9a4f3fbc8908b7a501 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Mon, 21 Feb 2022 04:29:57 +0000 Subject: [PATCH 6/7] [ci-review] Rector Rectify --- rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php b/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php index 7a18428aa6d..32ec25098ca 100644 --- a/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php +++ b/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php @@ -53,9 +53,11 @@ public function isDynamic(New_ $new): bool if (! $value instanceof ClassConstFetch) { return true; } + if (! $value->class instanceof Name) { return true; } + if (! $value->name instanceof Identifier) { return true; } From 45369053b2ce1ae95da60bd7d9e7757c95dd2918 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 21 Feb 2022 11:37:28 +0700 Subject: [PATCH 7/7] phpstan --- .../Php81/NodeAnalyzer/ComplexNewAnalyzer.php | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php b/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php index 32ec25098ca..17b78f8e787 100644 --- a/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php +++ b/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php @@ -46,24 +46,27 @@ public function isDynamic(New_ $new): bool continue; } - if ($value instanceof ConstFetch) { + if ($this->isAllowedConstFetchOrClassConstFeth($value)) { continue; } - if (! $value instanceof ClassConstFetch) { - return true; - } + return true; + } - if (! $value->class instanceof Name) { - return true; - } + return false; + } - if (! $value->name instanceof Identifier) { - return true; - } + private function isAllowedConstFetchOrClassConstFeth(Expr $expr): bool + { + if (! in_array($expr::class, [ConstFetch::class, ClassConstFetch::class], true)) { + return false; } - return false; + if ($expr instanceof ClassConstFetch) { + return $expr->class instanceof Name && $expr->name instanceof Identifier; + } + + return true; } private function isAllowedNew(Expr $expr): bool