Skip to content

Commit 4ec24e9

Browse files
authored
Merge pull request #18 from codebtech/feat/phpstan
Add PHPStan and modify error handling in BadgeComposer
2 parents a751cbf + bad3abf commit 4ec24e9

File tree

5 files changed

+194
-4
lines changed

5 files changed

+194
-4
lines changed

composer.json

Lines changed: 4 additions & 0 deletions
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

Lines changed: 155 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan.neon

Lines changed: 7 additions & 0 deletions
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

Lines changed: 10 additions & 2 deletions
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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,18 @@ public function testItThrowsExceptionWhenFileProcessingFails(): void
110110
}
111111

112112
/**
113+
* @param bool $manipulate
113114
* @throws ReflectionException
114115
*/
115-
public function finalizeCoverage()
116+
public function finalizeCoverage($manipulate = false)
116117
{
117118
$method = (new ReflectionClass($this->badgeComposer))->getMethod('finalizeCoverage');
118119

120+
if($manipulate) {
121+
$property = (new ReflectionClass($this->badgeComposer))->getProperty('badgeTemplate');
122+
$property->setValue($this->badgeComposer, 'invalid.svg');
123+
}
124+
119125
return $method->invoke($this->badgeComposer);
120126
}
121127

@@ -132,4 +138,15 @@ public function testFinalizeCoverageCalculatesAverageCoverage(): void
132138
$this->assertStringContainsString('unit', $outputFileContent);
133139
}
134140

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

0 commit comments

Comments
 (0)