Skip to content

Commit a69e335

Browse files
samsonasikjwderoos
andauthored
[CodeQuality] Handle ternary in identical left/right on UseIdenticalOverEqualWithSameTypeRector (#7748)
* Create identical_enclosed_ternary.php.inc Enclosed ternary comparations should retain the parentheses * fix --------- Co-authored-by: JW de Roos <[email protected]>
1 parent 8edca7d commit a69e335

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector\Fixture;
4+
5+
if ((true ? 1 : 0) != (true ? 1 : 0)) {
6+
7+
}
8+
9+
?>
10+
-----
11+
<?php
12+
13+
namespace Rector\Tests\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector\Fixture;
14+
15+
if ((true ? 1 : 0) !== (true ? 1 : 0)) {
16+
17+
}
18+
19+
?>

rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
use PhpParser\Node\Expr\BinaryOp\Identical;
1111
use PhpParser\Node\Expr\BinaryOp\NotEqual;
1212
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
13+
use PhpParser\Node\Expr\Ternary;
1314
use PHPStan\Type\BooleanType;
1415
use PHPStan\Type\FloatType;
1516
use PHPStan\Type\IntegerType;
1617
use PHPStan\Type\MixedType;
1718
use PHPStan\Type\StringType;
1819
use PHPStan\Type\Type;
1920
use PHPStan\Type\TypeTraverser;
21+
use Rector\NodeTypeResolver\Node\AttributeKey;
2022
use Rector\Rector\AbstractRector;
2123
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2224
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -135,6 +137,14 @@ private function normalizeScalarType(Type $type): Type
135137

136138
private function processIdenticalOrNotIdentical(Equal|NotEqual $node): Identical|NotIdentical
137139
{
140+
if ($node->left instanceof Ternary) {
141+
$node->left->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true);
142+
}
143+
144+
if ($node->right instanceof Ternary) {
145+
$node->right->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true);
146+
}
147+
138148
if ($node instanceof Equal) {
139149
return new Identical($node->left, $node->right);
140150
}

0 commit comments

Comments
 (0)