|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Symplify\EasyCodingStandard\Console\Output; |
| 6 | + |
| 7 | +use DOMDocument; |
| 8 | +use DOMElement; |
| 9 | +use RuntimeException; |
| 10 | +use Symplify\EasyCodingStandard\Console\ExitCode; |
| 11 | +use Symplify\EasyCodingStandard\Console\Style\EasyCodingStandardStyle; |
| 12 | +use Symplify\EasyCodingStandard\Contract\Console\Output\OutputFormatterInterface; |
| 13 | +use Symplify\EasyCodingStandard\ValueObject\Configuration; |
| 14 | +use Symplify\EasyCodingStandard\ValueObject\Error\ErrorAndDiffResult; |
| 15 | + |
| 16 | +/** |
| 17 | + * @see \Symplify\EasyCodingStandard\Tests\Console\Output\JsonOutputFormatterTest |
| 18 | + */ |
| 19 | +final readonly class CheckstyleOutputFormatter implements OutputFormatterInterface |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + public const NAME = 'checkstyle'; |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + private EasyCodingStandardStyle $easyCodingStandardStyle, |
| 28 | + private ExitCodeResolver $exitCodeResolver |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @return ExitCode::* |
| 34 | + */ |
| 35 | + public function report(ErrorAndDiffResult $errorAndDiffResult, Configuration $configuration): int |
| 36 | + { |
| 37 | + $checkstyleContent = $this->createCheckstyleContent($errorAndDiffResult); |
| 38 | + $this->easyCodingStandardStyle->writeln($checkstyleContent); |
| 39 | + |
| 40 | + return $this->exitCodeResolver->resolve($errorAndDiffResult, $configuration); |
| 41 | + } |
| 42 | + |
| 43 | + public function getName(): string |
| 44 | + { |
| 45 | + return self::NAME; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @api |
| 50 | + */ |
| 51 | + public function createCheckstyleContent(ErrorAndDiffResult $errorAndDiffResult): string |
| 52 | + { |
| 53 | + if (! \extension_loaded('dom')) { |
| 54 | + throw new RuntimeException('Cannot generate report! `ext-dom` is not available!'); |
| 55 | + } |
| 56 | + |
| 57 | + $domDocument = new DOMDocument('1.0', 'UTF-8'); |
| 58 | + |
| 59 | + /** @var DOMElement $checkstyleElement */ |
| 60 | + $checkstyleElement = $domDocument->appendChild($domDocument->createElement('checkstyle')); |
| 61 | + |
| 62 | + foreach ($errorAndDiffResult->getFileDiffs() as $fileDiff) { |
| 63 | + /** @var DOMElement $file */ |
| 64 | + $file = $checkstyleElement->appendChild($domDocument->createElement('file')); |
| 65 | + $file->setAttribute('name', $fileDiff->getRelativeFilePath()); |
| 66 | + |
| 67 | + foreach ($fileDiff->getAppliedCheckers() as $appliedChecker) { |
| 68 | + $errorElement = $this->createError($domDocument, $appliedChecker); |
| 69 | + $file->appendChild($errorElement); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + $domDocument->formatOutput = true; |
| 74 | + |
| 75 | + return (string) $domDocument->saveXML(); |
| 76 | + } |
| 77 | + |
| 78 | + private function createError(DOMDocument $domDocument, string $appliedChecker): DOMElement |
| 79 | + { |
| 80 | + $domElement = $domDocument->createElement('error'); |
| 81 | + $domElement->setAttribute('severity', 'warning'); |
| 82 | + $domElement->setAttribute('source', 'EasyCodingStandard.' . $appliedChecker); |
| 83 | + $domElement->setAttribute('message', 'Found violation(s) of type: ' . $appliedChecker); |
| 84 | + |
| 85 | + return $domElement; |
| 86 | + } |
| 87 | +} |
0 commit comments