-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new output formatter for checkstyle (#204)
* Add new output formatter for checkstyle * Simplify progress output check for JSON and Checkstyle outputs * Add composer suggest for ext-dom
- Loading branch information
Showing
6 changed files
with
164 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
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
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,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\EasyCodingStandard\Console\Output; | ||
|
||
use DOMDocument; | ||
use DOMElement; | ||
use RuntimeException; | ||
use Symplify\EasyCodingStandard\Console\ExitCode; | ||
use Symplify\EasyCodingStandard\Console\Style\EasyCodingStandardStyle; | ||
use Symplify\EasyCodingStandard\Contract\Console\Output\OutputFormatterInterface; | ||
use Symplify\EasyCodingStandard\ValueObject\Configuration; | ||
use Symplify\EasyCodingStandard\ValueObject\Error\ErrorAndDiffResult; | ||
|
||
/** | ||
* @see \Symplify\EasyCodingStandard\Tests\Console\Output\JsonOutputFormatterTest | ||
*/ | ||
final readonly class CheckstyleOutputFormatter implements OutputFormatterInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const NAME = 'checkstyle'; | ||
|
||
public function __construct( | ||
private EasyCodingStandardStyle $easyCodingStandardStyle, | ||
private ExitCodeResolver $exitCodeResolver | ||
) { | ||
} | ||
|
||
/** | ||
* @return ExitCode::* | ||
*/ | ||
public function report(ErrorAndDiffResult $errorAndDiffResult, Configuration $configuration): int | ||
{ | ||
$checkstyleContent = $this->createCheckstyleContent($errorAndDiffResult); | ||
$this->easyCodingStandardStyle->writeln($checkstyleContent); | ||
|
||
return $this->exitCodeResolver->resolve($errorAndDiffResult, $configuration); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return self::NAME; | ||
} | ||
|
||
/** | ||
* @api | ||
*/ | ||
public function createCheckstyleContent(ErrorAndDiffResult $errorAndDiffResult): string | ||
{ | ||
if (! \extension_loaded('dom')) { | ||
throw new RuntimeException('Cannot generate report! `ext-dom` is not available!'); | ||
} | ||
|
||
$domDocument = new DOMDocument('1.0', 'UTF-8'); | ||
|
||
/** @var DOMElement $checkstyleElement */ | ||
$checkstyleElement = $domDocument->appendChild($domDocument->createElement('checkstyle')); | ||
|
||
foreach ($errorAndDiffResult->getFileDiffs() as $fileDiff) { | ||
/** @var DOMElement $file */ | ||
$file = $checkstyleElement->appendChild($domDocument->createElement('file')); | ||
$file->setAttribute('name', $fileDiff->getRelativeFilePath()); | ||
|
||
foreach ($fileDiff->getAppliedCheckers() as $appliedChecker) { | ||
$errorElement = $this->createError($domDocument, $appliedChecker); | ||
$file->appendChild($errorElement); | ||
} | ||
} | ||
|
||
$domDocument->formatOutput = true; | ||
|
||
return (string) $domDocument->saveXML(); | ||
} | ||
|
||
private function createError(DOMDocument $domDocument, string $appliedChecker): DOMElement | ||
{ | ||
$domElement = $domDocument->createElement('error'); | ||
$domElement->setAttribute('severity', 'warning'); | ||
$domElement->setAttribute('source', 'EasyCodingStandard.' . $appliedChecker); | ||
$domElement->setAttribute('message', 'Found violation(s) of type: ' . $appliedChecker); | ||
|
||
return $domElement; | ||
} | ||
} |
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
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,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\EasyCodingStandard\Tests\Console\Output; | ||
|
||
use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer; | ||
use Symplify\EasyCodingStandard\Console\Formatter\ColorConsoleDiffFormatter; | ||
use Symplify\EasyCodingStandard\Console\Output\CheckstyleOutputFormatter; | ||
use Symplify\EasyCodingStandard\FileSystem\StaticRelativeFilePathHelper; | ||
use Symplify\EasyCodingStandard\Testing\PHPUnit\AbstractTestCase; | ||
use Symplify\EasyCodingStandard\ValueObject\Error\ErrorAndDiffResult; | ||
use Symplify\EasyCodingStandard\ValueObject\Error\FileDiff; | ||
|
||
final class CheckstyleOutputFormatterTest extends AbstractTestCase | ||
{ | ||
private CheckstyleOutputFormatter $checkstyleOutputFormatter; | ||
|
||
private ColorConsoleDiffFormatter $colorConsoleDiffFormatter; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->checkstyleOutputFormatter = $this->make(CheckstyleOutputFormatter::class); | ||
$this->colorConsoleDiffFormatter = $this->make(ColorConsoleDiffFormatter::class); | ||
} | ||
|
||
public function test(): void | ||
{ | ||
$relativeFilePath = StaticRelativeFilePathHelper::resolveFromCwd(__DIR__ . '/Source/RandomFile.php'); | ||
|
||
$fileDiffs = []; | ||
|
||
$diff = 'some diff'; | ||
$fileDiffs[] = new FileDiff( | ||
$relativeFilePath, | ||
$diff, | ||
$this->colorConsoleDiffFormatter->format($diff), | ||
[LineLengthFixer::class] | ||
); | ||
|
||
$diff = 'some other diff'; | ||
$fileDiffs[] = new FileDiff( | ||
$relativeFilePath, | ||
$diff, | ||
$this->colorConsoleDiffFormatter->format($diff), | ||
[LineLengthFixer::class] | ||
); | ||
|
||
$errorAndDiffResult = new ErrorAndDiffResult([], $fileDiffs, []); | ||
|
||
$checkstyleContent = $this->checkstyleOutputFormatter->createCheckstyleContent($errorAndDiffResult); | ||
$this->assertStringMatchesFormatFile( | ||
__DIR__ . '/Fixture/expected_checkstyle_output.xml', | ||
$checkstyleContent . PHP_EOL | ||
); | ||
} | ||
} |
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="tests/Console/Output/Source/RandomFile.php"> | ||
<error severity="warning" source="EasyCodingStandard.Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer" message="Found violation(s) of type: Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer"/> | ||
</file> | ||
<file name="tests/Console/Output/Source/RandomFile.php"> | ||
<error severity="warning" source="EasyCodingStandard.Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer" message="Found violation(s) of type: Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer"/> | ||
</file> | ||
</checkstyle> |