Skip to content

Commit

Permalink
implement cache for check:import
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Sep 14, 2024
1 parent ba417b6 commit 07f4ad1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"composer/class-map-generator": "^1.0.0",
"imanghafoori/php-abstract-filesystem": "^0.1.5",
"imanghafoori/php-search-replace": "^1.1.12",
"imanghafoori/php-token-analyzer": "^0.1.73",
"imanghafoori/php-token-analyzer": "^0.1.83",
"imanghafoori/php-imports-analyzer": "^1.0.6",
"imanghafoori/smart-realtime-facades": "^1.1.8",
"jetbrains/phpstorm-attributes": "1.*",
Expand Down
28 changes: 15 additions & 13 deletions src/Features/CheckImports/CheckImportsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public function handle()
CheckClassReferencesAreValid::$wrongClassRefsHandler = PrintWrongClassRefs::class;
}

CheckClassReferencesAreValid::$cache = cache()->get('micro_imports__223r') ?: [];

if ($this->option('force')) {
FacadeAliasReplacer::$forceReplace = true;
}
Expand All @@ -86,19 +88,15 @@ public function handle()
$folder = ltrim($this->option('folder'), '=');
$folder = rtrim($folder, '/\\');

$routeFiles = FilePath::removeExtraPaths(
RoutePaths::get(),
$folder,
$fileName
);
$routeFiles = FilePath::removeExtraPaths(RoutePaths::get(), $folder, $fileName);

$autoloadedFilesGen = FilePath::removeExtraPaths(
ComposerJson::autoloadedFilesList(base_path()),
$folder,
$fileName
);

$paramProvider = $this->getParamProvider();
$paramProvider = self::getParamProvider();

$checks = $this->checks;
unset($checks[1]);
Expand All @@ -110,7 +108,7 @@ public function handle()

$foldersStats = FileIterators::checkFolders(
$checks,
$this->getLaravelFolders(),
self::getLaravelFolders(),
$paramProvider,
$fileName,
$folder
Expand All @@ -131,7 +129,7 @@ public function handle()
$messages[0] = CheckImportReporter::totalImportsMsg();
$messages[1] = Reporters\Psr4Report::printAutoload($psr4Stats, $classMapStats);
$messages[2] = CheckImportReporter::header();
$messages[3] = $this->getFilesStats();
$messages[3] = self::getFilesStats();
$messages[4] = Reporters\BladeReport::getBladeStats($bladeStats);
$messages[5] = Reporters\LaravelFoldersReport::foldersStats($foldersStats);
$messages[6] = CheckImportReporter::getRouteStats($routeFiles);
Expand All @@ -148,15 +146,19 @@ public function handle()
$errorPrinter->printTime();

if (Thanks::shouldShow()) {
$this->printThanks($this);
self::printThanks($this);
}

cache()->rememberForever('microscope_imports_252r', function () {
return CheckClassReferencesAreValid::$cache;
});

$this->line('');

return ErrorCounter::getTotalErrors() > 0 ? 1 : 0;
}

private function printThanks($command)
private static function printThanks($command)
{
$command->line(PHP_EOL);
foreach (Thanks::messages() as $msg) {
Expand All @@ -167,7 +169,7 @@ private function printThanks($command)
/**
* @return \Closure
*/
private function getParamProvider()
private static function getParamProvider()
{
return function (PhpFileDescriptor $file) {
$imports = ParseUseStatement::parseUseStatements($file->getTokens());
Expand All @@ -176,7 +178,7 @@ private function getParamProvider()
};
}

private function getFilesStats()
private static function getFilesStats()
{
$filesCount = ChecksOnPsr4Classes::$checkedFilesCount;

Expand All @@ -186,7 +188,7 @@ private function getFilesStats()
/**
* @return array<string, \Generator>
*/
private function getLaravelFolders()
private static function getLaravelFolders()
{
return [
'config' => LaravelPaths::configDirs(),
Expand Down
39 changes: 32 additions & 7 deletions src/Features/CheckImports/Checks/CheckClassReferencesAreValid.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Imanghafoori\LaravelMicroscope\Features\CheckImports\Checks;

use Closure;
use Imanghafoori\LaravelMicroscope\Check;
use Imanghafoori\LaravelMicroscope\Features\CheckImports\Handlers;
use Imanghafoori\LaravelMicroscope\Foundations\PhpFileDescriptor;
Expand All @@ -13,6 +14,8 @@ class CheckClassReferencesAreValid implements Check

public static $checkExtra = true;

public static $cache = [];

public static $extraCorrectImportsHandler = Handlers\ExtraCorrectImports::class;

public static $extraWrongImportsHandler = Handlers\ExtraWrongImports::class;
Expand All @@ -21,17 +24,34 @@ class CheckClassReferencesAreValid implements Check

public static function check(PhpFileDescriptor $file, $imports = [])
{
$tokens = $file->getTokens();
loopStart:
$md5 = $file->getMd5();
$absFilePath = $file->getAbsolutePath();

loopStart:
$tokens = $file->getTokens();
$refFinder = function () use ($file, $tokens, $imports) {
$absFilePath = $file->getAbsolutePath();

return ImportsAnalyzer::findClassRefs($tokens, $absFilePath, $imports);
};

if (count($tokens) > 100) {
$refFinder = function () use ($md5, $refFinder) {
return self::getForever($md5, $refFinder);
};
}

[
$classReferences,
$hostNamespace,
$extraWrongImports,
$extraCorrectImports,
$wrongClassRefs,
$wrongDocblockRefs,
] = ImportsAnalyzer::getWrongRefs($tokens, $absFilePath, $imports);
$extraImports,
$docblockRefs,
$attributeReferences,
] = $refFinder();

[$wrongClassRefs] = ImportsAnalyzer::filterWrongClassRefs($classReferences, $absFilePath);
[$wrongDocblockRefs] = ImportsAnalyzer::filterWrongClassRefs($docblockRefs, $absFilePath);
[$extraWrongImports, $extraCorrectImports] = ImportsAnalyzer::filterWrongClassRefs($extraImports, $absFilePath);

if (self::$checkWrong && self::$wrongClassRefsHandler) {
[$tokens, $isFixed] = self::$wrongClassRefsHandler::handle(
Expand Down Expand Up @@ -63,4 +83,9 @@ private static function handleExtraImports($absFilePath, $extraWrongImports, $ex
self::$extraCorrectImportsHandler::handle($extraCorrectImports, $absFilePath);
}
}

public static function getForever($md5, Closure $refFinder)
{
return self::$cache[$md5] ?? (self::$cache[$md5] = $refFinder());
}
}

0 comments on commit 07f4ad1

Please sign in to comment.