Skip to content

Commit ac3e8f0

Browse files
Slamdunksebastianbergmann
authored andcommitted
Added switch
1 parent 307d07a commit ac3e8f0

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/StaticAnalysis/ExecutableLinesFindingVisitor.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,22 @@ public function enterNode(Node $node): void
5252
if ($node instanceof Node\Stmt\If_ ||
5353
$node instanceof Node\Stmt\ElseIf_ ||
5454
$node instanceof Node\Stmt\Else_ ||
55+
$node instanceof Node\Stmt\Case_ ||
5556
$node instanceof Node\Stmt\For_ ||
5657
$node instanceof Node\Stmt\Foreach_ ||
5758
$node instanceof Node\Stmt\While_) {
59+
$incrementNextBranch = false;
60+
5861
if (isset($this->executableLinesGroupedByBranch[$node->getStartLine()])) {
5962
$stmtBranch = 1 + $this->executableLinesGroupedByBranch[$node->getStartLine()];
6063

6164
if (false !== array_search($stmtBranch, $this->executableLinesGroupedByBranch, true)) {
62-
$stmtBranch = ++$this->nextBranch;
65+
$stmtBranch = 1 + $this->nextBranch;
66+
$incrementNextBranch = true;
6367
}
6468
} else {
65-
$stmtBranch = ++$this->nextBranch;
69+
$stmtBranch = 1 + $this->nextBranch;
70+
$incrementNextBranch = true;
6671
}
6772

6873
$endLine = $node->getEndLine();
@@ -89,18 +94,29 @@ public function enterNode(Node $node): void
8994
);
9095
$contentEnd = $endLine;
9196

97+
if ($node instanceof Node\Stmt\Case_) {
98+
$contentEnd++;
99+
}
100+
92101
end($node->stmts);
93102
$lastNode = current($node->stmts);
94103
reset($node->stmts);
95104

96-
if ($lastNode instanceof Node\Stmt\Nop) {
105+
if (
106+
$lastNode instanceof Node\Stmt\Nop ||
107+
$lastNode instanceof Node\Stmt\Break_
108+
) {
97109
$contentEnd = $lastNode->getEndLine() + 1;
98110
}
99111

100112
if (1 > ($contentEnd - $contentStart)) {
101113
return;
102114
}
103115

116+
if ($incrementNextBranch) {
117+
$this->nextBranch++;
118+
}
119+
104120
$this->setLineBranch(
105121
$contentStart,
106122
$contentEnd - 1,

tests/_files/source_for_branched_exec_lines.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,34 @@ public function withDoWhile() // +5
189189
) // 0
190190
; // 0
191191
} // 0
192+
public function withSwitch() // +1
193+
{ // 0
194+
$var = 1; // 0
195+
switch ($var) { // 0
196+
case 0: // 0
197+
case 1: // 0
198+
++$var; // +1
199+
break; // 0
200+
case 2: // -1
201+
++$var; // +2
202+
case 3: // -2
203+
++$var; // +3
204+
break; // 0
205+
default: // -3
206+
++$var; // +4
207+
} // -4
208+
switch ($var): // 0
209+
case 0: // 0
210+
case 1: // 0
211+
++$var; // +5
212+
break; // 0
213+
case 2: // -5
214+
++$var; // +6
215+
case 3: // -6
216+
++$var; // +7
217+
break; // 0
218+
default: // -7
219+
++$var; // +8
220+
endswitch; // -8
221+
} // 0
192222
}

0 commit comments

Comments
 (0)