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()); }