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