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: /**
11: * @api
12: */
13: final class CiDetectedErrorFormatter implements ErrorFormatter
14: {
15:
16: public function __construct(
17: private GithubErrorFormatter $githubErrorFormatter,
18: private TeamcityErrorFormatter $teamcityErrorFormatter,
19: )
20: {
21: }
22:
23: public function formatErrors(AnalysisResult $analysisResult, Output $output): int
24: {
25: $ciDetector = new CiDetector();
26:
27: try {
28: $ci = $ciDetector->detect();
29: if ($ci->getCiName() === CiDetector::CI_GITHUB_ACTIONS) {
30: return $this->githubErrorFormatter->formatErrors($analysisResult, $output);
31: } elseif ($ci->getCiName() === CiDetector::CI_TEAMCITY) {
32: return $this->teamcityErrorFormatter->formatErrors($analysisResult, $output);
33: }
34: } catch (CiNotDetectedException) {
35: // pass
36: }
37:
38: if (!$analysisResult->hasErrors() && !$analysisResult->hasWarnings()) {
39: return 0;
40: }
41:
42: return $analysisResult->getTotalErrorsCount() > 0 ? 1 : 0;
43: }
44:
45: }
46: