1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Command\ErrorFormatter;
4:
5: use OndraM\CiDetector\CiDetector;
6: use OndraM\CiDetector\Exception\CiNotDetectedException;
7: use PHPStan\Command\AnalysisResult;
8: use PHPStan\Command\Output;
9:
10: /** @api */
11: class CiDetectedErrorFormatter implements ErrorFormatter
12: {
13:
14: public function __construct(
15: private GithubErrorFormatter $githubErrorFormatter,
16: private TeamcityErrorFormatter $teamcityErrorFormatter,
17: )
18: {
19: }
20:
21: public function formatErrors(AnalysisResult $analysisResult, Output $output): int
22: {
23: $ciDetector = new CiDetector();
24:
25: try {
26: $ci = $ciDetector->detect();
27: if ($ci->getCiName() === CiDetector::CI_GITHUB_ACTIONS) {
28: return $this->githubErrorFormatter->formatErrors($analysisResult, $output);
29: } elseif ($ci->getCiName() === CiDetector::CI_TEAMCITY) {
30: return $this->teamcityErrorFormatter->formatErrors($analysisResult, $output);
31: }
32: } catch (CiNotDetectedException) {
33: // pass
34: }
35:
36: if (!$analysisResult->hasErrors() && !$analysisResult->hasWarnings()) {
37: return 0;
38: }
39:
40: return $analysisResult->getTotalErrorsCount() > 0 ? 1 : 0;
41: }
42:
43: }
44: