Skip to content

Commit

Permalink
Fixed nested ternary
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 14, 2020
1 parent 4be8d5a commit ea43e81
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/Parser/NodeTokensVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public function enterNode(Node $node)
[$immediatePredecessor, $immediateSuccessor] = $this->getImmediates($parent, $myStart, $myEnd);
if ($immediatePredecessor !== null) {
$tokensBefore = [];
for ($i = $immediatePredecessor->getAttribute('endTokenPos') + 1; $i < $myStart; $i++) {
for ($i = $immediatePredecessor; $i < $myStart; $i++) {
$tokensBefore[] = $this->tokens[$i];
}
$node->setAttribute('tokensBefore', $tokensBefore);
}

if ($immediateSuccessor !== null) {
$tokensAfter = [];
for ($i = $myEnd + 1; $i < $immediateSuccessor->getAttribute('startTokenPos'); $i++) {
for ($i = $myEnd + 1; $i <= $immediateSuccessor; $i++) {
$tokensAfter[] = $this->tokens[$i];
}
$node->setAttribute('tokensAfter', $tokensAfter);
Expand All @@ -58,7 +58,7 @@ public function enterNode(Node $node)
* @param Node $parent
* @param int $myStart
* @param int $myEnd
* @return array{Node|null, Node|null}
* @return array{int|null, int|null}
*/
private function getImmediates(Node $parent, int $myStart, int $myEnd): array
{
Expand All @@ -71,17 +71,21 @@ private function getImmediates(Node $parent, int $myStart, int $myEnd): array
$parentChild = $parentChildList->getNode();
$childEnd = $parentChild->getAttribute('endTokenPos');
if ($childEnd < $myStart) {
$immediatePredecessor = $parentChild;
$immediatePredecessor = $childEnd + 1;
$parentChildList = $parentChildList->getNext();
continue;
}

$childStart = $parentChild->getAttribute('startTokenPos');
if ($childStart > $myEnd) {
$immediateSuccessor = $parentChild;
$immediateSuccessor = $childStart - 1;
break;
}

if ($childStart + 1 < $myStart && $childEnd > $myEnd) {
return [$childStart + 1, $childEnd];
}

$parentChildList = $parentChildList->getNext();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ public function testReportOnPhp80(): void
]);
}

public function testBug(): void
{
$this->phpVersion = new PhpVersion(80000);
$this->analyse([__DIR__ . '/data/require-parentheses-bug.php'], []);
}

}
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Ternary/data/require-parentheses-bug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace RequireTernaryParenthesesBug;

function getPremiseName(PartnerProfilePremise $premise): string
{
$address = $premise->hasAddress() ? $premise->getAddress() : null;

return $address !== null && $address->getCity() !== null && $address->getStreet() !== null
? sprintf('%s, %s, %s', $premise->getName(), $address->getStreet(), $address->getCity())
: ($address !== null && $address->getCity() !== null
? sprintf('%s, %s', $premise->getName(), $address->getCity())
: $premise->getName());
}

0 comments on commit ea43e81

Please sign in to comment.