file_provider = new Provider\FakeFileProvider(); $config = new TestConfig(); $config->throw_exception = false; $stdout_report_options = new \Psalm\Report\ReportOptions(); $stdout_report_options->format = \Psalm\Report::TYPE_JSON; $this->project_analyzer = new ProjectAnalyzer( $config, new \Psalm\Internal\Provider\Providers( $this->file_provider, new Provider\FakeParserCacheProvider() ), $stdout_report_options ); $this->project_analyzer->getCodebase()->reportUnusedCode(); } /** * @dataProvider providerTestJsonOutputErrors * * @param string $code * @param string $message * @param int $line_number * @param string $error * */ public function testJsonOutputErrors($code, $message, $line_number, $error): void { $this->addFile('somefile.php', $code); $this->analyzeFile('somefile.php', new Context()); $issue_data = IssueBuffer::getIssuesData()['somefile.php'][0]; $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); $this->assertSame( $error, substr($code, $issue_data->from, $issue_data->to - $issue_data->from) ); } /** * @return array */ public function providerTestJsonOutputErrors(): array { return [ 'returnTypeError' => [ ' "The inferred type 'int' does not match the declared return type 'string' for fooFoo", 'line' => 3, 'error' => '$a + 1', ], 'undefinedVar' => [ ' 'Cannot find referenced variable $b', 'line' => 3, 'error' => '$b', ], 'unknownParamClass' => [ ' 'Class or interface Badger\\Bodger does not exist', 'line' => 2, 'error' => 'Badger\\Bodger', ], 'missingReturnType' => [ ' 'Method fooFoo does not have a return type, expecting string(hello)', 'line' => 2, 'error' => 'fooFoo', ], 'wrongMultilineReturnType' => [ ' "The inferred type 'string(hello)' does not match the declared return type 'int' for fooFoo", 'line' => 6, 'error' => '"hello"', ], 'assertCancelsMixedAssignment' => [ ' 'Docblock-defined type int for $a is always int', 'line' => 4, 'error' => 'is_int($a)', ], ]; } }