Skip to content

Commit 91a1975

Browse files
committed
fix
1 parent 87130cf commit 91a1975

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;
4+
5+
final class ShortCircuit5
6+
{
7+
public function run(bool $param)
8+
{
9+
return $this->clock?->now()->format;
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;
18+
19+
final class ShortCircuit5
20+
{
21+
public function run(bool $param)
22+
{
23+
return ($nullsafeVariable1 = $this->clock) ? $nullsafeVariable1->now()->format : null;
24+
}
25+
}
26+
27+
?>

rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,24 @@ public function refactor(Node $node): ?Ternary
6868
$nullsafeVariable = $this->createNullsafeVariable();
6969

7070
$assign = new Assign($nullsafeVariable, $node->var->var);
71-
$methodCallOrPropertyFetch = $node->var instanceof NullsafeMethodCall
72-
? new MethodCall(new MethodCall(
73-
$nullsafeVariable,
74-
$node->var->name,
75-
$node->var->args
76-
), $node->name, $node->args)
77-
: new PropertyFetch(new PropertyFetch($nullsafeVariable, $node->var->name), $node->name);
71+
72+
if ($node instanceof MethodCall) {
73+
if ($node->var instanceof NullsafeMethodCall) {
74+
$methodCallOrPropertyFetch = new MethodCall(new MethodCall($nullsafeVariable, $node->var->name, $node->var->args), $node->name, $node->args);
75+
return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull());
76+
} else {
77+
$methodCallOrPropertyFetch = new MethodCall(new PropertyFetch($nullsafeVariable, $node->var->name), $node->name, $node->args);
78+
return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull());
79+
}
80+
} else {
81+
if ($node->var instanceof NullsafeMethodCall) {
82+
$methodCallOrPropertyFetch = new PropertyFetch(new MethodCall($nullsafeVariable, $node->var->name, $node->var->args), $node->name);
83+
return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull());
84+
} else {
85+
$methodCallOrPropertyFetch = new PropertyFetch(new PropertyFetch($nullsafeVariable, $node->var->name), $node->name);
86+
return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull());
87+
}
88+
}
7889

7990
return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull());
8091
}

0 commit comments

Comments
 (0)