2016-12-08 04:38:57 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
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;
|
2016-12-08 04:38:57 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2018-11-12 16:57:05 +01:00
|
|
|
use Psalm\Tests\Internal\Provider;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function substr;
|
2016-12-08 04:38:57 +01:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class JsonOutputTest extends TestCase
|
2016-12-08 04:38:57 +01:00
|
|
|
{
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-05-17 00:36:36 +02:00
|
|
|
public function setUp() : void
|
2016-12-08 04:38:57 +01: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-04-25 05:45:02 +02:00
|
|
|
// case, so don't run a `parent::setUp()` call here.
|
2018-11-06 03:57:36 +01:00
|
|
|
FileAnalyzer::clearCache();
|
2017-07-25 22:11:02 +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-09 18:37:28 +02:00
|
|
|
$stdout_report_options = new \Psalm\Report\ReportOptions();
|
|
|
|
$stdout_report_options->format = \Psalm\Report::TYPE_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
|
|
|
$stdout_report_options
|
2017-07-25 22:11:02 +02:00
|
|
|
);
|
2017-02-01 01:22:05 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer->getCodebase()->reportUnusedCode();
|
2016-12-08 04:38:57 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @dataProvider providerTestJsonOutputErrors
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-04-25 05:45:02 +02:00
|
|
|
* @param string $code
|
|
|
|
* @param string $message
|
2017-05-27 02:16:18 +02:00
|
|
|
* @param int $line_number
|
2017-04-25 05:45:02 +02:00
|
|
|
* @param string $error
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function testJsonOutputErrors($code, $message, $line_number, $error)
|
2016-12-08 04:38:57 +01:00
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->addFile('somefile.php', $code);
|
2018-01-21 16:22:04 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2017-07-25 22:11:02 +02:00
|
|
|
$issue_data = IssueBuffer::getIssuesData()[0];
|
2016-12-08 22:23:07 +01:00
|
|
|
|
|
|
|
$this->assertSame('somefile.php', $issue_data['file_path']);
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->assertSame('error', $issue_data['severity']);
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->assertSame($message, $issue_data['message']);
|
2018-02-20 00:16:09 +01:00
|
|
|
$this->assertSame($line_number, $issue_data['line_from']);
|
2016-12-08 22:23:07 +01:00
|
|
|
$this->assertSame(
|
2017-04-25 05:45:02 +02:00
|
|
|
$error,
|
|
|
|
substr($code, $issue_data['from'], $issue_data['to'] - $issue_data['from'])
|
2017-01-02 05:30:59 +01:00
|
|
|
);
|
|
|
|
}
|
2017-01-16 05:18:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testJsonOutputForGetPsalmDotOrg()
|
|
|
|
{
|
|
|
|
$file_contents = '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function psalmCanVerify(int $your_code): ?string {
|
2017-01-16 05:18:26 +01: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;';
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->addFile(
|
2017-01-16 05:18:26 +01:00
|
|
|
'somefile.php',
|
|
|
|
$file_contents
|
|
|
|
);
|
|
|
|
|
2019-12-02 21:24:01 +01:00
|
|
|
$this->project_analyzer->consolidateAnalyzedData();
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$issue_data = IssueBuffer::getIssuesData();
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame(
|
2017-01-16 05:18:26 +01:00
|
|
|
[
|
|
|
|
[
|
2017-07-25 22:11:02 +02:00
|
|
|
'severity' => 'error',
|
2018-02-20 00:16:09 +01:00
|
|
|
'line_from' => 3,
|
|
|
|
'line_to' => 3,
|
2017-07-25 22:11:02 +02:00
|
|
|
'type' => 'UndefinedVariable',
|
2017-01-16 05:18:26 +01:00
|
|
|
'message' => 'Cannot find referenced variable $as_you',
|
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
|
|
|
'snippet' => ' return $as_you . "type";',
|
2018-01-02 02:04:03 +01:00
|
|
|
'selected_text' => '$as_you',
|
2018-01-11 21:50:45 +01:00
|
|
|
'from' => 66,
|
|
|
|
'to' => 73,
|
|
|
|
'snippet_from' => 57,
|
|
|
|
'snippet_to' => 83,
|
2018-02-20 00:16:09 +01:00
|
|
|
'column_from' => 10,
|
2018-02-22 00:59:31 +01:00
|
|
|
'column_to' => 17,
|
2017-01-16 05:18:26 +01:00
|
|
|
],
|
2017-12-30 03:28:21 +01:00
|
|
|
[
|
|
|
|
'severity' => 'error',
|
2018-02-20 00:16:09 +01:00
|
|
|
'line_from' => 2,
|
|
|
|
'line_to' => 2,
|
2017-12-30 03:28:21 +01:00
|
|
|
'type' => 'UnusedParam',
|
|
|
|
'message' => 'Param $your_code is never referenced in this method',
|
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
2018-01-11 21:50:45 +01:00
|
|
|
'snippet' => 'function psalmCanVerify(int $your_code): ?string {',
|
2018-01-02 02:04:03 +01:00
|
|
|
'selected_text' => '$your_code',
|
2017-12-30 03:28:21 +01:00
|
|
|
'from' => 34,
|
|
|
|
'to' => 44,
|
|
|
|
'snippet_from' => 6,
|
2018-01-11 21:50:45 +01:00
|
|
|
'snippet_to' => 56,
|
2018-02-20 00:16:09 +01:00
|
|
|
'column_from' => 29,
|
2018-02-22 00:59:31 +01:00
|
|
|
'column_to' => 39,
|
2017-12-30 03:28:21 +01:00
|
|
|
],
|
2017-01-16 05:18:26 +01:00
|
|
|
[
|
2017-07-25 22:11:02 +02:00
|
|
|
'severity' => 'error',
|
2018-02-20 00:16:09 +01:00
|
|
|
'line_from' => 2,
|
2018-03-29 16:26:26 +02:00
|
|
|
'line_to' => 2,
|
2017-07-25 22:11:02 +02:00
|
|
|
'type' => 'MixedInferredReturnType',
|
2019-10-17 07:14:33 +02:00
|
|
|
'message' => 'Could not verify return type \'null|string\' for psalmCanVerify',
|
2017-01-16 05:18:26 +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-01-02 02:04:03 +01:00
|
|
|
'selected_text' => '?string',
|
2018-01-11 21:50:45 +01:00
|
|
|
'from' => 47,
|
|
|
|
'to' => 54,
|
2017-07-25 22:11:02 +02:00
|
|
|
'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,
|
2017-01-16 05:18:26 +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,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'severity' => 'error',
|
|
|
|
'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,
|
|
|
|
],
|
2017-01-16 05:18:26 +01:00
|
|
|
],
|
|
|
|
$issue_data
|
|
|
|
);
|
|
|
|
}
|
2017-04-25 05:45:02 +02:00
|
|
|
|
|
|
|
/**
|
2019-02-23 22:22:39 +01:00
|
|
|
* @return array<string,array{string,message:string,line:int,error:string}>
|
2017-04-25 05:45:02 +02:00
|
|
|
*/
|
|
|
|
public function providerTestJsonOutputErrors()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'returnTypeError' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(int $a): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
return $a + 1;
|
|
|
|
}',
|
2017-12-07 21:50:25 +01:00
|
|
|
'message' => "The type 'int' does not match the declared return type 'string' for fooFoo",
|
|
|
|
'line' => 3,
|
2019-03-29 20:36:13 +01:00
|
|
|
'error' => '$a + 1',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'undefinedVar' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(int $a): int {
|
2017-04-25 05:45:02 +02:00
|
|
|
return $b + 1;
|
|
|
|
}',
|
|
|
|
'message' => 'Cannot find referenced variable $b',
|
|
|
|
'line' => 3,
|
2017-05-27 02:05:57 +02:00
|
|
|
'error' => '$b',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'unknownParamClass' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(Badger\Bodger $a): Badger\Bodger {
|
2017-04-25 05:45:02 +02:00
|
|
|
return $a;
|
|
|
|
}',
|
|
|
|
'message' => 'Class or interface Badger\\Bodger does not exist',
|
|
|
|
'line' => 2,
|
2017-05-27 02:05:57 +02:00
|
|
|
'error' => 'Badger\\Bodger',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'missingReturnType' => [
|
|
|
|
'<?php
|
|
|
|
function fooFoo() {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'message' => 'Method fooFoo does not have a return type, expecting string',
|
|
|
|
'line' => 2,
|
2018-10-07 02:11:19 +02:00
|
|
|
'error' => 'fooFoo',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'wrongMultilineReturnType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function fooFoo() {
|
|
|
|
return "hello";
|
|
|
|
}',
|
2018-08-09 04:44:02 +02:00
|
|
|
'message' => "The type 'string(hello)' does not match the declared return type 'int' for fooFoo",
|
2017-12-07 21:50:25 +01:00
|
|
|
'line' => 6,
|
2019-03-29 20:36:13 +01:00
|
|
|
'error' => '"hello"',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
2016-12-08 04:38:57 +01:00
|
|
|
}
|