Skip to content

Commit

Permalink
[CodeQuality] Fix tautology transformation on identical check on Simp…
Browse files Browse the repository at this point in the history
…lifyTautologyTernaryRector (#5854)

* SimplifyTautologyTernaryRector breaks logic

* rename fixture

* fixture fix

* update fixture

* more fixture

---------

Co-authored-by: kkmuffme <11071985+kkmuffme@users.noreply.github.com>
  • Loading branch information
samsonasik and kkmuffme committed May 2, 2024
1 parent c7d2ca3 commit 6525345
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ function tautologyTernary()
$value = ($typeHint !== $fullyQualifiedTypeHint) ? $fullyQualifiedTypeHint : $typeHint;

$value = ($typeHint === $fullyQualifiedTypeHint) ? $fullyQualifiedTypeHint : $typeHint;

$value = ($typeHint === $fullyQualifiedTypeHint) ? $typeHint : $fullyQualifiedTypeHint;
}

Expand All @@ -29,9 +28,8 @@ function tautologyTernary()
$value = $fullyQualifiedTypeHint;
$value = $fullyQualifiedTypeHint;

$value = $fullyQualifiedTypeHint;

$value = $typeHint;
$value = $fullyQualifiedTypeHint;
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector\Fixture;

final class IdenticalCompareLeft
{
public function run(string $param)
{
$foo = $this->bar($param) === '' ? '' : $this->bar($param);
echo 'asd' . $foo . 'def';
}

public function run2(string $param)
{
$foo = '' === $this->bar($param) ? '' : $this->bar($param);
echo 'asd' . $foo . 'def';
}

private function bar(string $value)
{
return trim($value);
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector\Fixture;

final class IdenticalCompareLeft
{
public function run(string $param)
{
$foo = $this->bar($param);
echo 'asd' . $foo . 'def';
}

public function run2(string $param)
{
$foo = $this->bar($param);
echo 'asd' . $foo . 'def';
}

private function bar(string $value)
{
return trim($value);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ public function refactor(Node $node): ?Node
return null;
}

return $node->if;
return $node->cond instanceof NotIdentical ? $node->if : $node->else;
}
}

0 comments on commit 6525345

Please sign in to comment.