Skip to content

Commit 8329c64

Browse files
takaramondrejmirtes
authored andcommitted
Lazy initialization of AnalyserResult::$errors
1 parent 9b64a6d commit 8329c64

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

src/Analyser/AnalyserResult.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
class AnalyserResult
1010
{
1111

12-
/** @var list<Error> */
13-
private array $unorderedErrors;
12+
/** @var list<Error>|null */
13+
private ?array $errors = null;
1414

1515
/**
16-
* @param list<Error> $errors
16+
* @param list<Error> $unorderedErrors
1717
* @param list<CollectedData> $collectedData
1818
* @param list<string> $internalErrors
1919
* @param array<string, array<string>>|null $dependencies
2020
* @param array<string, array<RootExportedNode>> $exportedNodes
2121
*/
2222
public function __construct(
23-
private array $errors,
23+
private array $unorderedErrors,
2424
private array $internalErrors,
2525
private array $collectedData,
2626
private ?array $dependencies,
@@ -29,20 +29,6 @@ public function __construct(
2929
private int $peakMemoryUsageBytes,
3030
)
3131
{
32-
$this->unorderedErrors = $errors;
33-
34-
usort(
35-
$this->errors,
36-
static fn (Error $a, Error $b): int => [
37-
$a->getFile(),
38-
$a->getLine(),
39-
$a->getMessage(),
40-
] <=> [
41-
$b->getFile(),
42-
$b->getLine(),
43-
$b->getMessage(),
44-
],
45-
);
4632
}
4733

4834
/**
@@ -58,6 +44,22 @@ public function getUnorderedErrors(): array
5844
*/
5945
public function getErrors(): array
6046
{
47+
if (!isset($this->errors)) {
48+
$this->errors = $this->unorderedErrors;
49+
usort(
50+
$this->errors,
51+
static fn (Error $a, Error $b): int => [
52+
$a->getFile(),
53+
$a->getLine(),
54+
$a->getMessage(),
55+
] <=> [
56+
$b->getFile(),
57+
$b->getLine(),
58+
$b->getMessage(),
59+
],
60+
);
61+
}
62+
6163
return $this->errors;
6264
}
6365

src/Command/AnalyseApplication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function analyse(
9090
if ($resultCache->isFullAnalysis() && count($projectStubFiles) !== 0) {
9191
$stubErrors = $this->stubValidator->validate($projectStubFiles, $debug);
9292
$intermediateAnalyserResult = new AnalyserResult(
93-
array_merge($intermediateAnalyserResult->getErrors(), $stubErrors),
93+
array_merge($intermediateAnalyserResult->getUnorderedErrors(), $stubErrors),
9494
$intermediateAnalyserResult->getInternalErrors(),
9595
$intermediateAnalyserResult->getCollectedData(),
9696
$intermediateAnalyserResult->getDependencies(),

0 commit comments

Comments
 (0)