Skip to content

Commit 67b2d1c

Browse files
committed
Optimization
1 parent c7e18d5 commit 67b2d1c

File tree

5 files changed

+32
-19
lines changed

5 files changed

+32
-19
lines changed

src/Analyser/ResultCache/ResultCache.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class ResultCache
1515

1616
private int $lastFullAnalysisTime;
1717

18+
/** @var mixed[] */
19+
private array $meta;
20+
1821
/** @var array<string, array<Error>> */
1922
private array $errors;
2023

@@ -28,6 +31,7 @@ class ResultCache
2831
* @param string[] $filesToAnalyse
2932
* @param bool $fullAnalysis
3033
* @param int $lastFullAnalysisTime
34+
* @param mixed[] $meta
3135
* @param array<string, array<Error>> $errors
3236
* @param array<string, array<string>> $dependencies
3337
* @param array<string, array<ExportedNode>> $exportedNodes
@@ -36,6 +40,7 @@ public function __construct(
3640
array $filesToAnalyse,
3741
bool $fullAnalysis,
3842
int $lastFullAnalysisTime,
43+
array $meta,
3944
array $errors,
4045
array $dependencies,
4146
array $exportedNodes
@@ -44,6 +49,7 @@ public function __construct(
4449
$this->filesToAnalyse = $filesToAnalyse;
4550
$this->fullAnalysis = $fullAnalysis;
4651
$this->lastFullAnalysisTime = $lastFullAnalysisTime;
52+
$this->meta = $meta;
4753
$this->errors = $errors;
4854
$this->dependencies = $dependencies;
4955
$this->exportedNodes = $exportedNodes;
@@ -67,6 +73,14 @@ public function getLastFullAnalysisTime(): int
6773
return $this->lastFullAnalysisTime;
6874
}
6975

76+
/**
77+
* @return mixed[]
78+
*/
79+
public function getMeta(): array
80+
{
81+
return $this->meta;
82+
}
83+
7084
/**
7185
* @return array<string, array<Error>>
7286
*/

src/Analyser/ResultCache/ResultCacheManager.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ?
9595
if ($output->isDebug()) {
9696
$output->writeLineFormatted('Result cache not used because of debug mode.');
9797
}
98-
return new ResultCache($allAnalysedFiles, true, time(), [], [], []);
98+
return new ResultCache($allAnalysedFiles, true, time(), $this->getMeta($projectConfigArray), [], [], []);
9999
}
100100
if ($onlyFiles) {
101101
if ($output->isDebug()) {
102102
$output->writeLineFormatted('Result cache not used because only files were passed as analysed paths.');
103103
}
104-
return new ResultCache($allAnalysedFiles, true, time(), [], [], []);
104+
return new ResultCache($allAnalysedFiles, true, time(), $this->getMeta($projectConfigArray), [], [], []);
105105
}
106106

107107
$cacheFilePath = $this->cacheFilePath;
@@ -116,7 +116,7 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ?
116116
if ($output->isDebug()) {
117117
$output->writeLineFormatted('Result cache not used because the cache file does not exist.');
118118
}
119-
return new ResultCache($allAnalysedFiles, true, time(), [], [], []);
119+
return new ResultCache($allAnalysedFiles, true, time(), $this->getMeta($projectConfigArray), [], [], []);
120120
}
121121

122122
try {
@@ -128,7 +128,7 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ?
128128

129129
@unlink($cacheFilePath);
130130

131-
return new ResultCache($allAnalysedFiles, true, time(), [], [], []);
131+
return new ResultCache($allAnalysedFiles, true, time(), $this->getMeta($projectConfigArray), [], [], []);
132132
}
133133

134134
if (!is_array($data)) {
@@ -137,22 +137,23 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ?
137137
$output->writeLineFormatted('Result cache not used because the cache file is corrupted.');
138138
}
139139

140-
return new ResultCache($allAnalysedFiles, true, time(), [], [], []);
140+
return new ResultCache($allAnalysedFiles, true, time(), $this->getMeta($projectConfigArray), [], [], []);
141141
}
142142

143-
if ($data['meta'] !== $this->getMeta($projectConfigArray)) {
143+
$meta = $this->getMeta($projectConfigArray);
144+
if ($data['meta'] !== $meta) {
144145
if ($output->isDebug()) {
145146
$output->writeLineFormatted('Result cache not used because the metadata do not match.');
146147
}
147-
return new ResultCache($allAnalysedFiles, true, time(), [], [], []);
148+
return new ResultCache($allAnalysedFiles, true, time(), $meta, [], [], []);
148149
}
149150

150151
if (time() - $data['lastFullAnalysisTime'] >= 60 * 60 * 24 * 7) {
151152
if ($output->isDebug()) {
152153
$output->writeLineFormatted('Result cache not used because it\'s more than 7 days since last full analysis.');
153154
}
154155
// run full analysis if the result cache is older than 7 days
155-
return new ResultCache($allAnalysedFiles, true, time(), [], [], []);
156+
return new ResultCache($allAnalysedFiles, true, time(), $meta, [], [], []);
156157
}
157158

158159
$invertedDependencies = $data['dependencies'];
@@ -232,7 +233,7 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ?
232233
}
233234
}
234235

235-
return new ResultCache(array_unique($filesToAnalyse), false, $data['lastFullAnalysisTime'], $filteredErrors, $invertedDependenciesToReturn, $filteredExportedNodes);
236+
return new ResultCache(array_unique($filesToAnalyse), false, $data['lastFullAnalysisTime'], $meta, $filteredErrors, $invertedDependenciesToReturn, $filteredExportedNodes);
236237
}
237238

238239
/**
@@ -263,20 +264,20 @@ private function exportedNodesChanged(string $analysedFile, array $cachedFileExp
263264
/**
264265
* @param AnalyserResult $analyserResult
265266
* @param ResultCache $resultCache
266-
* @param mixed[]|null $projectConfigArray
267267
* @param bool|string $save
268268
* @return ResultCacheProcessResult
269269
* @throws \PHPStan\ShouldNotHappenException
270270
*/
271-
public function process(AnalyserResult $analyserResult, ResultCache $resultCache, Output $output, bool $onlyFiles, ?array $projectConfigArray, $save): ResultCacheProcessResult
271+
public function process(AnalyserResult $analyserResult, ResultCache $resultCache, Output $output, bool $onlyFiles, $save): ResultCacheProcessResult
272272
{
273273
$internalErrors = $analyserResult->getInternalErrors();
274274
$freshErrorsByFile = [];
275275
foreach ($analyserResult->getErrors() as $error) {
276276
$freshErrorsByFile[$error->getFilePath()][] = $error;
277277
}
278278

279-
$doSave = function (array $errorsByFile, ?array $dependencies, array $exportedNodes, ?string $resultCacheName) use ($internalErrors, $resultCache, $output, $onlyFiles, $projectConfigArray): bool {
279+
$meta = $resultCache->getMeta();
280+
$doSave = function (array $errorsByFile, ?array $dependencies, array $exportedNodes, ?string $resultCacheName) use ($internalErrors, $resultCache, $output, $onlyFiles, $meta): bool {
280281
if ($onlyFiles) {
281282
if ($output->isDebug()) {
282283
$output->writeLineFormatted('Result cache was not saved because only files were passed as analysed paths.');
@@ -311,7 +312,7 @@ public function process(AnalyserResult $analyserResult, ResultCache $resultCache
311312
}
312313
}
313314

314-
$this->save($resultCache->getLastFullAnalysisTime(), $resultCacheName, $errorsByFile, $dependencies, $exportedNodes, $projectConfigArray);
315+
$this->save($resultCache->getLastFullAnalysisTime(), $resultCacheName, $errorsByFile, $dependencies, $exportedNodes, $meta);
315316

316317
if ($output->isDebug()) {
317318
$output->writeLineFormatted('Result cache is saved.');
@@ -445,15 +446,15 @@ private function mergeExportedNodes(ResultCache $resultCache, array $freshExport
445446
* @param array<string, array<Error>> $errors
446447
* @param array<string, array<string>> $dependencies
447448
* @param array<string, array<ExportedNode>> $exportedNodes
448-
* @param mixed[]|null $projectConfigArray
449+
* @param mixed[] $meta
449450
*/
450451
private function save(
451452
int $lastFullAnalysisTime,
452453
?string $resultCacheName,
453454
array $errors,
454455
array $dependencies,
455456
array $exportedNodes,
456-
?array $projectConfigArray
457+
array $meta
457458
): void
458459
{
459460
$invertedDependencies = [];
@@ -519,7 +520,7 @@ private function save(
519520
sprintf(
520521
$template,
521522
var_export($lastFullAnalysisTime, true),
522-
var_export($this->getMeta($projectConfigArray), true),
523+
var_export($meta, true),
523524
var_export($errors, true),
524525
var_export($invertedDependencies, true),
525526
var_export($exportedNodes, true)

src/Command/AnalyseApplication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function analyse(
109109
$errorOutput,
110110
$input
111111
);
112-
$resultCacheResult = $resultCacheManager->process($intermediateAnalyserResult, $resultCache, $errorOutput, $onlyFiles, $projectConfigArray, true);
112+
$resultCacheResult = $resultCacheManager->process($intermediateAnalyserResult, $resultCache, $errorOutput, $onlyFiles, true);
113113
$analyserResult = $resultCacheResult->getAnalyserResult();
114114
$internalErrors = $analyserResult->getInternalErrors();
115115
$errors = $ignoredErrorHelperResult->process($analyserResult->getErrors(), $onlyFiles, $files, count($internalErrors) > 0 || $analyserResult->hasReachedInternalErrorsCountLimit());

src/Command/FixerApplication.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,6 @@ private function reanalyseAfterFileChanges(
517517
$resultCache,
518518
$inceptionResult->getErrorOutput(),
519519
false,
520-
$projectConfigArray,
521520
true
522521
)->getAnalyserResult();
523522
$intermediateErrors = $ignoredErrorHelperResult->process(

src/Command/FixerWorkerCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
162162
$resultCache,
163163
$inceptionResult->getErrorOutput(),
164164
false,
165-
$projectConfigArray,
166165
is_string($saveResultCache) ? $saveResultCache : $saveResultCache === null
167166
)->getAnalyserResult();
168167

0 commit comments

Comments
 (0)