Skip to content
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

Add checkstyle formatter #271

Merged
merged 6 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
"symplify/easy-coding-standard": "^6.0",
"thecodingmachine/phpstan-strict-rules": "^0.11.0"
},
"suggest": {
"ext-simplexml": "*"
bu4ak marked this conversation as resolved.
Show resolved Hide resolved
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
Expand Down
82 changes: 82 additions & 0 deletions src/Application/Console/Formatters/Checkstyle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace NunoMaduro\PhpInsights\Application\Console\Formatters;

use Exception;
use NunoMaduro\PhpInsights\Application\Console\Contracts\Formatter;
use NunoMaduro\PhpInsights\Domain\Contracts\HasDetails;
use NunoMaduro\PhpInsights\Domain\Details;
use NunoMaduro\PhpInsights\Domain\Insights\Insight;
use NunoMaduro\PhpInsights\Domain\Insights\InsightCollection;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @internal
*/
final class Checkstyle implements Formatter
{
/** @var OutputInterface */
private $output;

public function __construct(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
}

/**
* Format the result to the desired format.
*
* @param InsightCollection $insightCollection
* @param string $dir
* @param array<string> $metrics
*/
public function format(
InsightCollection $insightCollection,
string $dir,
array $metrics
): void
{
$checkstyle = new \SimpleXMLElement('<checkstyle/>');
bu4ak marked this conversation as resolved.
Show resolved Hide resolved

foreach ($metrics as $metricClass) {
/** @var Insight $insight */
foreach ($insightCollection->allFrom(new $metricClass()) as $insight) {
if (! $insight instanceof HasDetails || ! $insight->hasIssue()) {
continue;
}

/** @var Details $detail */
foreach ($insight->getDetails() as $detail) {
$fileName = $this->getFileName($detail, $dir);

if (isset($checkstyle->file) && (string) $checkstyle->file->attributes()['name'] === $fileName) {
$file = $checkstyle->file;
} else {
$file = $checkstyle->addChild('file');
$file->addAttribute('name', $fileName);
}

$error = $file->addChild('error');
$error->addAttribute('severity', 'error');
$error->addAttribute('source', $insight->getTitle());
$error->addAttribute('line', $detail->hasLine() ? (string) $detail->getLine() : '');
$error->addAttribute('message', $detail->hasMessage() ? $detail->getMessage() : '');
}
}
}

$this->output->write((string) $checkstyle->asXML());
}

private function getFileName(Details $detail, string $dir): string
{
if ($detail->hasFile()) {
/** replacement is necessary because relative paths are needed */
return str_replace($dir . '/', '', $detail->getFile());
}
return '';
}
}
1 change: 1 addition & 0 deletions src/Application/Console/Formatters/FormatResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ final class FormatResolver
private static $formatters = [
'console' => Console::class,
'json' => Json::class,
'checkstyle' => Checkstyle::class,
];

public static function resolve(
Expand Down