Skip to content

Commit ea43e81

Browse files
committed
Fixed nested ternary
1 parent 4be8d5a commit ea43e81

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

src/Parser/NodeTokensVisitor.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ public function enterNode(Node $node)
3636
[$immediatePredecessor, $immediateSuccessor] = $this->getImmediates($parent, $myStart, $myEnd);
3737
if ($immediatePredecessor !== null) {
3838
$tokensBefore = [];
39-
for ($i = $immediatePredecessor->getAttribute('endTokenPos') + 1; $i < $myStart; $i++) {
39+
for ($i = $immediatePredecessor; $i < $myStart; $i++) {
4040
$tokensBefore[] = $this->tokens[$i];
4141
}
4242
$node->setAttribute('tokensBefore', $tokensBefore);
4343
}
4444

4545
if ($immediateSuccessor !== null) {
4646
$tokensAfter = [];
47-
for ($i = $myEnd + 1; $i < $immediateSuccessor->getAttribute('startTokenPos'); $i++) {
47+
for ($i = $myEnd + 1; $i <= $immediateSuccessor; $i++) {
4848
$tokensAfter[] = $this->tokens[$i];
4949
}
5050
$node->setAttribute('tokensAfter', $tokensAfter);
@@ -58,7 +58,7 @@ public function enterNode(Node $node)
5858
* @param Node $parent
5959
* @param int $myStart
6060
* @param int $myEnd
61-
* @return array{Node|null, Node|null}
61+
* @return array{int|null, int|null}
6262
*/
6363
private function getImmediates(Node $parent, int $myStart, int $myEnd): array
6464
{
@@ -71,17 +71,21 @@ private function getImmediates(Node $parent, int $myStart, int $myEnd): array
7171
$parentChild = $parentChildList->getNode();
7272
$childEnd = $parentChild->getAttribute('endTokenPos');
7373
if ($childEnd < $myStart) {
74-
$immediatePredecessor = $parentChild;
74+
$immediatePredecessor = $childEnd + 1;
7575
$parentChildList = $parentChildList->getNext();
7676
continue;
7777
}
7878

7979
$childStart = $parentChild->getAttribute('startTokenPos');
8080
if ($childStart > $myEnd) {
81-
$immediateSuccessor = $parentChild;
81+
$immediateSuccessor = $childStart - 1;
8282
break;
8383
}
8484

85+
if ($childStart + 1 < $myStart && $childEnd > $myEnd) {
86+
return [$childStart + 1, $childEnd];
87+
}
88+
8589
$parentChildList = $parentChildList->getNext();
8690
}
8791

tests/PHPStan/Rules/Ternary/RequireParenthesesForNestedTernaryRuleTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,10 @@ public function testReportOnPhp80(): void
4848
]);
4949
}
5050

51+
public function testBug(): void
52+
{
53+
$this->phpVersion = new PhpVersion(80000);
54+
$this->analyse([__DIR__ . '/data/require-parentheses-bug.php'], []);
55+
}
56+
5157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace RequireTernaryParenthesesBug;
4+
5+
function getPremiseName(PartnerProfilePremise $premise): string
6+
{
7+
$address = $premise->hasAddress() ? $premise->getAddress() : null;
8+
9+
return $address !== null && $address->getCity() !== null && $address->getStreet() !== null
10+
? sprintf('%s, %s, %s', $premise->getName(), $address->getStreet(), $address->getCity())
11+
: ($address !== null && $address->getCity() !== null
12+
? sprintf('%s, %s', $premise->getName(), $address->getCity())
13+
: $premise->getName());
14+
}

0 commit comments

Comments
 (0)