Skip to content

Commit e87bf49

Browse files
committed
Support branch coverage for anonymous functions
At this moment XDebug only tracks anonymous functions inside of methods or functions (not global)
1 parent d679f67 commit e87bf49

File tree

7 files changed

+30
-19
lines changed

7 files changed

+30
-19
lines changed

src/CodeCoverage/Report/Node/File.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,18 @@ function ($carry, $value) {
694694
protected function calcAndApplyClassAggregate(&$classOrTrait, $classOrTraitName)
695695
{
696696
foreach ($classOrTrait['methods'] as &$method) {
697-
$methodCoveragePath = $classOrTraitName . '->' . $method['methodName'];
697+
if ($method['methodName'] === 'anonymous function') {
698+
// Locate index
699+
$methodCoveragePath = $method['methodName'];
700+
foreach ($this->coverageData['branches'] as $index => $branch) {
701+
if ($method['startLine'] === $branch[0]['line_start']) {
702+
$methodCoveragePath = $index;
703+
}
704+
}
705+
706+
} else {
707+
$methodCoveragePath = $classOrTraitName . '->' . $method['methodName'];
708+
}
698709
if (isset($this->coverageData['paths'][$methodCoveragePath])) {
699710
$methodPaths = $this->coverageData['paths'][$methodCoveragePath];
700711
$this->calcPathsAggregate($methodPaths, $numExecutablePaths, $numExecutedPaths);

tests/_files/Report/HTML/CoverageForClassWithAnonymousFunction/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
</div>
5959
</td>
6060
<td class="danger small"><div align="right">0.00%</div></td>
61-
<td class="danger small"><div align="right">0&nbsp;/&nbsp;1</div></td>
61+
<td class="danger small"><div align="right">0&nbsp;/&nbsp;2</div></td>
6262
<td class="danger big"> <div class="progress">
6363
<div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="50.00" aria-valuemin="0" aria-valuemax="100" style="width: 50.00%">
6464
<span class="sr-only">50.00% covered (danger)</span>
@@ -94,7 +94,7 @@
9494
</div>
9595
</td>
9696
<td class="danger small"><div align="right">0.00%</div></td>
97-
<td class="danger small"><div align="right">0&nbsp;/&nbsp;1</div></td>
97+
<td class="danger small"><div align="right">0&nbsp;/&nbsp;2</div></td>
9898
<td class="danger big"> <div class="progress">
9999
<div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="50.00" aria-valuemin="0" aria-valuemax="100" style="width: 50.00%">
100100
<span class="sr-only">50.00% covered (danger)</span>

tests/_files/Report/HTML/CoverageForClassWithAnonymousFunction/source_with_class_and_anonymous_function.php.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
</div>
6868
</td>
6969
<td class="danger small"><div align="right">0.00%</div></td>
70-
<td class="danger small"><div align="right">0&nbsp;/&nbsp;1</div></td>
70+
<td class="danger small"><div align="right">0&nbsp;/&nbsp;2</div></td>
7171
<td class="warning big"> <div class="progress">
7272
<div class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="87.50" aria-valuemin="0" aria-valuemax="100" style="width: 87.50%">
7373
<span class="sr-only">87.50% covered (warning)</span>
@@ -104,7 +104,7 @@
104104
</div>
105105
</td>
106106
<td class="danger small"><div align="right">0.00%</div></td>
107-
<td class="danger small"><div align="right">0&nbsp;/&nbsp;1</div></td>
107+
<td class="danger small"><div align="right">0&nbsp;/&nbsp;2</div></td>
108108
<td class="warning big"> <div class="progress">
109109
<div class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="87.50" aria-valuemin="0" aria-valuemax="100" style="width: 87.50%">
110110
<span class="sr-only">87.50% covered (warning)</span>
@@ -155,14 +155,14 @@
155155
<td class="success small"><div align="right">100.00%</div></td>
156156
<td class="success small"><div align="right">1&nbsp;/&nbsp;1</div></td>
157157
<td class="success small">1</td>
158-
<td class="success big"> <div class="progress">
159-
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%">
160-
<span class="sr-only">100.00% covered (success)</span>
158+
<td class="danger big"> <div class="progress">
159+
<div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="0.00" aria-valuemin="0" aria-valuemax="100" style="width: 0.00%">
160+
<span class="sr-only">0.00% covered (danger)</span>
161161
</div>
162162
</div>
163163
</td>
164-
<td class="success small"><div align="right">100.00%</div></td>
165-
<td class="success small"><div align="right">0&nbsp;/&nbsp;0</div></td>
164+
<td class="danger small"><div align="right">0.00%</div></td>
165+
<td class="danger small"><div align="right">0&nbsp;/&nbsp;1</div></td>
166166
<td class="success big"> <div class="progress">
167167
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%">
168168
<span class="sr-only">100.00% covered (success)</span>

tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<directory name="%s">
88
<totals>
99
<lines total="19" comments="2" code="17" executable="8" executed="7" percent="87.50%"/>
10-
<paths count="1" tested="0" percent="0.00%"/>
10+
<paths count="2" tested="0" percent="0.00%"/>
1111
<methods count="2" tested="1" percent="50.00%"/>
1212
<functions count="0" tested="0" percent=""/>
1313
<classes count="1" tested="0" percent="0.00%"/>
@@ -16,7 +16,7 @@
1616
<file name="source_with_class_and_anonymous_function.php" href="source_with_class_and_anonymous_function.php.xml">
1717
<totals>
1818
<lines total="19" comments="2" code="17" executable="8" executed="7" percent="87.50%"/>
19-
<paths count="1" tested="0" percent="0.00%"/>
19+
<paths count="2" tested="0" percent="0.00%"/>
2020
<methods count="2" tested="1" percent="50.00%"/>
2121
<functions count="0" tested="0" percent=""/>
2222
<classes count="1" tested="0" percent="0.00%"/>

tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/source_with_class_and_anonymous_function.php.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<file name="source_with_class_and_anonymous_function.php">
44
<totals>
55
<lines total="19" comments="2" code="17" executable="8" executed="7" percent="87.50%"/>
6-
<paths count="1" tested="0" percent="0.00%"/>
6+
<paths count="2" tested="0" percent="0.00%"/>
77
<methods count="2" tested="1" percent="50.00%"/>
88
<functions count="0" tested="0" percent=""/>
99
<classes count="1" tested="0" percent="0.00%"/>
@@ -13,7 +13,7 @@
1313
<package full="" name="" sub="" category=""/>
1414
<namespace name=""/>
1515
<method name="runAnonymous" signature="runAnonymous()" start="5" end="18" crap="1.04" executable="3" executed="2" coverage="66.666666666667" executablePaths="1" executedPaths="0"/>
16-
<method name="anonymous function" signature="anonymous function (&amp;$val, $key)" start="11" end="13" crap="1" executable="2" executed="2" coverage="100" executablePaths="0" executedPaths="0"/>
16+
<method name="anonymous function" signature="anonymous function (&amp;$val, $key)" start="11" end="13" crap="1" executable="2" executed="2" coverage="100" executablePaths="1" executedPaths="0"/>
1717
</class>
1818
<coverage>
1919
<line nr="7">

tests/_files/class-with-anonymous-function-clover.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<project timestamp="%i">
44
<file name="%s/source_with_class_and_anonymous_function.php">
55
<class name="CoveredClassWithAnonymousFunctionInStaticMethod" namespace="global">
6-
<metrics complexity="2" methods="2" coveredmethods="1" conditionals="1" coveredconditionals="0" statements="5" coveredstatements="4" elements="7" coveredelements="5"/>
6+
<metrics complexity="2" methods="2" coveredmethods="1" conditionals="2" coveredconditionals="0" statements="5" coveredstatements="4" elements="7" coveredelements="5"/>
77
</class>
88
<line num="5" type="method" name="runAnonymous" visibility="public" complexity="1" crap="1.04" count="1"/>
99
<line num="7" type="stmt" count="1"/>
@@ -15,8 +15,8 @@
1515
<line num="14" type="stmt" count="1"/>
1616
<line num="17" type="stmt" count="1"/>
1717
<line num="18" type="stmt" count="1"/>
18-
<metrics loc="19" ncloc="17" classes="1" methods="2" coveredmethods="1" conditionals="1" coveredconditionals="0" statements="8" coveredstatements="7" elements="10" coveredelements="8"/>
18+
<metrics loc="19" ncloc="17" classes="1" methods="2" coveredmethods="1" conditionals="2" coveredconditionals="0" statements="8" coveredstatements="7" elements="10" coveredelements="8"/>
1919
</file>
20-
<metrics files="1" loc="19" ncloc="17" classes="1" methods="2" coveredmethods="1" conditionals="1" coveredconditionals="0" statements="8" coveredstatements="7" elements="10" coveredelements="8"/>
20+
<metrics files="1" loc="19" ncloc="17" classes="1" methods="2" coveredmethods="1" conditionals="2" coveredconditionals="0" statements="8" coveredstatements="7" elements="10" coveredelements="8"/>
2121
</project>
2222
</coverage>

tests/_files/class-with-anonymous-function-text.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Code Coverage Report:
77
Classes: 0.00% (0/1)
88
Methods: 50.00% (1/2)
99
Lines: 87.50% (7/8)
10-
Paths: 0.00% (0/1)
10+
Paths: 0.00% (0/2)
1111

1212
CoveredClassWithAnonymousFunctionInStaticMethod
13-
Methods: 50.00% ( 1/ 2) Lines: 80.00% ( 4/ 5) Paths: 0.00% ( 0/ 1)
13+
Methods: 50.00% ( 1/ 2) Lines: 80.00% ( 4/ 5) Paths: 0.00% ( 0/ 2)

0 commit comments

Comments
 (0)