Skip to content

Commit 9b8dba5

Browse files
authored
Merge pull request #1 from ggilder/remove-redundant-elements-calculation
Remove redundant elements calculation
2 parents 537ab3a + ad618e0 commit 9b8dba5

File tree

5 files changed

+21
-14
lines changed

5 files changed

+21
-14
lines changed

.github/workflows/run-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ jobs:
4545
run: composer test
4646

4747
- name: Upload coverage
48+
if: github.ref == 'refs/heads/main'
4849
env:
4950
COVERAGE_KEY: ${{ secrets.COVERAGE_KEY }}
5051
COVERAGE_HOST: ${{ secrets.COVERAGE_HOST }}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"php-parallel-lint/php-parallel-lint": "^1.3",
1212
"phpstan/phpstan": "^1.10",
1313
"phpunit/phpunit": "^10.0 || ^11.0",
14+
"robiningelbrecht/phpunit-coverage-tools": "^1.8",
1415
"squizlabs/php_codesniffer": "^3.6"
1516
},
1617
"autoload": {
@@ -27,7 +28,7 @@
2728
"parallel-lint --exclude vendor ."
2829
],
2930
"phpunit": [
30-
"XDEBUG_MODE=coverage phpunit --coverage-clover coverage.clover tests"
31+
"XDEBUG_MODE=coverage phpunit -d --min-coverage=100 --coverage-clover coverage.clover tests"
3132
],
3233
"sniff": [
3334
"phpcs --standard=PSR12 src/ tests/"

phpunit.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@
99
<directory suffix=".php">./src</directory>
1010
</include>
1111
</source>
12+
<extensions>
13+
<bootstrap class="RobinIngelbrecht\PHPUnitCoverageTools\PhpUnitExtension">
14+
<parameter name="exitOnLowCoverage" value="1" />
15+
</bootstrap>
16+
</extensions>
1217
</phpunit>

src/CloverParser.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44

55
use SimpleXMLElement;
66

7+
/**
8+
* Calculation reference https://confluence.atlassian.com/pages/viewpage.action?pageId=79986990
9+
* So in order to calculate the Total Percentage Coverage metric using data from an XML report
10+
* you have to use the following equation:
11+
*
12+
* TPC = (coveredconditionals + coveredstatements + coveredmethods) / (conditionals + statements + methods)
13+
*
14+
* Using elements is incorrect as according to that page notes, "elements" is conditionals + statements,
15+
* which means including them in the calculation would double-count statements.
16+
*/
717
class CloverParser
818
{
919
/** @var array<string> */
@@ -52,14 +62,12 @@ protected function reset(): void
5262
'methods' => 0,
5363
'conditionals' => 0,
5464
'statements' => 0,
55-
'elements' => 0,
5665
];
5766

5867
$this->coveredTotals = [
5968
'coveredmethods' => 0,
6069
'coveredconditionals' => 0,
6170
'coveredstatements' => 0,
62-
'coveredelements' => 0,
6371
];
6472

6573
$this->hasCalculatedTotals = false;
@@ -77,13 +85,6 @@ public function calculateTotals(): self
7785

7886
continue;
7987
}
80-
81-
foreach ($project->xpath('//file') as $file) {
82-
// no other way of seeing if the element exists?
83-
if ($file->metrics->count() > 0) {
84-
$this->incrementTotalsWithMetrics($file->metrics);
85-
}
86-
}
8788
}
8889
}
8990

@@ -105,8 +106,6 @@ protected function incrementTotalsWithMetrics(SimpleXMLElement $metrics): void
105106
$this->coveredTotals['coveredconditionals'] += (int) $metrics['coveredconditionals'];
106107
$this->totals['statements'] += (int) $metrics['statements'];
107108
$this->coveredTotals['coveredstatements'] += (int) $metrics['coveredstatements'];
108-
$this->totals['elements'] += (int) $metrics['elements'];
109-
$this->coveredTotals['coveredelements'] += (int) $metrics['coveredelements'];
110109
}
111110

112111
public function getPercentage(): float

tests/CloverParserTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ public function testCalculationFromProject(): void
7979
$this->assertSame(77.54237288135593, $parser->getPercentage());
8080
}
8181

82-
public function testCalculationFromFiles(): void
82+
public function testCalculationMissingProjectMetrics(): void
8383
{
8484
$parser = new CloverParser();
8585

86+
// This is not a realistic file; clover metrics should always have top-level project metrics
8687
$clover = <<<ENDCLOVER
8788
<?xml version="1.0" encoding="UTF-8"?>
8889
<coverage generated="1618905787">
@@ -114,7 +115,7 @@ public function testCalculationFromFiles(): void
114115

115116
$parser->addFile($path);
116117

117-
$this->assertSame(20.51282051282051, $parser->getPercentage());
118+
$this->assertSame(0.0, $parser->getPercentage());
118119
}
119120

120121
public function testCalculationAvoidsDivisionByZero(): void

0 commit comments

Comments
 (0)