From a8d25a367740f389fcbfe10d0649d09fbf05d5ad Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 1 Dec 2024 10:32:52 +0700 Subject: [PATCH 1/7] [DowngradePhp80] Handle short circuit on DowngradeNullsafeToTernaryOperatorRector --- ...wngradeNullsafeToTernaryOperatorRector.php | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php b/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php index 03236ed5..93616640 100644 --- a/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php +++ b/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php @@ -55,18 +55,33 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [NullsafeMethodCall::class, NullsafePropertyFetch::class]; + return [MethodCall::class, PropertyFetch::class, NullsafeMethodCall::class, NullsafePropertyFetch::class]; } /** - * @param NullsafeMethodCall|NullsafePropertyFetch $node + * @param MethodCall|NullsafeMethodCall|NullsafePropertyFetch $node */ - public function refactor(Node $node): Ternary + public function refactor(Node $node): ?Ternary { + if ($node instanceof MethodCall || $node instanceof PropertyFetch) { + if ($node->var instanceof NullsafeMethodCall || $node->var instanceof NullsafePropertyFetch) { + $nullsafeVariable = $this->createNullsafeVariable(); + + $assign = new Assign($nullsafeVariable, $node->var->var); + $methodCallOrPropertyFetch = $node->var instanceof NullsafeMethodCall + ? new MethodCall(new MethodCall($nullsafeVariable, $node->var->name, $node->var->args), $node->name, $node->args) + : new PropertyFetch(new PropertyFetch($nullsafeVariable, $node->var->name), $node->name); + + return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull()); + } + + return null; + } + $nullsafeVariable = $this->createNullsafeVariable(); $methodCallOrPropertyFetch = $node instanceof NullsafeMethodCall - ? new MethodCall($nullsafeVariable, $node->name, $node->getArgs()) + ? new MethodCall($nullsafeVariable, $node->name, $node->args) : new PropertyFetch($nullsafeVariable, $node->name); $assign = new Assign($nullsafeVariable, $node->var); From e778c2c36fc02a5c38ed6c5086a46c05300f4691 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 1 Dec 2024 10:32:56 +0700 Subject: [PATCH 2/7] [DowngradePhp80] Handle short circuit on DowngradeNullsafeToTernaryOperatorRector --- .../Fixture/short_circuit.php.inc | 27 +++++++++++++++++++ .../Fixture/short_circuit.php2.inc | 27 +++++++++++++++++++ .../Fixture/short_circuit3.php.inc | 27 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit.php.inc create mode 100644 rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit.php2.inc create mode 100644 rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit3.php.inc diff --git a/rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit.php.inc b/rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit.php.inc new file mode 100644 index 00000000..ba99b780 --- /dev/null +++ b/rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit.php.inc @@ -0,0 +1,27 @@ +clock?->now()->format('U.u'); + } +} + +?> +----- +clock) ? $nullsafeVariable1->now()->format('U.u') : null; + } +} + +?> diff --git a/rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit.php2.inc b/rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit.php2.inc new file mode 100644 index 00000000..bd59fe73 --- /dev/null +++ b/rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit.php2.inc @@ -0,0 +1,27 @@ +clock?->now()->format; + } +} + +?> +----- +clock) ? $nullsafeVariable1->now()->format : null; + } +} + +?> diff --git a/rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit3.php.inc b/rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit3.php.inc new file mode 100644 index 00000000..6e092e8f --- /dev/null +++ b/rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit3.php.inc @@ -0,0 +1,27 @@ +clock?->now->format; + } +} + +?> +----- +clock) ? $nullsafeVariable1->now->format : null; + } +} + +?> From 961c77b671afa2a80ca7d7e13c3cb12b77cf4c13 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sun, 1 Dec 2024 03:33:55 +0000 Subject: [PATCH 3/7] [ci-review] Rector Rectify --- .../DowngradeNullsafeToTernaryOperatorRector.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php b/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php index 93616640..248c71d1 100644 --- a/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php +++ b/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php @@ -69,7 +69,11 @@ public function refactor(Node $node): ?Ternary $assign = new Assign($nullsafeVariable, $node->var->var); $methodCallOrPropertyFetch = $node->var instanceof NullsafeMethodCall - ? new MethodCall(new MethodCall($nullsafeVariable, $node->var->name, $node->var->args), $node->name, $node->args) + ? new MethodCall(new MethodCall( + $nullsafeVariable, + $node->var->name, + $node->var->args + ), $node->name, $node->args) : new PropertyFetch(new PropertyFetch($nullsafeVariable, $node->var->name), $node->name); return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull()); From 87130cf48b2c6c52b7a1046836edc56bb3e48614 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 1 Dec 2024 10:35:34 +0700 Subject: [PATCH 4/7] more fixture --- .../Fixture/short_circuit4.php.inc | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit4.php.inc diff --git a/rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit4.php.inc b/rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit4.php.inc new file mode 100644 index 00000000..3b6bcf05 --- /dev/null +++ b/rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit4.php.inc @@ -0,0 +1,27 @@ +clock?->now->format('U.u'); + } +} + +?> +----- +clock) ? $nullsafeVariable1->now->format('U.u') : null; + } +} + +?> From 91a197569069d40451aacf5d62ae0b94322727e2 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 1 Dec 2024 10:52:22 +0700 Subject: [PATCH 5/7] fix --- .../Fixture/short_circuit5.php.inc | 27 +++++++++++++++++++ ...wngradeNullsafeToTernaryOperatorRector.php | 25 ++++++++++++----- 2 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit5.php.inc diff --git a/rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit5.php.inc b/rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit5.php.inc new file mode 100644 index 00000000..ac3b63b6 --- /dev/null +++ b/rules-tests/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector/Fixture/short_circuit5.php.inc @@ -0,0 +1,27 @@ +clock?->now()->format; + } +} + +?> +----- +clock) ? $nullsafeVariable1->now()->format : null; + } +} + +?> diff --git a/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php b/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php index 248c71d1..2a9eaac9 100644 --- a/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php +++ b/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php @@ -68,13 +68,24 @@ public function refactor(Node $node): ?Ternary $nullsafeVariable = $this->createNullsafeVariable(); $assign = new Assign($nullsafeVariable, $node->var->var); - $methodCallOrPropertyFetch = $node->var instanceof NullsafeMethodCall - ? new MethodCall(new MethodCall( - $nullsafeVariable, - $node->var->name, - $node->var->args - ), $node->name, $node->args) - : new PropertyFetch(new PropertyFetch($nullsafeVariable, $node->var->name), $node->name); + + if ($node instanceof MethodCall) { + if ($node->var instanceof NullsafeMethodCall) { + $methodCallOrPropertyFetch = new MethodCall(new MethodCall($nullsafeVariable, $node->var->name, $node->var->args), $node->name, $node->args); + return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull()); + } else { + $methodCallOrPropertyFetch = new MethodCall(new PropertyFetch($nullsafeVariable, $node->var->name), $node->name, $node->args); + return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull()); + } + } else { + if ($node->var instanceof NullsafeMethodCall) { + $methodCallOrPropertyFetch = new PropertyFetch(new MethodCall($nullsafeVariable, $node->var->name, $node->var->args), $node->name); + return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull()); + } else { + $methodCallOrPropertyFetch = new PropertyFetch(new PropertyFetch($nullsafeVariable, $node->var->name), $node->name); + return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull()); + } + } return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull()); } From 489ce474befddead76ca05a1b95eff79b99e8645 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sun, 1 Dec 2024 03:53:03 +0000 Subject: [PATCH 6/7] [ci-review] Rector Rectify --- ...wngradeNullsafeToTernaryOperatorRector.php | 42 +++++++++++++------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php b/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php index 2a9eaac9..4ea31bfb 100644 --- a/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php +++ b/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php @@ -68,26 +68,42 @@ public function refactor(Node $node): ?Ternary $nullsafeVariable = $this->createNullsafeVariable(); $assign = new Assign($nullsafeVariable, $node->var->var); - if ($node instanceof MethodCall) { if ($node->var instanceof NullsafeMethodCall) { - $methodCallOrPropertyFetch = new MethodCall(new MethodCall($nullsafeVariable, $node->var->name, $node->var->args), $node->name, $node->args); - return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull()); - } else { - $methodCallOrPropertyFetch = new MethodCall(new PropertyFetch($nullsafeVariable, $node->var->name), $node->name, $node->args); - return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull()); - } - } else { - if ($node->var instanceof NullsafeMethodCall) { - $methodCallOrPropertyFetch = new PropertyFetch(new MethodCall($nullsafeVariable, $node->var->name, $node->var->args), $node->name); - return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull()); - } else { - $methodCallOrPropertyFetch = new PropertyFetch(new PropertyFetch($nullsafeVariable, $node->var->name), $node->name); + $methodCallOrPropertyFetch = new MethodCall(new MethodCall( + $nullsafeVariable, + $node->var->name, + $node->var->args + ), $node->name, $node->args); return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull()); } + $methodCallOrPropertyFetch = new MethodCall(new PropertyFetch( + $nullsafeVariable, + $node->var->name + ), $node->name, $node->args); + return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull()); + } + + if ($node->var instanceof NullsafeMethodCall) { + $methodCallOrPropertyFetch = new PropertyFetch(new MethodCall( + $nullsafeVariable, + $node->var->name, + $node->var->args + ), $node->name); + return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull()); } + $methodCallOrPropertyFetch = new PropertyFetch(new PropertyFetch( + $nullsafeVariable, + $node->var->name + ), $node->name); return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull()); + + return new Ternary( + $assign, + $methodCallOrPropertyFetch, + $this->nodeFactory->createNull() + ); } return null; From 173fd86bdeca11beb34fad99adc76f8e82502c31 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 1 Dec 2024 10:53:58 +0700 Subject: [PATCH 7/7] cs fix --- .../DowngradeNullsafeToTernaryOperatorRector.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php b/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php index 4ea31bfb..9d8a456d 100644 --- a/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php +++ b/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php @@ -77,6 +77,7 @@ public function refactor(Node $node): ?Ternary ), $node->name, $node->args); return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull()); } + $methodCallOrPropertyFetch = new MethodCall(new PropertyFetch( $nullsafeVariable, $node->var->name @@ -98,12 +99,6 @@ public function refactor(Node $node): ?Ternary $node->var->name ), $node->name); return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull()); - - return new Ternary( - $assign, - $methodCallOrPropertyFetch, - $this->nodeFactory->createNull() - ); } return null;