Skip to content

Commit

Permalink
Add PHPStan and modify error handling in BadgeComposer
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
mohanrajpac committed Jun 1, 2024
1 parent a751cbf commit 2dfa014
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 4 deletions.
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"homepage": "https://github.com/codebtech/coveragebadge",
"require-dev": {
"ergebnis/composer-normalize": "^2.42",
"phpstan/phpstan": "^1.11",
"phpstan/phpstan-deprecation-rules": "^1.2",
"phpstan/phpstan-strict-rules": "^1.6",
"phpunit/phpunit": "^11.1",
"slevomat/coding-standard": "^8.15",
"squizlabs/php_codesniffer": "^3.10"
Expand Down Expand Up @@ -42,6 +45,7 @@
"lint": "phpcs",
"lint:fix": "phpcbf",
"php:badge": "bin/coverage-badge coverage/clover.xml badges/php.svg PHP",
"phpstan": "phpstan analyse --memory-limit=2048M",
"test": "phpunit --testdox --coverage-clover coverage/clover.xml --coverage-html coverage --coverage-filter src/"
}
}
156 changes: 155 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
level: max
paths:
- coverage-badge.php
- src/
excludePaths:
- tests/*
12 changes: 10 additions & 2 deletions src/BadgeComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
*/
class BadgeComposer
{
/**
* @var string[]
*/
private array $inputFiles;
private string $outputFile;
private string $coverageName;
private string $badgeTemplate = __DIR__ . '/../template/badge.svg';
private int $totalCoverage = 0;
private int $totalElements = 0;
private int $checkedElements = 0;
Expand Down Expand Up @@ -56,7 +60,7 @@ public function getTotalCoverage(): int
*
* This method checks if the input files exist and if the output file is provided.
*
* @param array $inputFiles The array of input files to validate.
* @param string[] $inputFiles The array of input files to validate.
* @param string $outputFile The output file to validate.
*
* @throws Exception If any of the input files do not exist or if the output file is not provided.
Expand Down Expand Up @@ -128,7 +132,11 @@ private function processFile(string $inputFile): void
private function finalizeCoverage(): void
{
$totalCoverage = $this->totalCoverage / count($this->inputFiles); // Average coverage across all files
$template = file_get_contents(__DIR__ . '/../template/badge.svg');
$template = file_get_contents($this->badgeTemplate);

if($template === false) {
throw new Exception('Error reading badge template file');
}

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

Expand Down
18 changes: 17 additions & 1 deletion tests/BadgeComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,15 @@ public function testItThrowsExceptionWhenFileProcessingFails(): void
/**
* @throws ReflectionException
*/
public function finalizeCoverage()
public function finalizeCoverage($manipuate = false)
{
$method = (new ReflectionClass($this->badgeComposer))->getMethod('finalizeCoverage');

if($manipuate) {
$property = (new ReflectionClass($this->badgeComposer))->getProperty('badgeTemplate');
$property->setValue($this->badgeComposer, 'invalid.svg');
}

return $method->invoke($this->badgeComposer);
}

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

/**
* @throws ReflectionException
*/
public function testFinalizeCoverageFailsWhenBadgeTemplateFileIsMissing(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Error reading badge template file');

$this->finalizeCoverage(true);
}

}

0 comments on commit 2dfa014

Please sign in to comment.