Skip to content

Commit

Permalink
[CodeQuality] Skip method call on else on TernaryFalseExpressionToIfR…
Browse files Browse the repository at this point in the history
…ector (#5373)

* [CodeQuality] Skip method call on else on TernaryFalseExpressionToIfRector

* Fixed 🎉

* add assign in else

* add else check

* use both

* fix
  • Loading branch information
samsonasik committed Dec 18, 2023
1 parent 02a30d4 commit 4283bef
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\Fixture;

final class SkipMethodCallOnElse
{
public function run(bool $param): void
{
$param ? $this->a() : $this->b();
}

public function a(): void
{
new HeavyObject1();
}

public function b(): null
{
new HeavyObject2();
return 'b';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\Fixture;

final class SkipMethodCallWithAssignOnElse
{
public function run(bool $param): void
{
$param ? $this->a() : $x = $this->b();
}

public function a(): void
{
new HeavyObject1();
}

public function b(): null
{
new HeavyObject2();
return 'b';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
return null;
}

if ($this->sideEffectNodeDetector->detect($ternary->else, $scope)) {
if ($this->sideEffectNodeDetector->detect($ternary->else, $scope) || $this->sideEffectNodeDetector->detectCallExpr($ternary->else, $scope)) {
return null;
}

Expand Down

0 comments on commit 4283bef

Please sign in to comment.