Skip to content

Commit

Permalink
Merge branch 'master' into shepherd-use-list-of-issues
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Psalm/Plugin/Shepherd.php
  • Loading branch information
alies-dev committed May 31, 2023
2 parents 2fa943a + 5b2efad commit 5e7e067
Show file tree
Hide file tree
Showing 17 changed files with 70 additions and 47 deletions.
7 changes: 7 additions & 0 deletions src/Psalm/Internal/Analyzer/IssueData.php
Original file line number Diff line number Diff line change
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
15 changes: 14 additions & 1 deletion src/Psalm/Internal/Cli/Psalm.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,20 @@ private static function initOutputFormat(array $options): string
{
return isset($options['output-format']) && is_string($options['output-format'])
? $options['output-format']
: Report::TYPE_CONSOLE;
: self::findDefaultOutputFormat();
}

/**
* @return Report::TYPE_*
*/
private static function findDefaultOutputFormat(): string
{
$emulator = getenv('TERMINAL_EMULATOR');
if (is_string($emulator) && substr($emulator, 0, 9) === 'JetBrains') {
return Report::TYPE_PHP_STORM;
}

return Report::TYPE_CONSOLE;
}

private static function initShowInfo(array $options): bool
Expand Down
14 changes: 7 additions & 7 deletions src/Psalm/Internal/Codebase/Analyzer.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private static function collectPayloadToSend(AfterAnalysisEvent $event): ?array
$issues = $event->getIssues();
$normalized_data = $issues === [] ? [] : array_values(array_filter(
array_merge(...array_values($issues)), // flatten an array
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 5e7e067

Please sign in to comment.