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\ProjectAnalyzer;
|
2020-08-23 16:32:07 +02:00
|
|
|
use Psalm\Internal\RuntimeCaches;
|
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
|
|
|
{
|
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.
|
2020-08-23 16:32:07 +02:00
|
|
|
RuntimeCaches::clearAll();
|
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
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testJsonOutputErrors($code, $message, $line_number, $error): void
|
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());
|
2020-01-22 03:07:44 +01:00
|
|
|
$issue_data = IssueBuffer::getIssuesData()['somefile.php'][0];
|
2016-12-08 22:23:07 +01:00
|
|
|
|
2020-02-17 00:24:40 +01:00
|
|
|
$this->assertSame('somefile.php', $issue_data->file_path);
|
|
|
|
$this->assertSame('error', $issue_data->severity);
|
|
|
|
$this->assertSame($message, $issue_data->message);
|
|
|
|
$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,
|
2020-02-17 00:24:40 +01:00
|
|
|
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
|
|
|
|
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
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerTestJsonOutputErrors(): array
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}',
|
2020-02-28 00:42:15 +01:00
|
|
|
'message' => "The inferred type 'int' does not match the declared return type 'string' for fooFoo",
|
2017-12-07 21:50:25 +01:00
|
|
|
'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";
|
|
|
|
}',
|
2020-10-16 23:56:31 +02:00
|
|
|
'message' => 'Method fooFoo does not have a return type, expecting string(hello)',
|
2017-04-25 05:45:02 +02:00
|
|
|
'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";
|
|
|
|
}',
|
2020-02-28 00:42:15 +01:00
|
|
|
'message' => "The inferred 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
|
|
|
],
|
2020-02-09 05:01:45 +01:00
|
|
|
'assertCancelsMixedAssignment' => [
|
|
|
|
'<?php
|
|
|
|
$a = $_GET["hello"];
|
|
|
|
assert(is_int($a));
|
|
|
|
if (is_int($a)) {}',
|
2020-09-11 04:44:35 +02:00
|
|
|
'message' => 'Docblock-defined type int for $a is always int',
|
2020-02-09 05:01:45 +01:00
|
|
|
'line' => 4,
|
|
|
|
'error' => 'is_int($a)',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
2016-12-08 04:38:57 +01:00
|
|
|
}
|