Skip to content

Commit

Permalink
Merge pull request #8244 from jack-worman/CountReport
Browse files Browse the repository at this point in the history
New "Count" Report Format
  • Loading branch information
orklah committed Jul 11, 2022
2 parents 057d78d + 3b76ac8 commit 7f3d55d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Expand Up @@ -397,6 +397,7 @@ public static function getFileReportOptions(array $report_file_paths, bool $show
'.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) {
Expand Down
9 changes: 8 additions & 1 deletion src/Psalm/IssueBuffer.php
Expand Up @@ -16,11 +16,11 @@
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;
Expand Down Expand Up @@ -908,6 +908,13 @@ public static function getOutput(
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();
Expand Down
19 changes: 1 addition & 18 deletions src/Psalm/Report.php
Expand Up @@ -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>
Expand Down
39 changes: 39 additions & 0 deletions src/Psalm/Report/CountReport.php
@@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Psalm/Report/ReportOptions.php
Expand Up @@ -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;

Expand Down
20 changes: 20 additions & 0 deletions tests/ReportOutputTest.php
Expand Up @@ -1269,6 +1269,26 @@ public function testGithubActionsOutput(): void
);
}

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(
Expand Down

0 comments on commit 7f3d55d

Please sign in to comment.