-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/analyzer/Report/Checkstyle/CheckstyleReportGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
tests/Analyzer/Report/Checkstyle/CheckstyleReportGeneratorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "tombstone("2020-01-01", "Class2")" was called by "invoker2" and 1 more caller" line="11"/> | ||
</file> | ||
<file name="%sfixtures%ssource%sfunctions.php"> | ||
<error severity="error" source="Tombstone.Analyzer.undead" message="Tombstone "tombstone("2020-01-01", "globalScope")" was called by "invoker1"" line="10"/> | ||
</file> | ||
</checkstyle> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters