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