1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Command;
4:
5: use PHPStan\Analyser\Error;
6: use PHPStan\Analyser\InternalError;
7: use PHPStan\Collectors\CollectedData;
8: use function array_map;
9: use function count;
10: use function usort;
11:
12: /**
13: * @api
14: * @final
15: */
16: class AnalysisResult
17: {
18:
19: /** @var list<Error> sorted by their file name, line number and message */
20: private array $fileSpecificErrors;
21:
22: /**
23: * @param list<Error> $fileSpecificErrors
24: * @param list<string> $notFileSpecificErrors
25: * @param list<InternalError> $internalErrors
26: * @param list<string> $warnings
27: * @param list<CollectedData> $collectedData
28: * @param array<string, string> $changedProjectExtensionFilesOutsideOfAnalysedPaths
29: */
30: public function __construct(
31: array $fileSpecificErrors,
32: private array $notFileSpecificErrors,
33: private array $internalErrors,
34: private array $warnings,
35: private array $collectedData,
36: private bool $defaultLevelUsed,
37: private ?string $projectConfigFile,
38: private bool $savedResultCache,
39: private int $peakMemoryUsageBytes,
40: private bool $isResultCacheUsed,
41: private array $changedProjectExtensionFilesOutsideOfAnalysedPaths,
42: )
43: {
44: usort(
45: $fileSpecificErrors,
46: static fn (Error $a, Error $b): int => [
47: $a->getFile(),
48: $a->getLine(),
49: $a->getMessage(),
50: ] <=> [
51: $b->getFile(),
52: $b->getLine(),
53: $b->getMessage(),
54: ],
55: );
56:
57: $this->fileSpecificErrors = $fileSpecificErrors;
58: }
59:
60: public function hasErrors(): bool
61: {
62: return $this->getTotalErrorsCount() > 0;
63: }
64:
65: public function getTotalErrorsCount(): int
66: {
67: return count($this->fileSpecificErrors) + count($this->notFileSpecificErrors);
68: }
69:
70: /**
71: * @return list<Error> sorted by their file name, line number and message
72: */
73: public function getFileSpecificErrors(): array
74: {
75: return $this->fileSpecificErrors;
76: }
77:
78: /**
79: * @return list<string>
80: */
81: public function getNotFileSpecificErrors(): array
82: {
83: return $this->notFileSpecificErrors;
84: }
85:
86: /**
87: * @deprecated Use getInternalErrorObjects
88: * @return list<string>
89: */
90: public function getInternalErrors(): array
91: {
92: return array_map(static fn (InternalError $internalError) => $internalError->getMessage(), $this->internalErrors);
93: }
94:
95: /**
96: * @return list<InternalError>
97: */
98: public function getInternalErrorObjects(): array
99: {
100: return $this->internalErrors;
101: }
102:
103: /**
104: * @return list<string>
105: */
106: public function getWarnings(): array
107: {
108: return $this->warnings;
109: }
110:
111: public function hasWarnings(): bool
112: {
113: return count($this->warnings) > 0;
114: }
115:
116: /**
117: * @return list<CollectedData>
118: */
119: public function getCollectedData(): array
120: {
121: return $this->collectedData;
122: }
123:
124: public function isDefaultLevelUsed(): bool
125: {
126: return $this->defaultLevelUsed;
127: }
128:
129: public function getProjectConfigFile(): ?string
130: {
131: return $this->projectConfigFile;
132: }
133:
134: public function hasInternalErrors(): bool
135: {
136: return count($this->internalErrors) > 0;
137: }
138:
139: public function isResultCacheSaved(): bool
140: {
141: return $this->savedResultCache;
142: }
143:
144: public function getPeakMemoryUsageBytes(): int
145: {
146: return $this->peakMemoryUsageBytes;
147: }
148:
149: public function isResultCacheUsed(): bool
150: {
151: return $this->isResultCacheUsed;
152: }
153:
154: /**
155: * @return array<string, string>
156: */
157: public function getChangedProjectExtensionFilesOutsideOfAnalysedPaths(): array
158: {
159: return $this->changedProjectExtensionFilesOutsideOfAnalysedPaths;
160: }
161:
162: }
163: