Skip to content

Commit 76ba107

Browse files
committed
Add path coverage to each line
1 parent 9e63c72 commit 76ba107

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

src/CodeCoverage.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ public function append(array $data, $id = null, $append = true, $linesToBeCovere
336336
$lineData = &$this->data[$file]['lines'][$line];
337337
if ($lineData === null) {
338338
$lineData = [
339-
'tests' => [$id],
339+
'pathCovered' => false,
340+
'tests' => [$id],
340341
];
341342
} elseif (!in_array($id, $lineData['tests'])) {
342343
$lineData['tests'][] = $id;
@@ -601,6 +602,7 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
601602

602603
foreach ($functionData['branches'] as $branch) {
603604
$this->data[$file]['branches'][$functionName][] = [
605+
'hit' => $branch['hit'],
604606
'line_start' => $branch['line_start'],
605607
'line_end' => $branch['line_end'],
606608
'tests' => []
@@ -618,10 +620,21 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
618620
$this->data[$file]['lines'][$lineNumber] = null;
619621
} else {
620622
$this->data[$file]['lines'][$lineNumber] = [
621-
'tests' => [],
623+
'pathCovered' => false,
624+
'tests' => [],
622625
];
623626
}
624627
}
628+
629+
foreach ($this->data[$file]['branches'] as $function) {
630+
foreach ($function as $branch) {
631+
for ($i = $branch['line_start']; $i < $branch['line_end']; $i++) {
632+
if (isset($this->data[$file]['lines'][$i])) {
633+
$this->data[$file]['lines'][$i]['pathCovered'] = (bool) $branch['hit'];
634+
}
635+
}
636+
}
637+
}
625638
}
626639
}
627640
}

tests/TestCase.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -392,47 +392,57 @@ protected function getExpectedDataArrayForBankAccount()
392392
TEST_FILES_PATH . 'BankAccount.php' => [
393393
'lines' => [
394394
8 => [
395-
'tests' => [
395+
'pathCovered' => true,
396+
'tests' => [
396397
0 => 'BankAccountTest::testBalanceIsInitiallyZero',
397398
1 => 'BankAccountTest::testDepositWithdrawMoney',
398399
],
399400
],
400401
9 => null,
401402
13 => [
402-
'tests' => [],
403+
'pathCovered' => false,
404+
'tests' => [],
403405
],
404406
14 => [
405-
'tests' => [],
407+
'pathCovered' => false,
408+
'tests' => [],
406409
],
407410
15 => [
408-
'tests' => [],
411+
'pathCovered' => false,
412+
'tests' => [],
409413
],
410414
16 => [
411-
'tests' => [],
415+
'pathCovered' => false,
416+
'tests' => [],
412417
],
413418
18 => [
414-
'tests' => [],
419+
'pathCovered' => false,
420+
'tests' => [],
415421
],
416422
22 => [
417-
'tests' => [
423+
'pathCovered' => false,
424+
'tests' => [
418425
0 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
419426
1 => 'BankAccountTest::testDepositWithdrawMoney',
420427
],
421428
],
422429
24 => [
423-
'tests' => [
430+
'pathCovered' => false,
431+
'tests' => [
424432
0 => 'BankAccountTest::testDepositWithdrawMoney',
425433
],
426434
],
427435
25 => null,
428436
29 => [
429-
'tests' => [
437+
'pathCovered' => false,
438+
'tests' => [
430439
0 => 'BankAccountTest::testBalanceCannotBecomeNegative',
431440
1 => 'BankAccountTest::testDepositWithdrawMoney',
432441
],
433442
],
434443
31 => [
435-
'tests' => [
444+
'pathCovered' => false,
445+
'tests' => [
436446
0 => 'BankAccountTest::testDepositWithdrawMoney',
437447
],
438448
],
@@ -444,42 +454,49 @@ protected function getExpectedDataArrayForBankAccount()
444454
'line_start' => 20,
445455
'line_end' => 25,
446456
'tests' => [],
457+
'hit' => 0,
447458
],
448459
],
449460
'BankAccount->getBalance' => [
450461
0 => [
451462
'line_start' => 6,
452463
'line_end' => 9,
453464
'tests' => [],
465+
'hit' => 1,
454466
],
455467
],
456468
'BankAccount->withdrawMoney' => [
457469
0 => [
458470
'line_start' => 27,
459471
'line_end' => 32,
460472
'tests' => [],
473+
'hit' => 0,
461474
],
462475
],
463476
'BankAccount->setBalance' => [
464477
0 => [
465478
'line_start' => 11,
466479
'line_end' => 13,
467480
'tests' => [],
481+
'hit' => 0,
468482
],
469483
1 => [
470484
'line_start' => 14,
471485
'line_end' => 15,
472486
'tests' => [],
487+
'hit' => 0,
473488
],
474489
2 => [
475490
'line_start' => 16,
476491
'line_end' => 16,
477492
'tests' => [],
493+
'hit' => 0,
478494
],
479495
3 => [
480496
'line_start' => 18,
481497
'line_end' => 18,
482498
'tests' => [],
499+
'hit' => 0,
483500
],
484501
],
485502
],

0 commit comments

Comments
 (0)