Skip to content

Add PHPStan and modify error handling in BadgeComposer #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 18 additions & 1 deletion tests/BadgeComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,18 @@ public function testItThrowsExceptionWhenFileProcessingFails(): void
}

/**
* @param bool $manipulate
* @throws ReflectionException
*/
public function finalizeCoverage()
public function finalizeCoverage($manipulate = false)
{
$method = (new ReflectionClass($this->badgeComposer))->getMethod('finalizeCoverage');

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

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

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

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

$this->finalizeCoverage(true);
}

}