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