Skip to content

Commit

Permalink
Lazy initialization of AnalyserResult::$errors
Browse files Browse the repository at this point in the history
  • Loading branch information
takaram committed May 13, 2023
1 parent 7bb15fa commit 732517e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
38 changes: 20 additions & 18 deletions src/Analyser/AnalyserResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
class AnalyserResult
{

/** @var list<Error> */
private array $unorderedErrors;
/** @var list<Error>|null */
private ?array $errors = null;

/**
* @param list<Error> $errors
* @param list<Error> $unorderedErrors
* @param list<CollectedData> $collectedData
* @param list<string> $internalErrors
* @param array<string, array<string>>|null $dependencies
* @param array<string, array<RootExportedNode>> $exportedNodes
*/
public function __construct(
private array $errors,
private array $unorderedErrors,
private array $internalErrors,
private array $collectedData,
private ?array $dependencies,
Expand All @@ -29,20 +29,6 @@ public function __construct(
private int $peakMemoryUsageBytes,
)
{
$this->unorderedErrors = $errors;

usort(
$this->errors,
static fn (Error $a, Error $b): int => [
$a->getFile(),
$a->getLine(),
$a->getMessage(),
] <=> [
$b->getFile(),
$b->getLine(),
$b->getMessage(),
],
);
}

/**
Expand All @@ -58,6 +44,22 @@ public function getUnorderedErrors(): array
*/
public function getErrors(): array
{
if (!isset($this->errors)) {
$this->errors = $this->unorderedErrors;
usort(
$this->errors,
static fn (Error $a, Error $b): int => [
$a->getFile(),
$a->getLine(),
$a->getMessage(),
] <=> [
$b->getFile(),
$b->getLine(),
$b->getMessage(),
],
);
}

return $this->errors;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Command/AnalyseApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function analyse(
if ($resultCache->isFullAnalysis() && count($projectStubFiles) !== 0) {
$stubErrors = $this->stubValidator->validate($projectStubFiles, $debug);
$intermediateAnalyserResult = new AnalyserResult(
array_merge($intermediateAnalyserResult->getErrors(), $stubErrors),
array_merge($intermediateAnalyserResult->getUnorderedErrors(), $stubErrors),
$intermediateAnalyserResult->getInternalErrors(),
$intermediateAnalyserResult->getCollectedData(),
$intermediateAnalyserResult->getDependencies(),
Expand Down

0 comments on commit 732517e

Please sign in to comment.