1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00

Merge pull request #8244 from jack-worman/CountReport

New "Count" Report Format
This commit is contained in:
orklah 2022-07-11 18:55:01 +02:00 committed by GitHub
commit 7f3d55dfa0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 20 deletions

View File

@ -397,6 +397,7 @@ class ProjectAnalyzer
'.pylint' => Report::TYPE_PYLINT,
'.console' => Report::TYPE_CONSOLE,
'.sarif' => Report::TYPE_SARIF,
'count.txt' => Report::TYPE_COUNT,
];
foreach ($report_file_paths as $report_file_path) {

View File

@ -16,11 +16,11 @@ use Psalm\Issue\MixedIssue;
use Psalm\Issue\TaintedInput;
use Psalm\Issue\UnusedPsalmSuppress;
use Psalm\Plugin\EventHandler\Event\AfterAnalysisEvent;
use Psalm\Report;
use Psalm\Report\CheckstyleReport;
use Psalm\Report\CodeClimateReport;
use Psalm\Report\CompactReport;
use Psalm\Report\ConsoleReport;
use Psalm\Report\CountReport;
use Psalm\Report\EmacsReport;
use Psalm\Report\GithubActionsReport;
use Psalm\Report\JsonReport;
@ -908,6 +908,13 @@ class IssueBuffer
case Report::TYPE_CODECLIMATE:
$output = new CodeClimateReport($normalized_data, self::$fixable_issue_counts, $report_options);
break;
case Report::TYPE_COUNT:
$output = new CountReport($normalized_data, self::$fixable_issue_counts, $report_options);
break;
default:
throw new RuntimeException('Unexpected report format: ' . $report_options->format);
}
return $output->create();

View File

@ -28,24 +28,7 @@ abstract class Report
public const TYPE_PHP_STORM = 'phpstorm';
public const TYPE_SARIF = 'sarif';
public const TYPE_CODECLIMATE = 'codeclimate';
public const SUPPORTED_OUTPUT_TYPES = [
self::TYPE_COMPACT,
self::TYPE_CONSOLE,
self::TYPE_PYLINT,
self::TYPE_JSON,
self::TYPE_JSON_SUMMARY,
self::TYPE_SONARQUBE,
self::TYPE_EMACS,
self::TYPE_XML,
self::TYPE_JUNIT,
self::TYPE_CHECKSTYLE,
self::TYPE_TEXT,
self::TYPE_GITHUB_ACTIONS,
self::TYPE_PHP_STORM,
self::TYPE_SARIF,
self::TYPE_CODECLIMATE,
];
public const TYPE_COUNT = 'count';
/**
* @var array<int, IssueData>

View File

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Psalm\Report;
use Psalm\Report;
use function array_key_exists;
use function uksort;
class CountReport extends Report
{
public function create(): string
{
$issue_type_counts = [];
foreach ($this->issues_data as $issue_data) {
if (array_key_exists($issue_data->type, $issue_type_counts)) {
$issue_type_counts[$issue_data->type]++;
} else {
$issue_type_counts[$issue_data->type] = 1;
}
}
uksort($issue_type_counts, function (string $a, string $b) use ($issue_type_counts): int {
$cmp_result = $issue_type_counts[$a] <=> $issue_type_counts[$b];
if ($cmp_result === 0) {
return $a <=> $b;
} else {
return $cmp_result;
}
});
$output = '';
foreach ($issue_type_counts as $issue_type => $count) {
$output .= "{$issue_type}: {$count}\n";
}
return $output;
}
}

View File

@ -22,7 +22,7 @@ class ReportOptions
public $show_info = true;
/**
* @var value-of<Report::SUPPORTED_OUTPUT_TYPES>
* @var Report::TYPE_*
*/
public $format = Report::TYPE_CONSOLE;

View File

@ -1269,6 +1269,26 @@ EOF;
);
}
public function testCountOutput(): void
{
$this->analyzeFileForReport();
$report_options = new ReportOptions();
$report_options->format = Report::TYPE_COUNT;
$expected_output = <<<'EOF'
MixedInferredReturnType: 1
MixedReturnStatement: 1
PossiblyUndefinedGlobalVariable: 1
UndefinedConstant: 1
UndefinedVariable: 1
EOF;
$this->assertSame(
$expected_output,
IssueBuffer::getOutput(IssueBuffer::getIssuesData(), $report_options)
);
}
public function testEmptyReportIfNotError(): void
{
$this->addFile(