Skip to content

Commit aa64d0d

Browse files
committed
fix switch with default case
1 parent c4630fd commit aa64d0d

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

src/StaticAnalysis/ExecutableLinesFindingVisitor.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
use PhpParser\Node\Stmt\If_;
4343
use PhpParser\Node\Stmt\Property;
4444
use PhpParser\Node\Stmt\Return_;
45-
use PhpParser\Node\Stmt\Switch_;
4645
use PhpParser\Node\Stmt\Throw_;
4746
use PhpParser\Node\Stmt\Unset_;
4847
use PhpParser\Node\Stmt\While_;
@@ -240,7 +239,12 @@ private function getLines(NodeAbstract $node, bool $fromReturns): array
240239
if ($node instanceof If_ ||
241240
$node instanceof ElseIf_ ||
242241
$node instanceof While_ ||
243-
$node instanceof Do_) {
242+
$node instanceof Do_ ||
243+
$node instanceof Case_) {
244+
if ($node instanceof Case_ && null === $node->cond) {
245+
return [];
246+
}
247+
244248
return [$this->getNodeStartLine($node->cond)];
245249
}
246250

@@ -325,7 +329,6 @@ private function isExecutable(Node $node): bool
325329
$node instanceof PropertyFetch ||
326330
$node instanceof Return_ ||
327331
$node instanceof StaticPropertyFetch ||
328-
$node instanceof Switch_ ||
329332
$node instanceof Ternary ||
330333
$node instanceof Throw_ ||
331334
$node instanceof Unset_ ||

tests/_files/source_with_heavy_indentation.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,33 @@ public function noTryNoFinally(): int
180180
return $res;
181181
}
182182

183+
public function switch(): int
184+
{
185+
switch (
186+
'x'
187+
.
188+
mt_rand()
189+
. 'x'
190+
) {
191+
case
192+
<<<EOF
193+
x1x
194+
EOF:
195+
$res = 'a';
196+
197+
break;
198+
case 'x2x':
199+
case 'x3x':
200+
$res = 'a';
201+
202+
break;
203+
default:
204+
$res = 'c';
205+
}
206+
207+
return $res;
208+
}
209+
183210
private static $staticState = 1;
184211
private const CONST_STATE = 1.1;
185212
}

tests/tests/Data/RawCodeCoverageDataTest.php

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public function testInlineCommentsKeepTheLine(): void
330330
public function testHeavyIndentationIsHandledCorrectly(): void
331331
{
332332
$file = TEST_FILES_PATH . 'source_with_heavy_indentation.php';
333-
333+
$n = 27;
334334
$this->assertEquals(
335335
[
336336
14,
@@ -385,25 +385,36 @@ public function testHeavyIndentationIsHandledCorrectly(): void
385385
// line 176 is finally statement, not in xdebug output (only catch condition is covered)
386386
177,
387387
180,
388-
189,
389-
191,
388+
188,
390389
193,
391390
195,
392391
197,
392+
198,
393393
199,
394-
201,
395-
203,
396-
205,
394+
200,
395+
202,
396+
// line 203 is default case, not in xdebug output (only cases with condition are covered)
397+
204,
397398
207,
398-
229,
399-
234,
400-
238,
401-
241,
402-
245,
403-
249,
404-
255,
405-
259,
406-
265,
399+
189 + $n,
400+
191 + $n,
401+
193 + $n,
402+
195 + $n,
403+
197 + $n,
404+
199 + $n,
405+
201 + $n,
406+
203 + $n,
407+
205 + $n,
408+
207 + $n,
409+
229 + $n,
410+
234 + $n,
411+
238 + $n,
412+
241 + $n,
413+
245 + $n,
414+
249 + $n,
415+
255 + $n,
416+
259 + $n,
417+
265 + $n,
407418
],
408419
array_keys(RawCodeCoverageData::fromUncoveredFile($file, new ParsingFileAnalyser(true, true))->lineCoverage()[$file])
409420
);

0 commit comments

Comments
 (0)