Skip to content

Commit

Permalink
Add checkstyle report generator #8
Browse files Browse the repository at this point in the history
  • Loading branch information
scheb committed Aug 11, 2020
1 parent d437322 commit 0421ab9
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"require": {
"php": "^7.1.3",
"ext-json": "*",
"ext-dom": "*",
"nikic/php-parser": "^4.0",
"phpunit/php-text-template": "^1.2.1|^2.0",
"psr/log": "^1.0",
Expand Down
88 changes: 88 additions & 0 deletions src/analyzer/Report/Checkstyle/CheckstyleReportGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace Scheb\Tombstone\Analyzer\Report\Checkstyle;

use Scheb\Tombstone\Analyzer\Model\AnalyzerResult;
use Scheb\Tombstone\Analyzer\Report\ReportGeneratorInterface;
use Scheb\Tombstone\Core\Model\Tombstone;

class CheckstyleReportGenerator implements ReportGeneratorInterface
{
/**
* @var string
*/
private $filePath;

public function __construct(string $filePath)
{
$this->filePath = $filePath;
}

public function getName(): string
{
return 'Checkstyle';
}

public function generate(AnalyzerResult $result): void
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$rootNode = $dom->appendChild($dom->createElement('checkstyle'));

foreach ($result->getFileResults() as $fileResult) {
if ($fileResult->getUndeadCount() > 0) {
/** @var \DOMElement $fileNode */
$fileNode = $rootNode->appendChild($dom->createElement('file'));
$fileNode->setAttribute('name', $fileResult->getFile()->getAbsolutePath());

foreach ($fileResult->getUndead() as $tombstone) {
$errorNode = $this->createError($dom, $tombstone);
$fileNode->appendChild($errorNode);
}
}
}

$dom->formatOutput = true;

file_put_contents($this->filePath, $dom->saveXML());
}

private function createError(\DOMDocument $dom, Tombstone $tombstone): \DOMElement
{
$error = $dom->createElement('error');
$error->setAttribute('severity', 'error');
$error->setAttribute('source', 'Tombstone.Analyzer.undead');
$error->setAttribute('message', $this->getMessage($tombstone));
$error->setAttribute('line', (string) $tombstone->getLine());

return $error;
}

private function getMessage(Tombstone $tombstone): string
{
return sprintf('Tombstone "%s" was called', (string) $tombstone).$this->getCalledBy($tombstone);
}

/**
* @psalm-type list<string|null>
*/
private function getCalledBy(Tombstone $tombstone): string
{
$vampires = $tombstone->getVampires();
$numVampires = \count($vampires);
if (0 === $numVampires) {
return '';
}

$invoker = array_shift($vampires)->getInvoker();
$calledBy = sprintf(' by "%s"', $invoker ?: 'global scope');

$numAdditionalVampires = $numVampires - 1;
if ($numAdditionalVampires > 0) {
$calledBy .= ' and '.$numAdditionalVampires.' more caller'.($numAdditionalVampires > 1 ? 's' : '');
}

return $calledBy;
}
}
1 change: 1 addition & 0 deletions src/analyzer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
],
"require": {
"php": "^7.1.3",
"ext-dom": "*",
"nikic/php-parser": "^4.0",
"phpunit/php-text-template": "^1.2.1|^2.0",
"scheb/tombstone-core": "self.version",
Expand Down
29 changes: 29 additions & 0 deletions tests/Analyzer/Report/Checkstyle/CheckstyleReportGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Scheb\Tombstone\Tests\Analyzer\Report\Checkstyle;

use Scheb\Tombstone\Analyzer\Report\Checkstyle\CheckstyleReportGenerator;
use Scheb\Tombstone\Tests\Analyzer\Report\fixtures\AnalyzerResultFixture;
use Scheb\Tombstone\Tests\TestCase;

class CheckstyleReportGeneratorTest extends TestCase
{
private const EXPORT_FILE = __DIR__.'/checkstyle.actual.xml';

/**
* @test
*/
public function generate_resultGiven_generateXmlFile(): void
{
$result = AnalyzerResultFixture::getAnalyzerResult();

$generator = new CheckstyleReportGenerator(self::EXPORT_FILE);
$generator->generate($result);

$this->assertFileExists(self::EXPORT_FILE);
$output = file_get_contents(self::EXPORT_FILE);
$this->assertStringMatchesFormatFile(__DIR__.'/checkstyle.xml', $output);
}
}
9 changes: 9 additions & 0 deletions tests/Analyzer/Report/Checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<file name="%sfixtures%ssource%sBar%sClass2.php">
<error severity="error" source="Tombstone.Analyzer.undead" message="Tombstone &quot;tombstone(&quot;2020-01-01&quot;, &quot;Class2&quot;)&quot; was called by &quot;invoker2&quot; and 1 more caller" line="11"/>
</file>
<file name="%sfixtures%ssource%sfunctions.php">
<error severity="error" source="Tombstone.Analyzer.undead" message="Tombstone &quot;tombstone(&quot;2020-01-01&quot;, &quot;globalScope&quot;)&quot; was called by &quot;invoker1&quot;" line="10"/>
</file>
</checkstyle>
6 changes: 4 additions & 2 deletions tests/Analyzer/Report/fixtures/AnalyzerResultFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ public static function getAnalyzerResult(): AnalyzerResult
$vampire1 = new Vampire('2020-02-01', 'invoker1', new StackTrace(), $globalScope, []);
$globalScope->addVampire($vampire1);
$vampire2 = new Vampire('2020-02-01', 'invoker2', new StackTrace(), $class2Tombstone, []);
$vampire3 = new Vampire('2020-02-01', 'invoker3', new StackTrace(), $class2Tombstone, []);
$class2Tombstone->addVampire($vampire2);
$vampire3 = new Vampire('2020-02-01', 'invoker3', new StackTrace(), $deletedTombstone, []);
$class2Tombstone->addVampire($vampire3);
$vampire4 = new Vampire('2020-02-01', 'invoker4', new StackTrace(), $deletedTombstone, []);

$deadList = [$functionTombstone, $class1Tombstone, $class3Tombstone];
$undeadList = [$globalScope, $class2Tombstone];
$deletedList = [$vampire3];
$deletedList = [$vampire4];

return new AnalyzerResult($deadList, $undeadList, $deletedList);
}
Expand Down

0 comments on commit 0421ab9

Please sign in to comment.