Skip to content

Commit 8edc6e1

Browse files
Fix the count of statements
1 parent bb82c42 commit 8edc6e1

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/Lib/IO/MetricsFactory.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use DigitalRevolution\CodeCoverageInspection\Model\Metric\MethodMetric;
99
use DOMDocument;
1010
use DOMNode;
11+
use DOMNodeList;
1112
use DOMXPath;
1213

1314
class MetricsFactory
@@ -75,14 +76,15 @@ public static function getFileMetrics(DOMDocument $document): array
7576
$filename = str_replace('\\', '/', (string)XMLUtil::getAttribute($parentNode, 'name'));
7677

7778
// calculate coverage
78-
$statements = count($xpath->query('line[@type="stmt"]', $parentNode));
79+
$statementsNodes = $xpath->query('line[@type="stmt"]', $parentNode);
80+
$statements = $statementsNodes === false ? 0 : count($statementsNodes);
7981
$coveredStatements = (int)XMLUtil::getAttribute($domMetric, 'coveredstatements');
8082
$coveragePercentage = $statements === 0 ? 100 : round($coveredStatements / $statements * 100, self::COVERAGE_PERCENTAGE_PRECISION);
8183

8284
// gather metrics per method
8385
$methodMetrics = self::getMethodMetrics($xpath, $parentNode);
8486

85-
$coveredStatements = self::getCoveredStatements($xpath, $parentNode);
87+
$coveredStatements = self::getCoveredStatements($statementsNodes);
8688

8789
$metrics[$filename] = new FileMetric($filename, $statements, $coveragePercentage, $methodMetrics, $coveredStatements);
8890
}
@@ -116,9 +118,8 @@ public static function getMethodMetrics(DOMXPath $xpath, DOMNode $fileNode): arr
116118
/**
117119
* @return int[]
118120
*/
119-
private static function getCoveredStatements(DOMXPath $xpath, DOMNode $fileNode): array
121+
private static function getCoveredStatements(DOMNodeList|false $statementNodes): array
120122
{
121-
$statementNodes = $xpath->query('line[@type="stmt"]', $fileNode);
122123
if ($statementNodes === false || count($statementNodes) === 0) {
123124
return [];
124125
}

tests/Unit/Lib/IO/MetricsFactoryTest.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function testGetMethodMetrics(): void
6666
<metrics loc="11" ncloc="11" classes="0"
6767
methods="0" coveredmethods="0"
6868
conditionals="0" coveredconditionals="0"
69-
statements="50" coveredstatements="1"
69+
statements="5" coveredstatements="1"
7070
elements="0" coveredelements="0"/>
7171
<line num="29" type="method" name="isFoobar" visibility="public" complexity="1" crap="1" count="1"/>
7272
<line num="31" type="stmt" count="0"/>
@@ -164,24 +164,33 @@ public static function multiFilesDataProvider(): array
164164
'first 100 %' => [
165165
'<file name="utTestFile.php">
166166
<metrics loc="11" ncloc="11" statements="1" coveredstatements="1"/>
167+
<line num="1" type="method" name="methodName" count="1"/>
168+
<line num="2" type="stmt" count="1"/>
167169
</file>',
168170
' <file name="utTestFile.php">
169171
<metrics loc="11" ncloc="11" statements="0" coveredstatements="0"/>
172+
<line num="1" type="method" name="methodName" count="1"/>
170173
</file>',
171-
0,
174+
1,
172175
100.0,
173-
[]
176+
[2]
174177
],
175178
'second 100%' => [
176179
'<file name="utTestFile.php">
177180
<metrics loc="11" ncloc="11" statements="2" coveredstatements="0"/>
181+
<line num="1" type="method" name="methodName" count="1"/>
182+
<line num="2" type="stmt" count="0"/>
183+
<line num="3" type="stmt" count="0"/>
178184
</file>',
179185
'<file name="utTestFile.php">
180186
<metrics loc="11" ncloc="11" statements="2" coveredstatements="2"/>
187+
<line num="1" type="method" name="methodName" count="1"/>
188+
<line num="2" type="stmt" count="1"/>
189+
<line num="3" type="stmt" count="1"/>
181190
</file>',
182-
0,
191+
1,
183192
100.0,
184-
[]
193+
[2, 3]
185194
]
186195
];
187196
}

0 commit comments

Comments
 (0)