1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Command;
4:
5: use Closure;
6: use PHPStan\Analyser\Error;
7: use PHPStan\Analyser\InternalError;
8: use PHPStan\Collectors\CollectedData;
9: use function count;
10: use function usort;
11:
12: /**
13: * @api
14: */
15: final class AnalysisResult
16: {
17:
18: /** @var list<Error> sorted by their file name, line number and message */
19: private array $fileSpecificErrors;
20:
21: /**
22: * @param list<Error> $fileSpecificErrors
23: * @param list<string> $notFileSpecificErrors
24: * @param list<InternalError> $internalErrors
25: * @param list<string> $warnings
26: * @param list<CollectedData>|(Closure(): list<CollectedData>) $collectedData A closure defers decoding the result cache until asked
27: * @param list<string> $processedFiles
28: * @param list<Error> $fixableIgnoredErrors ignored errors that --fix-ignored can fix
29: */
30: public function __construct(
31: array $fileSpecificErrors,
32: private array $notFileSpecificErrors,
33: private array $internalErrors,
34: private array $warnings,
35: private array|Closure $collectedData,
36: private bool $defaultLevelUsed,
37: private ?string $projectConfigFile,
38: private bool $savedResultCache,
39: private int $peakMemoryUsageBytes,
40: private bool $isResultCacheUsed,
41: private array $processedFiles = [],
42: private bool $resultCacheExisted = true,
43: private int $workerCount = 0,
44: private array $fixableIgnoredErrors = [],
45: )
46: {
47: usort(
48: $fileSpecificErrors,
49: static fn (Error $a, Error $b): int => [
50: $a->getFile(),
51: $a->getLine(),
52: $a->getMessage(),
53: ] <=> [
54: $b->getFile(),
55: $b->getLine(),
56: $b->getMessage(),
57: ],
58: );
59:
60: $this->fileSpecificErrors = $fileSpecificErrors;
61: }
62:
63: public function hasErrors(): bool
64: {
65: return $this->getTotalErrorsCount() > 0;
66: }
67:
68: public function getTotalErrorsCount(): int
69: {
70: return count($this->fileSpecificErrors) + count($this->notFileSpecificErrors);
71: }
72:
73: /**
74: * @return list<Error> sorted by their file name, line number and message
75: */
76: public function getFileSpecificErrors(): array
77: {
78: return $this->fileSpecificErrors;
79: }
80:
81: /**
82: * @return list<string>
83: */
84: public function getNotFileSpecificErrors(): array
85: {
86: return $this->notFileSpecificErrors;
87: }
88:
89: /**
90: * @return list<InternalError>
91: */
92: public function getInternalErrorObjects(): array
93: {
94: return $this->internalErrors;
95: }
96:
97: /**
98: * @return list<string>
99: */
100: public function getWarnings(): array
101: {
102: return $this->warnings;
103: }
104:
105: public function hasWarnings(): bool
106: {
107: return count($this->warnings) > 0;
108: }
109:
110: /**
111: * @return list<CollectedData>
112: */
113: public function getCollectedData(): array
114: {
115: if ($this->collectedData instanceof Closure) {
116: $this->collectedData = ($this->collectedData)();
117: }
118:
119: return $this->collectedData;
120: }
121:
122: public function isDefaultLevelUsed(): bool
123: {
124: return $this->defaultLevelUsed;
125: }
126:
127: public function getProjectConfigFile(): ?string
128: {
129: return $this->projectConfigFile;
130: }
131:
132: public function hasInternalErrors(): bool
133: {
134: return count($this->internalErrors) > 0;
135: }
136:
137: public function isResultCacheSaved(): bool
138: {
139: return $this->savedResultCache;
140: }
141:
142: public function getPeakMemoryUsageBytes(): int
143: {
144: return $this->peakMemoryUsageBytes;
145: }
146:
147: /**
148: * How many parallel workers produced this result; 0 when the analysis ran in
149: * the main process.
150: */
151: public function getWorkerCount(): int
152: {
153: return $this->workerCount;
154: }
155:
156: public function isResultCacheUsed(): bool
157: {
158: return $this->isResultCacheUsed;
159: }
160:
161: /**
162: * Whether the result cache file existed when PHPStan started.
163: * False means the cache was never created; a stale/invalid cache that
164: * triggered a full re-analysis still counts as existing.
165: */
166: public function resultCacheExisted(): bool
167: {
168: return $this->resultCacheExisted;
169: }
170:
171: /**
172: * @deprecated
173: * @return array<string, string>
174: */
175: public function getChangedProjectExtensionFilesOutsideOfAnalysedPaths(): array
176: {
177: return [];
178: }
179:
180: /**
181: * @return list<string>
182: */
183: public function getProcessedFiles(): array
184: {
185: return $this->processedFiles;
186: }
187:
188: /**
189: * @api
190: * @param list<Error> $fileSpecificErrors
191: */
192: public function withFileSpecificErrors(array $fileSpecificErrors): self
193: {
194: return new self(
195: $fileSpecificErrors,
196: $this->notFileSpecificErrors,
197: $this->internalErrors,
198: $this->warnings,
199: $this->collectedData,
200: $this->defaultLevelUsed,
201: $this->projectConfigFile,
202: $this->savedResultCache,
203: $this->peakMemoryUsageBytes,
204: $this->isResultCacheUsed,
205: $this->processedFiles,
206: $this->resultCacheExisted,
207: $this->workerCount,
208: $this->fixableIgnoredErrors,
209: );
210: }
211:
212: /**
213: * @api
214: * @return list<Error>
215: */
216: public function getFixableIgnoredErrors(): array
217: {
218: return $this->fixableIgnoredErrors;
219: }
220:
221: }
222: