Skip to content

Commit

Permalink
Merge pull request #9846 from lptn/use-consts-for-issue-severity
Browse files Browse the repository at this point in the history
Introduce and use `IssueData` constants for severity levels
  • Loading branch information
orklah committed May 31, 2023
2 parents 6b94453 + 3700ab6 commit 5b2efad
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 46 deletions.
7 changes: 7 additions & 0 deletions src/Psalm/Internal/Analyzer/IssueData.php
Expand Up @@ -11,6 +11,12 @@
*/
class IssueData
{
public const SEVERITY_INFO = 'info';
public const SEVERITY_ERROR = 'error';

/**
* @var self::SEVERITY_*
*/
public string $severity;

public int $line_from;
Expand Down Expand Up @@ -93,6 +99,7 @@ class IssueData
public ?string $dupe_key = null;

/**
* @param self::SEVERITY_* $severity
* @param ?list<DataFlowNodeData|array{label: string, entry_path_type: string}> $taint_trace
* @param ?list<DataFlowNodeData> $other_references
*/
Expand Down
14 changes: 7 additions & 7 deletions src/Psalm/Internal/Codebase/Analyzer.php
Expand Up @@ -1552,13 +1552,13 @@ private function taskDoneClosure(array $issues): void
$has_info = false;

foreach ($issues as $issue) {
if ($issue->severity === 'error') {
$has_error = true;
break;
}

if ($issue->severity === 'info') {
$has_info = true;
switch ($issue->severity) {
case IssueData::SEVERITY_INFO:
$has_info = true;
break;
default:
$has_error = true;
break;
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/Psalm/Internal/LanguageServer/LanguageServer.php
Expand Up @@ -728,10 +728,9 @@ function (IssueData $issue_data): Diagnostic {
new Position($end_line - 1, $end_column - 1),
);
switch ($severity) {
case Config::REPORT_INFO:
case IssueData::SEVERITY_INFO:
$diagnostic_severity = DiagnosticSeverity::WARNING;
break;
case Config::REPORT_ERROR:
default:
$diagnostic_severity = DiagnosticSeverity::ERROR;
break;
Expand Down Expand Up @@ -788,7 +787,7 @@ function (IssueData $issue_data): Diagnostic {
);

if ($position !== false) {
$issue_data->severity = Config::REPORT_INFO;
$issue_data->severity = IssueData::SEVERITY_INFO;
/** @psalm-suppress MixedArgument */
array_splice($issue_baseline[$file][$type]['s'], $position, 1);
/** @psalm-suppress MixedArrayAssignment, MixedOperand, MixedAssignment */
Expand All @@ -797,7 +796,7 @@ function (IssueData $issue_data): Diagnostic {
} else {
/** @psalm-suppress MixedArrayAssignment */
$issue_baseline[$file][$type]['s'] = [];
$issue_data->severity = Config::REPORT_INFO;
$issue_data->severity = IssueData::SEVERITY_INFO;
/** @psalm-suppress MixedArrayAssignment, MixedOperand, MixedAssignment */
$issue_baseline[$file][$type]['o']--;
}
Expand All @@ -806,7 +805,7 @@ function (IssueData $issue_data): Diagnostic {
}, $data[$file_path] ?? []),
function (IssueData $issue_data) {
//Hide Warnings
if ($issue_data->severity === Config::REPORT_INFO &&
if ($issue_data->severity === IssueData::SEVERITY_INFO &&
$this->client->clientConfiguration->hideWarnings
) {
return false;
Expand Down
3 changes: 3 additions & 0 deletions src/Psalm/Issue/CodeIssue.php
Expand Up @@ -68,6 +68,9 @@ public static function getIssueType(): string
return array_pop($fqcn_parts);
}

/**
* @param IssueData::SEVERITY_* $severity
*/
public function toIssueData(string $severity): IssueData
{
$location = $this->code_location;
Expand Down
10 changes: 5 additions & 5 deletions src/Psalm/IssueBuffer.php
Expand Up @@ -302,7 +302,7 @@ public static function add(CodeIssue $e, bool $is_fixable = false): bool

if ($reporting_level === Config::REPORT_INFO) {
if ($is_tainted || !self::alreadyEmitted($emitted_key)) {
self::$issues_data[$e->getFilePath()][] = $e->toIssueData(Config::REPORT_INFO);
self::$issues_data[$e->getFilePath()][] = $e->toIssueData(IssueData::SEVERITY_INFO);

if ($is_fixable) {
self::addFixableIssue($issue_type);
Expand Down Expand Up @@ -331,7 +331,7 @@ public static function add(CodeIssue $e, bool $is_fixable = false): bool

if ($is_tainted || !self::alreadyEmitted($emitted_key)) {
++self::$error_count;
self::$issues_data[$e->getFilePath()][] = $e->toIssueData(Config::REPORT_ERROR);
self::$issues_data[$e->getFilePath()][] = $e->toIssueData(IssueData::SEVERITY_ERROR);

if ($is_fixable) {
self::addFixableIssue($issue_type);
Expand Down Expand Up @@ -598,13 +598,13 @@ public static function finish(
);

if ($position !== false) {
$issue_data->severity = Config::REPORT_INFO;
$issue_data->severity = IssueData::SEVERITY_INFO;
array_splice($issue_baseline[$file][$type]['s'], $position, 1);
$issue_baseline[$file][$type]['o']--;
}
} else {
$issue_baseline[$file][$type]['s'] = [];
$issue_data->severity = Config::REPORT_INFO;
$issue_data->severity = IssueData::SEVERITY_INFO;
$issue_baseline[$file][$type]['o']--;
}
}
Expand All @@ -618,7 +618,7 @@ public static function finish(
foreach ($issues as $issue_name => $issue) {
if ($issue['o'] !== 0) {
$issues_data[$file_path][] = new IssueData(
Config::REPORT_ERROR,
IssueData::SEVERITY_ERROR,
0,
0,
UnusedBaselineEntry::getIssueType(),
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Plugin/Shepherd.php
Expand Up @@ -123,7 +123,7 @@ private static function collectPayloadToSend(AfterAnalysisEvent $event): ?array
$issues = $event->getIssues();
$normalized_data = $issues === [] ? [] : array_filter(
array_merge(...array_values($issues)),
static fn(IssueData $i): bool => $i->severity === 'error',
static fn(IssueData $i): bool => $i->severity === IssueData::SEVERITY_ERROR,
);

$codebase = $event->getCodebase();
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Report.php
Expand Up @@ -74,7 +74,7 @@ public function __construct(
if (!$report_options->show_info) {
$this->issues_data = array_filter(
$issues_data,
static fn(IssueData $issue_data): bool => $issue_data->severity !== Config::REPORT_INFO,
static fn(IssueData $issue_data): bool => $issue_data->severity !== IssueData::SEVERITY_INFO,
);
} else {
$this->issues_data = $issues_data;
Expand Down
3 changes: 2 additions & 1 deletion src/Psalm/Report/CompactReport.php
Expand Up @@ -3,6 +3,7 @@
namespace Psalm\Report;

use Psalm\Config;
use Psalm\Internal\Analyzer\IssueData;
use Psalm\Report;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\BufferedOutput;
Expand Down Expand Up @@ -31,7 +32,7 @@ public function create(): string

$output = [];
foreach ($this->issues_data as $i => $issue_data) {
if (!$this->show_info && $issue_data->severity === Config::REPORT_INFO) {
if (!$this->show_info && $issue_data->severity === IssueData::SEVERITY_INFO) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Report/EmacsReport.php
Expand Up @@ -2,7 +2,7 @@

namespace Psalm\Report;

use Psalm\Config;
use Psalm\Internal\Analyzer\IssueData;
use Psalm\Report;

use function sprintf;
Expand All @@ -18,7 +18,7 @@ public function create(): string
$issue_data->file_path,
$issue_data->line_from,
$issue_data->column_from,
($issue_data->severity === Config::REPORT_ERROR ? 'error' : 'warning'),
($issue_data->severity === IssueData::SEVERITY_ERROR ? 'error' : 'warning'),
$issue_data->type,
$issue_data->message,
$issue_data->link,
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Report/GithubActionsReport.php
Expand Up @@ -2,7 +2,7 @@

namespace Psalm\Report;

use Psalm\Config;
use Psalm\Internal\Analyzer\IssueData;
use Psalm\Report;

use function sprintf;
Expand Down Expand Up @@ -34,7 +34,7 @@ public function create(): string

$output .= sprintf(
'::%1$s %2$s::%3$s',
($issue_data->severity === Config::REPORT_ERROR ? 'error' : 'warning'),
($issue_data->severity === IssueData::SEVERITY_ERROR ? 'error' : 'warning'),
$properties,
$data,
) . "\n";
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Report/JunitReport.php
Expand Up @@ -27,8 +27,8 @@ public function create(): string
$ndata = [];

foreach ($this->issues_data as $error) {
$is_error = $error->severity === Config::REPORT_ERROR;
$is_warning = $error->severity === Config::REPORT_INFO;
$is_error = $error->severity === IssueData::SEVERITY_ERROR;
$is_warning = $error->severity === IssueData::SEVERITY_INFO;

if (!$is_error && !$is_warning) {
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Report/TextReport.php
Expand Up @@ -2,7 +2,7 @@

namespace Psalm\Report;

use Psalm\Config;
use Psalm\Internal\Analyzer\IssueData;
use Psalm\Report;

use function sprintf;
Expand All @@ -18,7 +18,7 @@ public function create(): string
$issue_data->file_path,
$issue_data->line_from,
$issue_data->column_from,
($issue_data->severity === Config::REPORT_ERROR ? 'error' : 'warning'),
($issue_data->severity === IssueData::SEVERITY_ERROR ? 'error' : 'warning'),
$issue_data->type,
$issue_data->message,
) . "\n";
Expand Down
2 changes: 1 addition & 1 deletion tests/ByIssueLevelAndTypeReportTest.php
Expand Up @@ -75,7 +75,7 @@ public function testItGeneratesReport(): void
private function issueData(int $errorLevel, string $type): IssueData
{
return new IssueData(
'error',
IssueData::SEVERITY_ERROR,
1,
1,
$type,
Expand Down
24 changes: 12 additions & 12 deletions tests/ErrorBaselineTest.php
Expand Up @@ -187,7 +187,7 @@ public function testCreateShouldAggregateIssuesPerFile(): void
[
'sample/sample-file.php' => [
new IssueData(
'error',
IssueData::SEVERITY_ERROR,
0,
0,
'MixedAssignment',
Expand All @@ -204,7 +204,7 @@ public function testCreateShouldAggregateIssuesPerFile(): void
0,
),
new IssueData(
'error',
IssueData::SEVERITY_ERROR,
0,
0,
'MixedAssignment',
Expand All @@ -221,7 +221,7 @@ public function testCreateShouldAggregateIssuesPerFile(): void
0,
),
new IssueData(
'error',
IssueData::SEVERITY_ERROR,
0,
0,
'MixedAssignment',
Expand All @@ -238,7 +238,7 @@ public function testCreateShouldAggregateIssuesPerFile(): void
0,
),
new IssueData(
'error',
IssueData::SEVERITY_ERROR,
0,
0,
'MixedOperand',
Expand Down Expand Up @@ -274,7 +274,7 @@ public function testCreateShouldAggregateIssuesPerFile(): void
],
'sample/sample-file2.php' => [
new IssueData(
'error',
IssueData::SEVERITY_ERROR,
0,
0,
'MixedAssignment',
Expand All @@ -291,7 +291,7 @@ public function testCreateShouldAggregateIssuesPerFile(): void
0,
),
new IssueData(
'error',
IssueData::SEVERITY_ERROR,
0,
0,
'MixedAssignment',
Expand All @@ -308,7 +308,7 @@ public function testCreateShouldAggregateIssuesPerFile(): void
0,
),
new IssueData(
'error',
IssueData::SEVERITY_ERROR,
0,
0,
'TypeCoercion',
Expand Down Expand Up @@ -410,7 +410,7 @@ public function testUpdateShouldRemoveExistingIssuesWithoutAddingNewOnes(): void
$newIssues = [
'sample/sample-file.php' => [
new IssueData(
'error',
IssueData::SEVERITY_ERROR,
0,
0,
'MixedAssignment',
Expand All @@ -427,7 +427,7 @@ public function testUpdateShouldRemoveExistingIssuesWithoutAddingNewOnes(): void
0,
),
new IssueData(
'error',
IssueData::SEVERITY_ERROR,
0,
0,
'MixedAssignment',
Expand All @@ -444,7 +444,7 @@ public function testUpdateShouldRemoveExistingIssuesWithoutAddingNewOnes(): void
0,
),
new IssueData(
'error',
IssueData::SEVERITY_ERROR,
0,
0,
'MixedOperand',
Expand All @@ -461,7 +461,7 @@ public function testUpdateShouldRemoveExistingIssuesWithoutAddingNewOnes(): void
0,
),
new IssueData(
'error',
IssueData::SEVERITY_ERROR,
0,
0,
'MixedOperand',
Expand All @@ -480,7 +480,7 @@ public function testUpdateShouldRemoveExistingIssuesWithoutAddingNewOnes(): void
],
'sample/sample-file2.php' => [
new IssueData(
'error',
IssueData::SEVERITY_ERROR,
0,
0,
'TypeCoercion',
Expand Down
8 changes: 4 additions & 4 deletions tests/IssueBufferTest.php
Expand Up @@ -24,7 +24,7 @@ public function testFinishDoesNotCorruptInternalState(): void
IssueBuffer::addIssues([
'/path/one.php' => [
new IssueData(
"error",
IssueData::SEVERITY_ERROR,
0,
0,
"MissingPropertyType",
Expand All @@ -43,7 +43,7 @@ public function testFinishDoesNotCorruptInternalState(): void
],
'/path/two.php' => [
new IssueData(
"error",
IssueData::SEVERITY_ERROR,
0,
0,
"MissingPropertyType",
Expand All @@ -62,7 +62,7 @@ public function testFinishDoesNotCorruptInternalState(): void
],
'/path/three.php' => [
new IssueData(
"error",
IssueData::SEVERITY_ERROR,
0,
0,
"MissingPropertyType",
Expand All @@ -81,7 +81,7 @@ public function testFinishDoesNotCorruptInternalState(): void
],
'/path/four.php' => [
new IssueData(
"error",
IssueData::SEVERITY_ERROR,
0,
0,
"MissingPropertyType",
Expand Down
2 changes: 1 addition & 1 deletion tests/ReportOutputTest.php
Expand Up @@ -833,7 +833,7 @@ public function testFilteredJsonReportIsStillArray(): void
{
$issues_data = [
22 => new IssueData(
'info',
IssueData::SEVERITY_INFO,
15,
15,
'PossiblyUndefinedGlobalVariable',
Expand Down

0 comments on commit 5b2efad

Please sign in to comment.