2017-09-08 17:18:48 +02:00
|
|
|
<?php
|
2020-02-11 22:09:57 +01:00
|
|
|
|
2017-09-08 17:18:48 +02:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2020-03-05 04:51:05 +01:00
|
|
|
use DOMDocument;
|
2019-03-23 19:27:54 +01:00
|
|
|
use Psalm\Context;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\FileAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2017-09-08 17:18:48 +02:00
|
|
|
use Psalm\IssueBuffer;
|
2019-06-09 18:37:28 +02:00
|
|
|
use Psalm\Report;
|
2020-02-11 22:09:57 +01:00
|
|
|
use Psalm\Report\JsonReport;
|
2019-07-05 22:24:00 +02:00
|
|
|
use Psalm\Tests\Internal\Provider;
|
2020-02-11 22:09:57 +01:00
|
|
|
|
|
|
|
use function file_get_contents;
|
|
|
|
use function json_decode;
|
|
|
|
use function ob_end_clean;
|
|
|
|
use function ob_start;
|
|
|
|
use function preg_replace;
|
|
|
|
use function array_values;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function unlink;
|
2017-09-08 17:18:48 +02:00
|
|
|
|
|
|
|
class ReportOutputTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-05-17 00:36:36 +02:00
|
|
|
public function setUp() : void
|
2017-09-08 17:18:48 +02:00
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
// `TestCase::setUp()` creates its own ProjectAnalyzer and Config instance, but we don't want to do that in this
|
2017-09-08 17:18:48 +02:00
|
|
|
// case, so don't run a `parent::setUp()` call here.
|
2018-11-06 03:57:36 +01:00
|
|
|
FileAnalyzer::clearCache();
|
2017-09-08 17:18:48 +02:00
|
|
|
$this->file_provider = new Provider\FakeFileProvider();
|
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
$config = new TestConfig();
|
|
|
|
$config->throw_exception = false;
|
2019-06-13 17:06:01 +02:00
|
|
|
$config->setCustomErrorLevel('PossiblyUndefinedGlobalVariable', \Psalm\Config::REPORT_INFO);
|
2018-01-21 16:22:04 +01:00
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
$json_report_options = ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.json']);
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = new ProjectAnalyzer(
|
2018-01-21 16:22:04 +01:00
|
|
|
$config,
|
2018-11-06 03:57:36 +01:00
|
|
|
new \Psalm\Internal\Provider\Providers(
|
2018-09-28 22:18:45 +02:00
|
|
|
$this->file_provider,
|
|
|
|
new Provider\FakeParserCacheProvider()
|
|
|
|
),
|
2019-06-09 18:37:28 +02:00
|
|
|
new Report\ReportOptions(),
|
|
|
|
$json_report_options
|
2017-09-08 17:18:48 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testReportFormatValid()
|
|
|
|
{
|
2018-01-21 16:22:04 +01:00
|
|
|
$config = new TestConfig();
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
2017-09-08 17:18:48 +02:00
|
|
|
// No exception
|
|
|
|
foreach (['.xml', '.txt', '.json', '.emacs'] as $extension) {
|
2019-06-09 18:37:28 +02:00
|
|
|
ProjectAnalyzer::getFileReportOptions(['/tmp/report' . $extension]);
|
2017-09-08 17:18:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testReportFormatException()
|
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\UnexpectedValueException::class);
|
2018-01-21 16:22:04 +01:00
|
|
|
$config = new TestConfig();
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
ProjectAnalyzer::getFileReportOptions(['/tmp/report.log']);
|
2017-09-08 17:18:48 +02:00
|
|
|
}
|
|
|
|
|
2019-06-13 17:06:01 +02:00
|
|
|
public function analyzeFileForReport() : void
|
2017-09-08 17:18:48 +02:00
|
|
|
{
|
2019-07-05 22:24:00 +02:00
|
|
|
$file_contents = '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function psalmCanVerify(int $your_code): ?string {
|
2017-09-08 17:18:48 +02:00
|
|
|
return $as_you . "type";
|
|
|
|
}
|
|
|
|
|
|
|
|
// and it supports PHP 5.4 - 7.1
|
|
|
|
echo CHANGE_ME;
|
|
|
|
|
|
|
|
if (rand(0, 100) > 10) {
|
|
|
|
$a = 5;
|
|
|
|
} else {
|
|
|
|
//$a = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
$file_contents
|
|
|
|
);
|
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2019-06-13 17:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testJsonReport()
|
|
|
|
{
|
|
|
|
$this->analyzeFileForReport();
|
2018-01-21 16:22:04 +01:00
|
|
|
|
2017-09-08 17:18:48 +02:00
|
|
|
$issue_data = [
|
2018-02-20 00:16:09 +01:00
|
|
|
[
|
|
|
|
'severity' => 'error',
|
|
|
|
'line_from' => 3,
|
|
|
|
'line_to' => 3,
|
|
|
|
'type' => 'UndefinedVariable',
|
|
|
|
'message' => 'Cannot find referenced variable $as_you',
|
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
|
|
|
'snippet' => ' return $as_you . "type";',
|
|
|
|
'selected_text' => '$as_you',
|
|
|
|
'from' => 66,
|
|
|
|
'to' => 73,
|
|
|
|
'snippet_from' => 57,
|
|
|
|
'snippet_to' => 83,
|
|
|
|
'column_from' => 10,
|
2018-02-22 00:59:31 +01:00
|
|
|
'column_to' => 17,
|
2020-02-17 22:43:50 +01:00
|
|
|
'error_level' => -1,
|
2018-02-20 00:16:09 +01:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'severity' => 'error',
|
|
|
|
'line_from' => 2,
|
2018-03-29 16:26:26 +02:00
|
|
|
'line_to' => 2,
|
2018-02-20 00:16:09 +01:00
|
|
|
'type' => 'MixedInferredReturnType',
|
2019-10-17 07:14:33 +02:00
|
|
|
'message' => 'Could not verify return type \'null|string\' for psalmCanVerify',
|
2018-02-20 00:16:09 +01:00
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
2019-03-08 05:19:17 +01:00
|
|
|
'snippet' => 'function psalmCanVerify(int $your_code): ?string {',
|
2018-02-20 00:16:09 +01:00
|
|
|
'selected_text' => '?string',
|
|
|
|
'from' => 47,
|
|
|
|
'to' => 54,
|
|
|
|
'snippet_from' => 6,
|
2019-03-08 05:19:17 +01:00
|
|
|
'snippet_to' => 56,
|
2018-02-20 00:16:09 +01:00
|
|
|
'column_from' => 42,
|
2018-02-22 00:59:31 +01:00
|
|
|
'column_to' => 49,
|
2020-02-17 22:43:50 +01:00
|
|
|
'error_level' => 1,
|
2018-02-20 00:16:09 +01:00
|
|
|
],
|
2018-02-25 16:43:54 +01:00
|
|
|
[
|
|
|
|
'severity' => 'error',
|
|
|
|
'line_from' => 7,
|
|
|
|
'line_to' => 7,
|
|
|
|
'type' => 'UndefinedConstant',
|
|
|
|
'message' => 'Const CHANGE_ME is not defined',
|
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
|
|
|
'snippet' => 'echo CHANGE_ME;',
|
|
|
|
'selected_text' => 'CHANGE_ME',
|
|
|
|
'from' => 125,
|
|
|
|
'to' => 134,
|
|
|
|
'snippet_from' => 120,
|
|
|
|
'snippet_to' => 135,
|
|
|
|
'column_from' => 6,
|
|
|
|
'column_to' => 15,
|
2020-02-17 22:43:50 +01:00
|
|
|
'error_level' => -1,
|
2018-02-25 16:43:54 +01:00
|
|
|
],
|
|
|
|
[
|
2019-06-13 17:06:01 +02:00
|
|
|
'severity' => 'info',
|
2018-02-25 16:43:54 +01:00
|
|
|
'line_from' => 15,
|
|
|
|
'line_to' => 15,
|
|
|
|
'type' => 'PossiblyUndefinedGlobalVariable',
|
|
|
|
'message' => 'Possibly undefined global variable $a, first seen on line 10',
|
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
|
|
|
'snippet' => 'echo $a',
|
|
|
|
'selected_text' => '$a',
|
|
|
|
'from' => 201,
|
|
|
|
'to' => 203,
|
|
|
|
'snippet_from' => 196,
|
|
|
|
'snippet_to' => 203,
|
|
|
|
'column_from' => 6,
|
|
|
|
'column_to' => 8,
|
2020-02-17 22:43:50 +01:00
|
|
|
'error_level' => 3,
|
2018-02-25 16:43:54 +01:00
|
|
|
],
|
2017-09-08 17:18:48 +02:00
|
|
|
];
|
2018-02-20 00:16:09 +01:00
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
$json_report_options = ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.json'])[0];
|
|
|
|
|
2017-09-08 17:18:48 +02:00
|
|
|
$this->assertSame(
|
2020-02-11 22:09:57 +01:00
|
|
|
array_values($issue_data),
|
2019-06-09 18:37:28 +02:00
|
|
|
json_decode(IssueBuffer::getOutput($json_report_options), true)
|
2017-09-08 17:18:48 +02:00
|
|
|
);
|
2019-06-13 17:06:01 +02:00
|
|
|
}
|
|
|
|
|
2020-02-11 22:09:57 +01:00
|
|
|
public function testFilteredJsonReportIsStillArray(): void
|
|
|
|
{
|
|
|
|
$issues_data = [
|
2020-02-17 00:24:40 +01:00
|
|
|
22 => new \Psalm\Internal\Analyzer\IssueData(
|
|
|
|
'info',
|
|
|
|
15,
|
|
|
|
15,
|
|
|
|
'PossiblyUndefinedGlobalVariable',
|
|
|
|
'Possibly undefined global variable $a, first seen on line 10',
|
|
|
|
'somefile.php',
|
|
|
|
'somefile.php',
|
|
|
|
'echo $a',
|
|
|
|
'$a',
|
|
|
|
201,
|
|
|
|
203,
|
|
|
|
196,
|
|
|
|
203,
|
|
|
|
6,
|
2020-02-17 00:43:54 +01:00
|
|
|
8
|
2020-02-17 00:24:40 +01:00
|
|
|
),
|
2020-02-11 22:09:57 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$report_options = ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.json'])[0];
|
|
|
|
$fixable_issue_counts = ['MixedInferredReturnType' => 1];
|
|
|
|
|
|
|
|
$report = new JsonReport(
|
|
|
|
$issues_data,
|
|
|
|
$fixable_issue_counts,
|
|
|
|
$report_options
|
|
|
|
);
|
|
|
|
$this->assertIsArray(json_decode($report->create()));
|
|
|
|
}
|
|
|
|
|
2019-06-18 03:17:09 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSonarqubeReport()
|
|
|
|
{
|
|
|
|
$this->analyzeFileForReport();
|
|
|
|
|
|
|
|
$issue_data = [
|
|
|
|
'issues' => [
|
|
|
|
[
|
|
|
|
'engineId' => 'Psalm',
|
|
|
|
'ruleId' => 'UndefinedVariable',
|
|
|
|
'primaryLocation' => [
|
|
|
|
'message' => 'Cannot find referenced variable $as_you',
|
|
|
|
'filePath' => 'somefile.php',
|
|
|
|
'textRange' => [
|
|
|
|
'startLine' => 3,
|
|
|
|
'endLine' => 3,
|
|
|
|
'startColumn' => 9,
|
|
|
|
'endColumn' => 16,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'type' => 'CODE_SMELL',
|
|
|
|
'severity' => 'CRITICAL',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'engineId' => 'Psalm',
|
|
|
|
'ruleId' => 'MixedInferredReturnType',
|
|
|
|
'primaryLocation' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'message' => 'Could not verify return type \'null|string\' for psalmCanVerify',
|
2019-06-18 03:17:09 +02:00
|
|
|
'filePath' => 'somefile.php',
|
|
|
|
'textRange' => [
|
|
|
|
'startLine' => 2,
|
|
|
|
'endLine' => 2,
|
|
|
|
'startColumn' => 41,
|
|
|
|
'endColumn' => 48,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'type' => 'CODE_SMELL',
|
|
|
|
'severity' => 'CRITICAL',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'engineId' => 'Psalm',
|
|
|
|
'ruleId' => 'UndefinedConstant',
|
|
|
|
'primaryLocation' => [
|
|
|
|
'message' => 'Const CHANGE_ME is not defined',
|
|
|
|
'filePath' => 'somefile.php',
|
|
|
|
'textRange' => [
|
|
|
|
'startLine' => 7,
|
|
|
|
'endLine' => 7,
|
|
|
|
'startColumn' => 5,
|
|
|
|
'endColumn' => 14,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'type' => 'CODE_SMELL',
|
|
|
|
'severity' => 'CRITICAL',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'engineId' => 'Psalm',
|
|
|
|
'ruleId' => 'PossiblyUndefinedGlobalVariable',
|
|
|
|
'primaryLocation' => [
|
|
|
|
'message' => 'Possibly undefined global variable $a, first seen on line 10',
|
|
|
|
'filePath' => 'somefile.php',
|
|
|
|
'textRange' => [
|
|
|
|
'startLine' => 15,
|
|
|
|
'endLine' => 15,
|
|
|
|
'startColumn' => 5,
|
|
|
|
'endColumn' => 7,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'type' => 'CODE_SMELL',
|
|
|
|
'severity' => 'MINOR',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$sonarqube_report_options = ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-sonarqube.json'])[0];
|
|
|
|
$sonarqube_report_options->format = 'sonarqube';
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$issue_data,
|
|
|
|
json_decode(IssueBuffer::getOutput($sonarqube_report_options), true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-13 17:06:01 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testEmacsReport()
|
|
|
|
{
|
|
|
|
$this->analyzeFileForReport();
|
2018-12-09 21:47:20 +01:00
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
$emacs_report_options = ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.emacs'])[0];
|
|
|
|
|
2017-09-08 17:18:48 +02:00
|
|
|
$this->assertSame(
|
2018-12-09 21:47:20 +01:00
|
|
|
'somefile.php:3:10:error - Cannot find referenced variable $as_you
|
2019-10-17 07:14:33 +02:00
|
|
|
somefile.php:2:42:error - Could not verify return type \'null|string\' for psalmCanVerify
|
2018-12-09 21:47:20 +01:00
|
|
|
somefile.php:7:6:error - Const CHANGE_ME is not defined
|
2019-06-13 17:06:01 +02:00
|
|
|
somefile.php:15:6:warning - Possibly undefined global variable $a, first seen on line 10
|
2018-12-09 21:47:20 +01:00
|
|
|
',
|
2019-06-09 18:37:28 +02:00
|
|
|
IssueBuffer::getOutput($emacs_report_options)
|
2017-09-08 17:18:48 +02:00
|
|
|
);
|
2019-06-13 17:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPylintReport()
|
|
|
|
{
|
|
|
|
$this->analyzeFileForReport();
|
2018-12-09 21:47:20 +01:00
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
$pylint_report_options = ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.pylint'])[0];
|
|
|
|
|
2018-12-09 21:47:20 +01:00
|
|
|
$this->assertSame(
|
|
|
|
'somefile.php:3: [E0001] UndefinedVariable: Cannot find referenced variable $as_you (column 10)
|
2019-10-17 07:14:33 +02:00
|
|
|
somefile.php:2: [E0001] MixedInferredReturnType: Could not verify return type \'null|string\' for psalmCanVerify (column 42)
|
2018-12-09 21:47:20 +01:00
|
|
|
somefile.php:7: [E0001] UndefinedConstant: Const CHANGE_ME is not defined (column 6)
|
2019-06-13 17:06:01 +02:00
|
|
|
somefile.php:15: [W0001] PossiblyUndefinedGlobalVariable: Possibly undefined global variable $a, first seen on line 10 (column 6)
|
2018-12-09 21:47:20 +01:00
|
|
|
',
|
2019-06-09 18:37:28 +02:00
|
|
|
IssueBuffer::getOutput($pylint_report_options)
|
2018-12-09 21:47:20 +01:00
|
|
|
);
|
2019-06-13 17:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testConsoleReport()
|
|
|
|
{
|
|
|
|
$this->analyzeFileForReport();
|
2018-12-09 21:47:20 +01:00
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
$console_report_options = new Report\ReportOptions();
|
|
|
|
$console_report_options->use_color = false;
|
|
|
|
|
2018-12-09 21:47:20 +01:00
|
|
|
$this->assertSame(
|
|
|
|
'ERROR: UndefinedVariable - somefile.php:3:10 - Cannot find referenced variable $as_you
|
|
|
|
return $as_you . "type";
|
|
|
|
|
2019-10-17 07:14:33 +02:00
|
|
|
ERROR: MixedInferredReturnType - somefile.php:2:42 - Could not verify return type \'null|string\' for psalmCanVerify
|
2018-12-09 21:47:20 +01:00
|
|
|
function psalmCanVerify(int $your_code): ?string {
|
|
|
|
|
|
|
|
ERROR: UndefinedConstant - somefile.php:7:6 - Const CHANGE_ME is not defined
|
|
|
|
echo CHANGE_ME;
|
|
|
|
|
2019-06-13 17:06:01 +02:00
|
|
|
INFO: PossiblyUndefinedGlobalVariable - somefile.php:15:6 - Possibly undefined global variable $a, first seen on line 10
|
2018-12-09 21:47:20 +01:00
|
|
|
echo $a
|
|
|
|
|
|
|
|
',
|
2019-06-09 18:37:28 +02:00
|
|
|
IssueBuffer::getOutput($console_report_options)
|
2018-12-09 21:47:20 +01:00
|
|
|
);
|
2019-06-13 17:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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";
|
|
|
|
|
2019-10-17 07:14:33 +02:00
|
|
|
ERROR: MixedInferredReturnType - somefile.php:2:42 - Could not verify return type \'null|string\' for psalmCanVerify
|
2019-06-13 17:06:01 +02:00
|
|
|
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();
|
2018-12-09 21:47:20 +01:00
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
$console_report_options = new Report\ReportOptions();
|
|
|
|
$console_report_options->show_snippet = false;
|
|
|
|
$console_report_options->use_color = false;
|
|
|
|
|
2018-12-09 21:47:20 +01:00
|
|
|
$this->assertSame(
|
|
|
|
'ERROR: UndefinedVariable - somefile.php:3:10 - Cannot find referenced variable $as_you
|
|
|
|
|
|
|
|
|
2019-10-17 07:14:33 +02:00
|
|
|
ERROR: MixedInferredReturnType - somefile.php:2:42 - Could not verify return type \'null|string\' for psalmCanVerify
|
2018-12-09 21:47:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
ERROR: UndefinedConstant - somefile.php:7:6 - Const CHANGE_ME is not defined
|
|
|
|
|
|
|
|
|
2019-06-13 17:06:01 +02:00
|
|
|
INFO: PossiblyUndefinedGlobalVariable - somefile.php:15:6 - Possibly undefined global variable $a, first seen on line 10
|
2018-12-09 21:47:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
',
|
2019-06-09 18:37:28 +02:00
|
|
|
IssueBuffer::getOutput($console_report_options)
|
2018-12-09 21:47:20 +01:00
|
|
|
);
|
2019-06-13 17:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCompactReport()
|
|
|
|
{
|
|
|
|
$this->analyzeFileForReport();
|
2018-12-09 21:47:20 +01:00
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
$compact_report_options = new Report\ReportOptions();
|
|
|
|
$compact_report_options->format = Report::TYPE_COMPACT;
|
|
|
|
$compact_report_options->use_color = false;
|
|
|
|
|
2018-12-09 21:47:20 +01:00
|
|
|
$this->assertSame(
|
2019-07-03 22:58:27 +02:00
|
|
|
'FILE: somefile.php' . "\n" .
|
|
|
|
"\n" .
|
|
|
|
'+----------+------+---------------------------------+---------------------------------------------------------------+' . "\n" .
|
|
|
|
'| SEVERITY | LINE | ISSUE | DESCRIPTION |' . "\n" .
|
|
|
|
'+----------+------+---------------------------------+---------------------------------------------------------------+' . "\n" .
|
|
|
|
'| ERROR | 3 | UndefinedVariable | Cannot find referenced variable $as_you |' . "\n" .
|
2019-10-17 07:14:33 +02:00
|
|
|
'| ERROR | 2 | MixedInferredReturnType | Could not verify return type \'null|string\' for psalmCanVerify |' . "\n" .
|
2019-07-03 22:58:27 +02:00
|
|
|
'| ERROR | 7 | UndefinedConstant | Const CHANGE_ME is not defined |' . "\n" .
|
|
|
|
'| INFO | 15 | PossiblyUndefinedGlobalVariable | Possibly undefined global variable $a, first seen on line 10 |' . "\n" .
|
|
|
|
'+----------+------+---------------------------------+---------------------------------------------------------------+' . "\n",
|
2019-07-07 14:55:53 +02:00
|
|
|
$this->toUnixLineEndings(IssueBuffer::getOutput($compact_report_options))
|
2018-12-09 21:47:20 +01:00
|
|
|
);
|
2019-06-13 17:06:01 +02:00
|
|
|
}
|
2019-12-19 21:18:09 +01:00
|
|
|
|
2019-06-13 17:06:01 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCheckstyleReport()
|
|
|
|
{
|
|
|
|
$this->analyzeFileForReport();
|
2018-12-09 21:47:20 +01:00
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
$checkstyle_report_options = ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.checkstyle.xml'])[0];
|
|
|
|
|
2019-05-08 20:26:52 +02:00
|
|
|
$this->assertSame(
|
|
|
|
'<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<checkstyle>
|
|
|
|
<file name="somefile.php">
|
|
|
|
<error line="3" column="10" severity="error" message="UndefinedVariable: Cannot find referenced variable $as_you"/>
|
|
|
|
</file>
|
|
|
|
<file name="somefile.php">
|
2019-10-17 07:14:33 +02:00
|
|
|
<error line="2" column="42" severity="error" message="MixedInferredReturnType: Could not verify return type \'null|string\' for psalmCanVerify"/>
|
2019-05-08 20:26:52 +02:00
|
|
|
</file>
|
|
|
|
<file name="somefile.php">
|
|
|
|
<error line="7" column="6" severity="error" message="UndefinedConstant: Const CHANGE_ME is not defined"/>
|
|
|
|
</file>
|
|
|
|
<file name="somefile.php">
|
2019-06-13 17:06:01 +02:00
|
|
|
<error line="15" column="6" severity="info" message="PossiblyUndefinedGlobalVariable: Possibly undefined global variable $a, first seen on line 10"/>
|
2019-05-08 20:26:52 +02:00
|
|
|
</file>
|
|
|
|
</checkstyle>
|
|
|
|
',
|
2019-06-09 18:37:28 +02:00
|
|
|
IssueBuffer::getOutput($checkstyle_report_options)
|
2019-05-08 20:26:52 +02:00
|
|
|
);
|
|
|
|
|
2017-09-08 17:18:48 +02:00
|
|
|
// FIXME: The XML parser only return strings, all int value are casted, so the assertSame failed
|
|
|
|
//$this->assertSame(
|
|
|
|
// ['report' => ['item' => $issue_data]],
|
2018-11-06 03:57:36 +01:00
|
|
|
// XML2Array::createArray(IssueBuffer::getOutput(ProjectAnalyzer::TYPE_XML, false), LIBXML_NOCDATA)
|
2017-09-08 17:18:48 +02:00
|
|
|
//);
|
|
|
|
}
|
2017-09-27 00:18:24 +02:00
|
|
|
|
2019-12-19 21:18:09 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testJunitReport()
|
|
|
|
{
|
|
|
|
$this->analyzeFileForReport();
|
|
|
|
|
|
|
|
$checkstyle_report_options = ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.junit.xml'])[0];
|
|
|
|
|
2020-03-05 04:51:05 +01:00
|
|
|
$xml = IssueBuffer::getOutput($checkstyle_report_options);
|
|
|
|
|
2019-12-19 21:18:09 +01:00
|
|
|
$this->assertSame(
|
|
|
|
'<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<testsuites>
|
|
|
|
<testsuite failures="3" warnings="1" name="psalm" tests="4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/junit-team/junit5/r5.5.1/platform-tests/src/test/resources/jenkins-junit.xsd">
|
|
|
|
<testsuite name="somefile.php" file="somefile.php" assertions="4" failures="3" warnings="1" tests="4">
|
|
|
|
<testcase name="somefile.php:3" file="somefile.php" class="UndefinedVariable" classname="UndefinedVariable" line="3" assertions="1">
|
|
|
|
<failure type="UndefinedVariable">message: Cannot find referenced variable $as_you
|
|
|
|
type: UndefinedVariable
|
|
|
|
snippet: return $as_you . "type";
|
|
|
|
selected_text: $as_you
|
|
|
|
line: 3
|
|
|
|
column_from: 10
|
|
|
|
column_to: 17
|
|
|
|
</failure>
|
|
|
|
</testcase>
|
|
|
|
<testcase name="somefile.php:2" file="somefile.php" class="MixedInferredReturnType" classname="MixedInferredReturnType" line="2" assertions="1">
|
|
|
|
<failure type="MixedInferredReturnType">message: Could not verify return type \'null|string\' for psalmCanVerify
|
|
|
|
type: MixedInferredReturnType
|
|
|
|
snippet: function psalmCanVerify(int $your_code): ?string {
|
|
|
|
selected_text: ?string
|
|
|
|
line: 2
|
|
|
|
column_from: 42
|
|
|
|
column_to: 49
|
|
|
|
</failure>
|
|
|
|
</testcase>
|
|
|
|
<testcase name="somefile.php:7" file="somefile.php" class="UndefinedConstant" classname="UndefinedConstant" line="7" assertions="1">
|
|
|
|
<failure type="UndefinedConstant">message: Const CHANGE_ME is not defined
|
|
|
|
type: UndefinedConstant
|
|
|
|
snippet: echo CHANGE_ME;
|
|
|
|
selected_text: CHANGE_ME
|
|
|
|
line: 7
|
|
|
|
column_from: 6
|
|
|
|
column_to: 15
|
|
|
|
</failure>
|
|
|
|
</testcase>
|
|
|
|
<testcase name="somefile.php:15" file="somefile.php" class="PossiblyUndefinedGlobalVariable" classname="PossiblyUndefinedGlobalVariable" line="15" assertions="1">
|
|
|
|
<failure type="PossiblyUndefinedGlobalVariable">message: Possibly undefined global variable $a, first seen on line 10
|
|
|
|
type: PossiblyUndefinedGlobalVariable
|
|
|
|
snippet: echo $a
|
|
|
|
selected_text: $a
|
|
|
|
line: 15
|
|
|
|
column_from: 6
|
|
|
|
column_to: 8
|
|
|
|
</failure>
|
|
|
|
</testcase>
|
|
|
|
</testsuite>
|
|
|
|
</testsuite>
|
|
|
|
</testsuites>
|
|
|
|
',
|
2020-03-05 04:51:05 +01:00
|
|
|
$xml
|
2019-12-19 21:18:09 +01:00
|
|
|
);
|
|
|
|
|
2020-03-05 04:51:05 +01:00
|
|
|
// Validate against junit xsd
|
|
|
|
$dom = new DOMDocument("1.0", "UTF-8");
|
|
|
|
$dom->preserveWhiteSpace = false;
|
|
|
|
$dom->loadXML($xml);
|
|
|
|
|
|
|
|
// Validate against xsd
|
|
|
|
$valid = $dom->schemaValidate(__DIR__ . "/junit.xsd");
|
|
|
|
$this->assertTrue($valid, "Output did not validate against XSD");
|
|
|
|
|
2019-12-19 21:18:09 +01:00
|
|
|
// FIXME: The XML parser only return strings, all int value are casted, so the assertSame failed
|
|
|
|
//$this->assertSame(
|
|
|
|
// ['report' => ['item' => $issue_data]],
|
|
|
|
// XML2Array::createArray(IssueBuffer::getOutput(ProjectAnalyzer::TYPE_XML, false), LIBXML_NOCDATA)
|
|
|
|
//);
|
|
|
|
}
|
|
|
|
|
2017-09-27 00:18:24 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testEmptyReportIfNotError()
|
|
|
|
{
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php ?>'
|
|
|
|
);
|
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2017-09-27 00:18:24 +02:00
|
|
|
$this->assertSame(
|
|
|
|
'[]
|
|
|
|
',
|
2019-06-09 18:37:28 +02:00
|
|
|
IssueBuffer::getOutput(ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.json'])[0])
|
2017-09-27 00:18:24 +02:00
|
|
|
);
|
|
|
|
$this->assertSame(
|
|
|
|
'',
|
2019-06-09 18:37:28 +02:00
|
|
|
IssueBuffer::getOutput(ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.emacs'])[0])
|
2017-09-27 00:18:24 +02:00
|
|
|
);
|
|
|
|
$this->assertSame(
|
|
|
|
'<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<report>
|
|
|
|
<item/>
|
|
|
|
</report>
|
|
|
|
',
|
2019-06-09 18:37:28 +02:00
|
|
|
IssueBuffer::getOutput(ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.xml'])[0])
|
2017-09-27 00:18:24 +02:00
|
|
|
);
|
2017-09-27 00:33:55 +02:00
|
|
|
|
2019-05-08 20:26:52 +02:00
|
|
|
$this->assertSame(
|
|
|
|
'<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<checkstyle>
|
|
|
|
</checkstyle>
|
|
|
|
',
|
2019-06-09 18:37:28 +02:00
|
|
|
IssueBuffer::getOutput(ProjectAnalyzer::getFileReportOptions([__DIR__ . '/test-report.checkstyle.xml'])[0])
|
2019-05-08 20:26:52 +02:00
|
|
|
);
|
|
|
|
|
2018-04-13 01:42:24 +02:00
|
|
|
ob_start();
|
2018-11-11 18:01:14 +01:00
|
|
|
IssueBuffer::finish($this->project_analyzer, true, 0);
|
2018-04-13 01:42:24 +02:00
|
|
|
ob_end_clean();
|
2017-09-27 00:33:55 +02:00
|
|
|
$this->assertFileExists(__DIR__ . '/test-report.json');
|
|
|
|
$this->assertSame('[]
|
|
|
|
', file_get_contents(__DIR__ . '/test-report.json'));
|
|
|
|
unlink(__DIR__ . '/test-report.json');
|
2017-09-27 00:18:24 +02:00
|
|
|
}
|
2019-07-07 14:55:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Needed when running on Windows
|
|
|
|
*/
|
|
|
|
private function toUnixLineEndings(string $output): string
|
|
|
|
{
|
|
|
|
return preg_replace('~\r\n?~', "\n", $output);
|
|
|
|
}
|
2017-09-08 17:18:48 +02:00
|
|
|
}
|