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