*/ class CodeClimateReport extends Report { public function create(): string { $options = $this->pretty ? Json::PRETTY : Json::DEFAULT; $issues_data = \array_map( function (IssueData $issue): array { /** * map fields to new structure. * Expected fields: * - type * - check_name * - description* * - content * - categories[] * - severity * - fingerprint* * - location.path* * - location.lines.begin* * * Fields with * are the one used by Gitlab for Code Quality */ return [ 'type' => 'issue', 'check_name' => $issue->type, 'description' => $issue->message, 'categories' => [$issue->type], 'severity' => $this->convertSeverity($issue->severity), 'fingerprint' => $this->calculateFingerprint($issue), 'location' => [ 'path' => $issue->file_path, 'lines' => [ 'begin' => $issue->from, 'end' => $issue->to, ], ], ]; }, $this->issues_data ); return Json::encode(array_values($issues_data), $options) . "\n"; } /** * convert our own severity to CodeClimate format * Values can be : info, minor, major, critical, or blocker * @return string */ protected function convertSeverity(string $input): string { if (Config::REPORT_INFO === $input) { return 'info'; } if (Config::REPORT_ERROR === $input) { return 'critical'; } if (Config::REPORT_SUPPRESS === $input) { return 'minor'; } // unknown cases ? fallback return 'critical'; } /** * calculate a unique fingerprint for a given issue */ protected function calculateFingerprint(IssueData $issue): string { return md5($issue->type.$issue->message.$issue->file_path.$issue->from.$issue->to); } }