Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Dec 1, 2024
1 parent 87130cf commit 91a1975
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;

final class ShortCircuit5
{
public function run(bool $param)
{
return $this->clock?->now()->format;
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;

final class ShortCircuit5
{
public function run(bool $param)
{
return ($nullsafeVariable1 = $this->clock) ? $nullsafeVariable1->now()->format : null;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Check failure on line 90 in rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php

View workflow job for this annotation

GitHub Actions / code_analysis_reusable / PHPStan

Unreachable statement - code above always terminates.
}
Expand Down

0 comments on commit 91a1975

Please sign in to comment.