Skip to content

Commit 2dfa014

Browse files
committed
Add PHPStan and modify error handling in BadgeComposer
This commit adds PHPStan and its related strict rules and deprecation rules to the `composer.json`. It also modifies the `finalizeCoverage` method in `BadgeComposer` to handle errors when badge template file is missing. A test for this scenario has been added to `BadgeComposerTest.php`.
1 parent a751cbf commit 2dfa014

File tree

5 files changed

+193
-4
lines changed

5 files changed

+193
-4
lines changed

composer.json

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
"homepage": "https://github.com/codebtech/coveragebadge",
1313
"require-dev": {
1414
"ergebnis/composer-normalize": "^2.42",
15+
"phpstan/phpstan": "^1.11",
16+
"phpstan/phpstan-deprecation-rules": "^1.2",
17+
"phpstan/phpstan-strict-rules": "^1.6",
1518
"phpunit/phpunit": "^11.1",
1619
"slevomat/coding-standard": "^8.15",
1720
"squizlabs/php_codesniffer": "^3.10"
@@ -42,6 +45,7 @@
4245
"lint": "phpcs",
4346
"lint:fix": "phpcbf",
4447
"php:badge": "bin/coverage-badge coverage/clover.xml badges/php.svg PHP",
48+
"phpstan": "phpstan analyse --memory-limit=2048M",
4549
"test": "phpunit --testdox --coverage-clover coverage/clover.xml --coverage-html coverage --coverage-filter src/"
4650
}
4751
}

composer.lock

+155-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan.neon

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
parameters:
2+
level: max
3+
paths:
4+
- coverage-badge.php
5+
- src/
6+
excludePaths:
7+
- tests/*

src/BadgeComposer.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222
*/
2323
class BadgeComposer
2424
{
25+
/**
26+
* @var string[]
27+
*/
2528
private array $inputFiles;
2629
private string $outputFile;
2730
private string $coverageName;
31+
private string $badgeTemplate = __DIR__ . '/../template/badge.svg';
2832
private int $totalCoverage = 0;
2933
private int $totalElements = 0;
3034
private int $checkedElements = 0;
@@ -56,7 +60,7 @@ public function getTotalCoverage(): int
5660
*
5761
* This method checks if the input files exist and if the output file is provided.
5862
*
59-
* @param array $inputFiles The array of input files to validate.
63+
* @param string[] $inputFiles The array of input files to validate.
6064
* @param string $outputFile The output file to validate.
6165
*
6266
* @throws Exception If any of the input files do not exist or if the output file is not provided.
@@ -128,7 +132,11 @@ private function processFile(string $inputFile): void
128132
private function finalizeCoverage(): void
129133
{
130134
$totalCoverage = $this->totalCoverage / count($this->inputFiles); // Average coverage across all files
131-
$template = file_get_contents(__DIR__ . '/../template/badge.svg');
135+
$template = file_get_contents($this->badgeTemplate);
136+
137+
if($template === false) {
138+
throw new Exception('Error reading badge template file');
139+
}
132140

133141
$template = str_replace('{{ total }}', (string) $totalCoverage, $template);
134142

tests/BadgeComposerTest.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,15 @@ public function testItThrowsExceptionWhenFileProcessingFails(): void
112112
/**
113113
* @throws ReflectionException
114114
*/
115-
public function finalizeCoverage()
115+
public function finalizeCoverage($manipuate = false)
116116
{
117117
$method = (new ReflectionClass($this->badgeComposer))->getMethod('finalizeCoverage');
118118

119+
if($manipuate) {
120+
$property = (new ReflectionClass($this->badgeComposer))->getProperty('badgeTemplate');
121+
$property->setValue($this->badgeComposer, 'invalid.svg');
122+
}
123+
119124
return $method->invoke($this->badgeComposer);
120125
}
121126

@@ -132,4 +137,15 @@ public function testFinalizeCoverageCalculatesAverageCoverage(): void
132137
$this->assertStringContainsString('unit', $outputFileContent);
133138
}
134139

140+
/**
141+
* @throws ReflectionException
142+
*/
143+
public function testFinalizeCoverageFailsWhenBadgeTemplateFileIsMissing(): void
144+
{
145+
$this->expectException(Exception::class);
146+
$this->expectExceptionMessage('Error reading badge template file');
147+
148+
$this->finalizeCoverage(true);
149+
}
150+
135151
}

0 commit comments

Comments
 (0)