From 6525345f4d3e594311a7186982087ec17ef8b0ee Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 2 May 2024 15:04:33 +0700 Subject: [PATCH] [CodeQuality] Fix tautology transformation on identical check on SimplifyTautologyTernaryRector (#5854) * SimplifyTautologyTernaryRector breaks logic * rename fixture * fixture fix * update fixture * more fixture --------- Co-authored-by: kkmuffme <11071985+kkmuffme@users.noreply.github.com> --- .../Fixture/fixture.php.inc | 4 +- .../Fixture/identical_compare_left.php.inc | 51 +++++++++++++++++++ .../SimplifyTautologyTernaryRector.php | 2 +- 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/Ternary/SimplifyTautologyTernaryRector/Fixture/identical_compare_left.php.inc diff --git a/rules-tests/CodeQuality/Rector/Ternary/SimplifyTautologyTernaryRector/Fixture/fixture.php.inc b/rules-tests/CodeQuality/Rector/Ternary/SimplifyTautologyTernaryRector/Fixture/fixture.php.inc index 37062483545..dfa20c0c255 100644 --- a/rules-tests/CodeQuality/Rector/Ternary/SimplifyTautologyTernaryRector/Fixture/fixture.php.inc +++ b/rules-tests/CodeQuality/Rector/Ternary/SimplifyTautologyTernaryRector/Fixture/fixture.php.inc @@ -11,7 +11,6 @@ function tautologyTernary() $value = ($typeHint !== $fullyQualifiedTypeHint) ? $fullyQualifiedTypeHint : $typeHint; $value = ($typeHint === $fullyQualifiedTypeHint) ? $fullyQualifiedTypeHint : $typeHint; - $value = ($typeHint === $fullyQualifiedTypeHint) ? $typeHint : $fullyQualifiedTypeHint; } @@ -29,9 +28,8 @@ function tautologyTernary() $value = $fullyQualifiedTypeHint; $value = $fullyQualifiedTypeHint; - $value = $fullyQualifiedTypeHint; - $value = $typeHint; + $value = $fullyQualifiedTypeHint; } ?> diff --git a/rules-tests/CodeQuality/Rector/Ternary/SimplifyTautologyTernaryRector/Fixture/identical_compare_left.php.inc b/rules-tests/CodeQuality/Rector/Ternary/SimplifyTautologyTernaryRector/Fixture/identical_compare_left.php.inc new file mode 100644 index 00000000000..7eb7c80a9e7 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Ternary/SimplifyTautologyTernaryRector/Fixture/identical_compare_left.php.inc @@ -0,0 +1,51 @@ +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); + } +} + +?> +----- +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); + } +} + +?> diff --git a/rules/CodeQuality/Rector/Ternary/SimplifyTautologyTernaryRector.php b/rules/CodeQuality/Rector/Ternary/SimplifyTautologyTernaryRector.php index 51e56a7ab99..67e6be187a7 100644 --- a/rules/CodeQuality/Rector/Ternary/SimplifyTautologyTernaryRector.php +++ b/rules/CodeQuality/Rector/Ternary/SimplifyTautologyTernaryRector.php @@ -61,6 +61,6 @@ public function refactor(Node $node): ?Node return null; } - return $node->if; + return $node->cond instanceof NotIdentical ? $node->if : $node->else; } }