mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
parent
78a9d1aa9b
commit
e3c9dbf2c0
@ -61,10 +61,21 @@ abstract class Report
|
|||||||
int $mixed_expression_count = 1,
|
int $mixed_expression_count = 1,
|
||||||
int $total_expression_count = 1
|
int $total_expression_count = 1
|
||||||
) {
|
) {
|
||||||
|
if (!$report_options->show_info) {
|
||||||
|
$this->issues_data = array_filter(
|
||||||
|
$issues_data,
|
||||||
|
function (array $issue_data) : bool {
|
||||||
|
return $issue_data['severity'] !== Config::REPORT_INFO;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
$this->issues_data = $issues_data;
|
$this->issues_data = $issues_data;
|
||||||
|
}
|
||||||
|
|
||||||
$this->use_color = $report_options->use_color;
|
$this->use_color = $report_options->use_color;
|
||||||
$this->show_snippet = $report_options->show_snippet;
|
$this->show_snippet = $report_options->show_snippet;
|
||||||
$this->show_info = $report_options->show_info;
|
$this->show_info = $report_options->show_info;
|
||||||
|
|
||||||
$this->mixed_expression_count = $mixed_expression_count;
|
$this->mixed_expression_count = $mixed_expression_count;
|
||||||
$this->total_expression_count = $total_expression_count;
|
$this->total_expression_count = $total_expression_count;
|
||||||
}
|
}
|
||||||
|
@ -13,10 +13,6 @@ class ConsoleReport extends Report
|
|||||||
{
|
{
|
||||||
$output = '';
|
$output = '';
|
||||||
foreach ($this->issues_data as $issue_data) {
|
foreach ($this->issues_data as $issue_data) {
|
||||||
if (!$this->show_info && $issue_data['severity'] === Config::REPORT_INFO) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$output .= $this->format($issue_data) . "\n" . "\n";
|
$output .= $this->format($issue_data) . "\n" . "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@ class ReportOutputTest extends TestCase
|
|||||||
|
|
||||||
$config = new TestConfig();
|
$config = new TestConfig();
|
||||||
$config->throw_exception = false;
|
$config->throw_exception = false;
|
||||||
|
$config->setCustomErrorLevel('PossiblyUndefinedGlobalVariable', \Psalm\Config::REPORT_INFO);
|
||||||
|
|
||||||
$json_report_options = ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.json']);
|
$json_report_options = ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.json']);
|
||||||
|
|
||||||
@ -63,10 +64,7 @@ class ReportOutputTest extends TestCase
|
|||||||
ProjectAnalyzer::getFileReportOptions(['/tmp/report.log']);
|
ProjectAnalyzer::getFileReportOptions(['/tmp/report.log']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function analyzeFileForReport() : void
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function testGetOutputForGetPsalmDotOrg()
|
|
||||||
{
|
{
|
||||||
$file_contents = '<?php
|
$file_contents = '<?php
|
||||||
function psalmCanVerify(int $your_code): ?string {
|
function psalmCanVerify(int $your_code): ?string {
|
||||||
@ -90,6 +88,14 @@ echo $a;';
|
|||||||
);
|
);
|
||||||
|
|
||||||
$this->analyzeFile('somefile.php', new Context());
|
$this->analyzeFile('somefile.php', new Context());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testJsonReport()
|
||||||
|
{
|
||||||
|
$this->analyzeFileForReport();
|
||||||
|
|
||||||
$issue_data = [
|
$issue_data = [
|
||||||
[
|
[
|
||||||
@ -144,7 +150,7 @@ echo $a;';
|
|||||||
'column_to' => 15,
|
'column_to' => 15,
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'severity' => 'error',
|
'severity' => 'info',
|
||||||
'line_from' => 15,
|
'line_from' => 15,
|
||||||
'line_to' => 15,
|
'line_to' => 15,
|
||||||
'type' => 'PossiblyUndefinedGlobalVariable',
|
'type' => 'PossiblyUndefinedGlobalVariable',
|
||||||
@ -168,6 +174,14 @@ echo $a;';
|
|||||||
$issue_data,
|
$issue_data,
|
||||||
json_decode(IssueBuffer::getOutput($json_report_options), true)
|
json_decode(IssueBuffer::getOutput($json_report_options), true)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testEmacsReport()
|
||||||
|
{
|
||||||
|
$this->analyzeFileForReport();
|
||||||
|
|
||||||
$emacs_report_options = ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.emacs'])[0];
|
$emacs_report_options = ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.emacs'])[0];
|
||||||
|
|
||||||
@ -175,10 +189,18 @@ echo $a;';
|
|||||||
'somefile.php:3:10:error - Cannot find referenced variable $as_you
|
'somefile.php:3:10:error - Cannot find referenced variable $as_you
|
||||||
somefile.php:2:42:error - Could not verify return type \'string|null\' for psalmCanVerify
|
somefile.php:2:42:error - Could not verify return type \'string|null\' for psalmCanVerify
|
||||||
somefile.php:7:6:error - Const CHANGE_ME is not defined
|
somefile.php:7:6:error - Const CHANGE_ME is not defined
|
||||||
somefile.php:15:6:error - Possibly undefined global variable $a, first seen on line 10
|
somefile.php:15:6:warning - Possibly undefined global variable $a, first seen on line 10
|
||||||
',
|
',
|
||||||
IssueBuffer::getOutput($emacs_report_options)
|
IssueBuffer::getOutput($emacs_report_options)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testPylintReport()
|
||||||
|
{
|
||||||
|
$this->analyzeFileForReport();
|
||||||
|
|
||||||
$pylint_report_options = ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.pylint'])[0];
|
$pylint_report_options = ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.pylint'])[0];
|
||||||
|
|
||||||
@ -186,10 +208,18 @@ somefile.php:15:6:error - Possibly undefined global variable $a, first seen on l
|
|||||||
'somefile.php:3: [E0001] UndefinedVariable: Cannot find referenced variable $as_you (column 10)
|
'somefile.php:3: [E0001] UndefinedVariable: Cannot find referenced variable $as_you (column 10)
|
||||||
somefile.php:2: [E0001] MixedInferredReturnType: Could not verify return type \'string|null\' for psalmCanVerify (column 42)
|
somefile.php:2: [E0001] MixedInferredReturnType: Could not verify return type \'string|null\' for psalmCanVerify (column 42)
|
||||||
somefile.php:7: [E0001] UndefinedConstant: Const CHANGE_ME is not defined (column 6)
|
somefile.php:7: [E0001] UndefinedConstant: Const CHANGE_ME is not defined (column 6)
|
||||||
somefile.php:15: [E0001] PossiblyUndefinedGlobalVariable: Possibly undefined global variable $a, first seen on line 10 (column 6)
|
somefile.php:15: [W0001] PossiblyUndefinedGlobalVariable: Possibly undefined global variable $a, first seen on line 10 (column 6)
|
||||||
',
|
',
|
||||||
IssueBuffer::getOutput($pylint_report_options)
|
IssueBuffer::getOutput($pylint_report_options)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testConsoleReport()
|
||||||
|
{
|
||||||
|
$this->analyzeFileForReport();
|
||||||
|
|
||||||
$console_report_options = new Report\ReportOptions();
|
$console_report_options = new Report\ReportOptions();
|
||||||
$console_report_options->use_color = false;
|
$console_report_options->use_color = false;
|
||||||
@ -204,12 +234,46 @@ function psalmCanVerify(int $your_code): ?string {
|
|||||||
ERROR: UndefinedConstant - somefile.php:7:6 - Const CHANGE_ME is not defined
|
ERROR: UndefinedConstant - somefile.php:7:6 - Const CHANGE_ME is not defined
|
||||||
echo CHANGE_ME;
|
echo CHANGE_ME;
|
||||||
|
|
||||||
ERROR: PossiblyUndefinedGlobalVariable - somefile.php:15:6 - Possibly undefined global variable $a, first seen on line 10
|
INFO: PossiblyUndefinedGlobalVariable - somefile.php:15:6 - Possibly undefined global variable $a, first seen on line 10
|
||||||
echo $a
|
echo $a
|
||||||
|
|
||||||
',
|
',
|
||||||
IssueBuffer::getOutput($console_report_options)
|
IssueBuffer::getOutput($console_report_options)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testConsoleReportNoInfo()
|
||||||
|
{
|
||||||
|
$this->analyzeFileForReport();
|
||||||
|
|
||||||
|
$console_report_options = new Report\ReportOptions();
|
||||||
|
$console_report_options->use_color = false;
|
||||||
|
$console_report_options->show_info = false;
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
'ERROR: UndefinedVariable - somefile.php:3:10 - Cannot find referenced variable $as_you
|
||||||
|
return $as_you . "type";
|
||||||
|
|
||||||
|
ERROR: MixedInferredReturnType - somefile.php:2:42 - Could not verify return type \'string|null\' for psalmCanVerify
|
||||||
|
function psalmCanVerify(int $your_code): ?string {
|
||||||
|
|
||||||
|
ERROR: UndefinedConstant - somefile.php:7:6 - Const CHANGE_ME is not defined
|
||||||
|
echo CHANGE_ME;
|
||||||
|
|
||||||
|
',
|
||||||
|
IssueBuffer::getOutput($console_report_options)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testConsoleReportNoSnippet()
|
||||||
|
{
|
||||||
|
$this->analyzeFileForReport();
|
||||||
|
|
||||||
$console_report_options = new Report\ReportOptions();
|
$console_report_options = new Report\ReportOptions();
|
||||||
$console_report_options->show_snippet = false;
|
$console_report_options->show_snippet = false;
|
||||||
@ -225,12 +289,20 @@ ERROR: MixedInferredReturnType - somefile.php:2:42 - Could not verify return typ
|
|||||||
ERROR: UndefinedConstant - somefile.php:7:6 - Const CHANGE_ME is not defined
|
ERROR: UndefinedConstant - somefile.php:7:6 - Const CHANGE_ME is not defined
|
||||||
|
|
||||||
|
|
||||||
ERROR: PossiblyUndefinedGlobalVariable - somefile.php:15:6 - Possibly undefined global variable $a, first seen on line 10
|
INFO: PossiblyUndefinedGlobalVariable - somefile.php:15:6 - Possibly undefined global variable $a, first seen on line 10
|
||||||
|
|
||||||
|
|
||||||
',
|
',
|
||||||
IssueBuffer::getOutput($console_report_options)
|
IssueBuffer::getOutput($console_report_options)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testCompactReport()
|
||||||
|
{
|
||||||
|
$this->analyzeFileForReport();
|
||||||
|
|
||||||
$compact_report_options = new Report\ReportOptions();
|
$compact_report_options = new Report\ReportOptions();
|
||||||
$compact_report_options->format = Report::TYPE_COMPACT;
|
$compact_report_options->format = Report::TYPE_COMPACT;
|
||||||
@ -245,10 +317,18 @@ ERROR: PossiblyUndefinedGlobalVariable - somefile.php:15:6 - Possibly undefined
|
|||||||
'| ERROR | 3 | UndefinedVariable | Cannot find referenced variable $as_you |' . PHP_EOL .
|
'| ERROR | 3 | UndefinedVariable | Cannot find referenced variable $as_you |' . PHP_EOL .
|
||||||
'| ERROR | 2 | MixedInferredReturnType | Could not verify return type \'string|null\' for psalmCanVerify |' . PHP_EOL .
|
'| ERROR | 2 | MixedInferredReturnType | Could not verify return type \'string|null\' for psalmCanVerify |' . PHP_EOL .
|
||||||
'| ERROR | 7 | UndefinedConstant | Const CHANGE_ME is not defined |' . PHP_EOL .
|
'| ERROR | 7 | UndefinedConstant | Const CHANGE_ME is not defined |' . PHP_EOL .
|
||||||
'| ERROR | 15 | PossiblyUndefinedGlobalVariable | Possibly undefined global variable $a, first seen on line 10 |' . PHP_EOL .
|
'| INFO | 15 | PossiblyUndefinedGlobalVariable | Possibly undefined global variable $a, first seen on line 10 |' . PHP_EOL .
|
||||||
'+----------+------+---------------------------------+---------------------------------------------------------------+' . PHP_EOL,
|
'+----------+------+---------------------------------+---------------------------------------------------------------+' . PHP_EOL,
|
||||||
IssueBuffer::getOutput($compact_report_options)
|
IssueBuffer::getOutput($compact_report_options)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testCheckstyleReport()
|
||||||
|
{
|
||||||
|
$this->analyzeFileForReport();
|
||||||
|
|
||||||
$checkstyle_report_options = ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.checkstyle.xml'])[0];
|
$checkstyle_report_options = ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.checkstyle.xml'])[0];
|
||||||
|
|
||||||
@ -265,7 +345,7 @@ ERROR: PossiblyUndefinedGlobalVariable - somefile.php:15:6 - Possibly undefined
|
|||||||
<error line="7" column="6" severity="error" message="UndefinedConstant: Const CHANGE_ME is not defined"/>
|
<error line="7" column="6" severity="error" message="UndefinedConstant: Const CHANGE_ME is not defined"/>
|
||||||
</file>
|
</file>
|
||||||
<file name="somefile.php">
|
<file name="somefile.php">
|
||||||
<error line="15" column="6" severity="error" message="PossiblyUndefinedGlobalVariable: Possibly undefined global variable $a, first seen on line 10"/>
|
<error line="15" column="6" severity="info" message="PossiblyUndefinedGlobalVariable: Possibly undefined global variable $a, first seen on line 10"/>
|
||||||
</file>
|
</file>
|
||||||
</checkstyle>
|
</checkstyle>
|
||||||
',
|
',
|
||||||
|
Loading…
Reference in New Issue
Block a user