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

[FEATURE] add codeclimate iteration zero #93

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
80 changes: 80 additions & 0 deletions src/Linter/ReportPrinter/CodeClimateReportPrinter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php declare(strict_types=1);
namespace Helmich\TypoScriptLint\Linter\ReportPrinter;

use DOMDocument;
use Helmich\TypoScriptLint\Application;
use Helmich\TypoScriptLint\Linter\Report\Report;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Report printer that generates CodeClimate JSON documents [1].
*
* These reports are well-suited for being used in continuous integration
* environments like gitlab [2].
*
* [1] https://github.com/codeclimate/spec/blob/master/SPEC.md
* [2] https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html
*
* @package Helmich\TypoScriptLint
* @subpackage Linter\ReportPrinter
*/
class CodeClimateReportPrinter implements Printer
{

/** @var OutputInterface */
private $output;

/**
* Constructs a new checkstyle report printer.
*
* @param OutputInterface $output Output stream to write on. Might be STDOUT or a file.
*/
public function __construct(OutputInterface $output)
{
$this->output = $output;
}

/**
* Writes a report in checkstyle XML format.
*
* @param Report $report The report to print.
* @return void
*/
public function writeReport(Report $report): void
{
$issues = [];

foreach ($report->getFiles() as $file) {
foreach ($file->getIssues() as $issue) {
$issueData = [
'type' => 'issue',
'check_name' => $issue->getSource(),
'description' => $issue->getMessage(),
'categories' => ['Style'],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if all checks fit into the Style category. For instance, the DuplicateAssignmentSniff might also fit into the Bug Risk category.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a hint how to connect your issue to the categorie? From your code I have not seen an entry point to extract an issue category.

'location' => [
'path' => $file->getFilename(),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not entirely sure -- I think that $file->getFilename() might possibly return the absolute file name, whereas the code climate spec requires the relative file name (to the working dir, presumably).

With #91 (still work-in-progress, however), there is a utility class Helmich\TypoScriptLint\Linter\ReportPrinter\PathUtils coming in that implements just this; maybe we could already pull that class into this PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, can you tell me, maybe it makes sense to expose an getAbsolutePath and getRelativePath function from the file object? You could store the pathes splitted in 2 parts and return that easily.

'lines' => [
'begin' => $issue->getLine() ? ((string) $issue->getLine()) : 0
kaystrobach marked this conversation as resolved.
Show resolved Hide resolved
]
]
];

$column = $issue->getColumn();
if ($column !== null) {
$issueData['location']['lines']['column'] = $column;
}

$issueData['fingerprint'] = $this->fingerprint($issueData);

$issues[] = $issueData;
}
}

$this->output->write(json_encode($issues));
}

protected function fingerprint(array $issue)
kaystrobach marked this conversation as resolved.
Show resolved Hide resolved
{
return md5(json_encode($issue));
}
}
6 changes: 5 additions & 1 deletion src/Logging/LinterLoggerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


use Helmich\TypoScriptLint\Linter\ReportPrinter\CheckstyleReportPrinter;
use Helmich\TypoScriptLint\Linter\ReportPrinter\CodeClimateReportPrinter;
use Helmich\TypoScriptLint\Linter\ReportPrinter\ConsoleReportPrinter;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -34,6 +35,9 @@ public function createLogger(string $outputFormat, OutputInterface $reportOutput
: $consoleOutput;

switch ($outputFormat) {
case 'json':
case 'codeclimate':
return new CompactConsoleLogger(new CodeClimateReportPrinter($reportOutput), $errorOutput);
case 'checkstyle':
case 'xml':
return new CompactConsoleLogger(new CheckstyleReportPrinter($reportOutput), $errorOutput);
Expand All @@ -46,4 +50,4 @@ public function createLogger(string $outputFormat, OutputInterface $reportOutput
throw new \InvalidArgumentException('Invalid report printer "' . $outputFormat . '"!');
}
}
}
}