2016-12-08 04:38:57 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2016-12-08 04:38:57 +01:00
|
|
|
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;
|
2021-07-02 01:10:21 +02:00
|
|
|
use Psalm\Internal\Provider\FakeFileProvider;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Internal\Provider\Providers;
|
2020-08-23 16:32:07 +02:00
|
|
|
use Psalm\Internal\RuntimeCaches;
|
2016-12-08 04:38:57 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Report;
|
|
|
|
use Psalm\Report\ReportOptions;
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Internal\Provider\FakeParserCacheProvider;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
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
|
|
|
{
|
2021-12-05 18:51:26 +01: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();
|
2021-07-02 01:10:21 +02:00
|
|
|
$this->file_provider = new FakeFileProvider();
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
$config = new TestConfig();
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$stdout_report_options = new ReportOptions();
|
|
|
|
$stdout_report_options->format = Report::TYPE_JSON;
|
2019-06-09 18:37:28 +02:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = new ProjectAnalyzer(
|
2018-01-21 16:22:04 +01:00
|
|
|
$config,
|
2021-12-03 20:11:20 +01:00
|
|
|
new Providers(
|
2018-09-28 22:18:45 +02:00
|
|
|
$this->file_provider,
|
2022-12-18 17:15:15 +01:00
|
|
|
new FakeParserCacheProvider(),
|
2018-09-28 22:18:45 +02:00
|
|
|
),
|
2022-12-18 17:15:15 +01: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-01-13 20:07:23 +01:00
|
|
|
*/
|
2022-02-11 00:35:50 +01:00
|
|
|
public function testJsonOutputErrors(
|
|
|
|
string $code,
|
|
|
|
int $error_count,
|
|
|
|
string $message,
|
|
|
|
int $line_number,
|
2023-07-24 10:48:32 +02:00
|
|
|
string $error,
|
2022-02-11 00:35:50 +01:00
|
|
|
): void {
|
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());
|
2022-02-10 07:04:02 +01:00
|
|
|
$all_issue_data = IssueBuffer::getIssuesData()['somefile.php'];
|
|
|
|
$this->assertCount($error_count, $all_issue_data);
|
|
|
|
$issue_data = $all_issue_data[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,
|
2022-12-18 17:15:15 +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
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @return array<string,array{code:string,error_count:int,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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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;
|
|
|
|
}',
|
2022-02-10 07:04:02 +01:00
|
|
|
'error_count' => 2,
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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;
|
|
|
|
}',
|
2022-02-10 07:04:02 +01:00
|
|
|
'error_count' => 5,
|
2017-04-25 05:45:02 +02:00
|
|
|
'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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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;
|
|
|
|
}',
|
2022-02-10 07:04:02 +01:00
|
|
|
'error_count' => 3,
|
2021-05-03 23:54:09 +02:00
|
|
|
'message' => 'Class, interface or enum named Badger\\Bodger does not exist',
|
2017-04-25 05:45:02 +02:00
|
|
|
'line' => 2,
|
2017-05-27 02:05:57 +02:00
|
|
|
'error' => 'Badger\\Bodger',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'missingReturnType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
function fooFoo() {
|
|
|
|
return "hello";
|
|
|
|
}',
|
2022-02-10 07:04:02 +01:00
|
|
|
'error_count' => 1,
|
2022-01-16 16:07:56 +01:00
|
|
|
'message' => "Method fooFoo does not have a return type, expecting '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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function fooFoo() {
|
|
|
|
return "hello";
|
|
|
|
}',
|
2022-02-10 07:04:02 +01:00
|
|
|
'error_count' => 2,
|
2022-01-16 16:07:56 +01:00
|
|
|
'message' => "The inferred type ''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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-09 05:01:45 +01:00
|
|
|
$a = $_GET["hello"];
|
2022-09-10 13:06:17 +02:00
|
|
|
assert(is_string($a));
|
|
|
|
if (is_string($a)) {}',
|
2022-10-16 14:39:34 +02:00
|
|
|
'error_count' => 1,
|
2022-09-10 13:06:17 +02:00
|
|
|
'message' => 'Docblock-defined type string for $a is always string',
|
2020-02-09 05:01:45 +01:00
|
|
|
'line' => 4,
|
2022-09-10 13:06:17 +02:00
|
|
|
'error' => 'is_string($a)',
|
2020-02-09 05:01:45 +01:00
|
|
|
],
|
2022-02-10 07:04:02 +01:00
|
|
|
'singleIssueForTypeDifference' => [
|
|
|
|
'code' => '<?php
|
|
|
|
function fooFoo(?string $a, ?string $b): void {
|
|
|
|
if ($a || $b) {
|
|
|
|
if ($a || $b) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_count' => 1,
|
2022-02-11 00:46:22 +01:00
|
|
|
'message' => 'Operand of type non-falsy-string is always truthy',
|
2022-02-10 07:04:02 +01:00
|
|
|
'line' => 4,
|
|
|
|
'error' => '$b',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
2016-12-08 04:38:57 +01:00
|
|
|
}
|