Skip to content

Commit e109841

Browse files
Slamdunksebastianbergmann
authored andcommitted
Added return
1 parent d36e95b commit e109841

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

src/StaticAnalysis/ExecutableLinesFindingVisitor.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function enterNode(Node $node): void
4444
if ($node instanceof Node\Stmt\Function_ ||
4545
$node instanceof Node\Stmt\ClassMethod
4646
) {
47-
$this->setLineBranch($node->getStartLine(), $node->getEndLine(), ++$this->nextBranch);
47+
$this->setLineBranch($node->getStartLine(), $node->getEndLine() - 1, ++$this->nextBranch);
4848

4949
return;
5050
}
@@ -138,8 +138,30 @@ public function enterNode(Node $node): void
138138
return;
139139
}
140140

141+
if ($node instanceof Node\Stmt\Return_) {
142+
$returnBranch = $this->executableLinesGroupedByBranch[$node->getStartLine()];
143+
$returnEndLine = $node->getEndLine();
144+
$nextBranch = null;
145+
146+
foreach ($this->executableLinesGroupedByBranch as $line => $branch) {
147+
if ($line <= $returnEndLine || $branch !== $returnBranch) {
148+
continue;
149+
}
150+
151+
if (null === $nextBranch) {
152+
$nextBranch = ++$this->nextBranch;
153+
}
154+
155+
$this->executableLinesGroupedByBranch[$line] = $nextBranch;
156+
}
157+
158+
return;
159+
}
160+
141161
if ($node instanceof Node\Stmt\Declare_) {
142162
$this->unsets[] = range($node->getStartLine(), $node->getEndLine());
163+
164+
return;
143165
}
144166

145167
if ($node instanceof Node\Identifier) {

tests/_files/source_for_branched_exec_lines.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
function simple() // +1
1414
{ // 0
1515
return 1; // 0
16-
} // 0
16+
}
1717

1818
$var2 = 1; // -1
1919

@@ -31,7 +31,7 @@ function withIf() // +3
3131
$var += 2; // +1
3232
} // -1
3333
return $var; // 0
34-
} // 0
34+
}
3535

3636
class MyClass
3737
{
@@ -48,7 +48,7 @@ function // 0
4848
if (false) { // 0
4949
$var += 2; // +1
5050
} // -1
51-
} // 0
51+
}
5252
public function withForeach() // +2
5353
{ // 0
5454
$var = 1; // 0
@@ -73,7 +73,7 @@ public function withForeach() // +2
7373
{ // 0
7474
$var += 2; // +4
7575
} // -4
76-
} // 0
76+
}
7777
public function withWhile() // +5
7878
{ // 0
7979
$var = 1; // 0
@@ -96,7 +96,7 @@ public function withWhile() // +5
9696
{ // 0
9797
++$var; // +4
9898
} // -4
99-
} // 0
99+
}
100100
public function withIfElseifElse() // +5
101101
{ // 0
102102
$var = 1; // 0
@@ -140,7 +140,7 @@ public function withIfElseifElse() // +5
140140
{ // 0
141141
++$var; // +12
142142
} // -12
143-
} // 0
143+
}
144144
public function withFor() // +13
145145
{ // 0
146146
$var = 1; // 0
@@ -163,7 +163,7 @@ public function withFor() // +13
163163
{ // 0
164164
$var += 2; // +4
165165
} // -4
166-
} // 0
166+
}
167167
public function withDoWhile() // +5
168168
{ // 0
169169
$var = 1; // 0
@@ -188,7 +188,7 @@ public function withDoWhile() // +5
188188
$var // 0
189189
) // 0
190190
; // 0
191-
} // 0
191+
}
192192
public function withSwitch() // +1
193193
{ // 0
194194
$var = 1; // 0
@@ -218,7 +218,7 @@ public function withSwitch() // +1
218218
default: // -7
219219
++$var; // +8
220220
endswitch; // -8
221-
} // 0
221+
}
222222
public function withMatch() // +9
223223
{ // 0
224224
$var = 1; // 0
@@ -248,5 +248,21 @@ public function withMatch() // +9
248248
, // -6
249249
} // 0
250250
; // 0
251-
} // 0
251+
}
252+
public function withReturn() // +7
253+
{ // 0
254+
$var = 1; // 0
255+
if (false) { // 0
256+
++$var; // +1
257+
return // 0
258+
$var // 0
259+
; // 0
260+
++$var; // +1
261+
if (false) { // 0
262+
++$var; // +1
263+
} // -1
264+
} // -2
265+
return; // 0
266+
++$var; // +4
267+
}
252268
}

0 commit comments

Comments
 (0)