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 count;
9: use function usort;
10:
11: /**
12: * @api
13: */
14: final class AnalysisResult
15: {
16:
17: /** @var list<Error> sorted by their file name, line number and message */
18: private array $fileSpecificErrors;
19:
20: /**
21: * @param list<Error> $fileSpecificErrors
22: * @param list<string> $notFileSpecificErrors
23: * @param list<InternalError> $internalErrors
24: * @param list<string> $warnings
25: * @param list<CollectedData> $collectedData
26: * @param array<string, string> $changedProjectExtensionFilesOutsideOfAnalysedPaths
27: * @param list<string> $processedFiles
28: */
29: public function __construct(
30: array $fileSpecificErrors,
31: private array $notFileSpecificErrors,
32: private array $internalErrors,
33: private array $warnings,
34: private array $collectedData,
35: private bool $defaultLevelUsed,
36: private ?string $projectConfigFile,
37: private bool $savedResultCache,
38: private int $peakMemoryUsageBytes,
39: private bool $isResultCacheUsed,
40: private array $changedProjectExtensionFilesOutsideOfAnalysedPaths,
41: private array $processedFiles = [],
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: * @return list<InternalError>
88: */
89: public function getInternalErrorObjects(): array
90: {
91: return $this->internalErrors;
92: }
93:
94: /**
95: * @return list<string>
96: */
97: public function getWarnings(): array
98: {
99: return $this->warnings;
100: }
101:
102: public function hasWarnings(): bool
103: {
104: return count($this->warnings) > 0;
105: }
106:
107: /**
108: * @return list<CollectedData>
109: */
110: public function getCollectedData(): array
111: {
112: return $this->collectedData;
113: }
114:
115: public function isDefaultLevelUsed(): bool
116: {
117: return $this->defaultLevelUsed;
118: }
119:
120: public function getProjectConfigFile(): ?string
121: {
122: return $this->projectConfigFile;
123: }
124:
125: public function hasInternalErrors(): bool
126: {
127: return count($this->internalErrors) > 0;
128: }
129:
130: public function isResultCacheSaved(): bool
131: {
132: return $this->savedResultCache;
133: }
134:
135: public function getPeakMemoryUsageBytes(): int
136: {
137: return $this->peakMemoryUsageBytes;
138: }
139:
140: public function isResultCacheUsed(): bool
141: {
142: return $this->isResultCacheUsed;
143: }
144:
145: /**
146: * @return array<string, string>
147: */
148: public function getChangedProjectExtensionFilesOutsideOfAnalysedPaths(): array
149: {
150: return $this->changedProjectExtensionFilesOutsideOfAnalysedPaths;
151: }
152:
153: /**
154: * @return list<string>
155: */
156: public function getProcessedFiles(): array
157: {
158: return $this->processedFiles;
159: }
160:
161: /**
162: * @api
163: * @param list<Error> $fileSpecificErrors
164: */
165: public function withFileSpecificErrors(array $fileSpecificErrors): self
166: {
167: return new self(
168: $fileSpecificErrors,
169: $this->notFileSpecificErrors,
170: $this->internalErrors,
171: $this->warnings,
172: $this->collectedData,
173: $this->defaultLevelUsed,
174: $this->projectConfigFile,
175: $this->savedResultCache,
176: $this->peakMemoryUsageBytes,
177: $this->isResultCacheUsed,
178: $this->changedProjectExtensionFilesOutsideOfAnalysedPaths,
179: $this->processedFiles,
180: );
181: }
182:
183: }
184: