Skip to content

Commit 7c0aeea

Browse files
authored
[CodeQuality] Preserve assignment parentheses in NegatedAndsToPositiveOrsRector (#9011)
1 parent e9fc2fd commit 7c0aeea

2 files changed

Lines changed: 63 additions & 4 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\BooleanNot\NegatedAndsToPositiveOrsRector\Fixture;
4+
5+
class AssignmentComparison
6+
{
7+
public function run($enabled, $cache, $id)
8+
{
9+
if (! ($enabled && ($value = $this->value()) !== null)) {
10+
return null;
11+
}
12+
13+
if (! ($enabled && ($registro = $cache[$id] ?? null) !== null)) {
14+
return null;
15+
}
16+
17+
if (! (($value = $this->value()) !== null && $enabled)) {
18+
return null;
19+
}
20+
}
21+
22+
private function value()
23+
{
24+
return rand(0, 1) ? 'value' : null;
25+
}
26+
}
27+
28+
?>
29+
-----
30+
<?php
31+
32+
namespace Rector\Tests\CodeQuality\Rector\BooleanNot\NegatedAndsToPositiveOrsRector\Fixture;
33+
34+
class AssignmentComparison
35+
{
36+
public function run($enabled, $cache, $id)
37+
{
38+
if (!$enabled || ($value = $this->value()) === null) {
39+
return null;
40+
}
41+
42+
if (!$enabled || ($registro = $cache[$id] ?? null) === null) {
43+
return null;
44+
}
45+
46+
if (($value = $this->value()) === null || !$enabled) {
47+
return null;
48+
}
49+
}
50+
51+
private function value()
52+
{
53+
return rand(0, 1) ? 'value' : null;
54+
}
55+
}
56+
57+
?>

‎src/PhpParser/Printer/BetterStandardPrinter.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,10 +525,12 @@ private function wrapBinaryOpWithBrackets(Node $node): void
525525
$node->right->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true);
526526
}
527527

528-
if ($node->left instanceof Assign
529-
&& $this->origTokens instanceof TokenStream
530-
&& ! $this->origTokens->haveParens($node->left->getStartTokenPos(), $node->left->getEndTokenPos())) {
531-
$node->left->setAttribute(AttributeKey::ORIGINAL_NODE, null);
528+
if ($node->left instanceof Assign && $this->origTokens instanceof TokenStream) {
529+
if ($this->origTokens->haveParens($node->left->getStartTokenPos(), $node->left->getEndTokenPos())) {
530+
$node->left->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true);
531+
} else {
532+
$node->left->setAttribute(AttributeKey::ORIGINAL_NODE, null);
533+
}
532534
}
533535

534536
if ($node->right instanceof Assign && $this->origTokens instanceof TokenStream) {

0 commit comments

Comments
 (0)