diff --git a/composer.json b/composer.json index 5a53b3eb..f19cb6ca 100644 --- a/composer.json +++ b/composer.json @@ -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.*", diff --git a/src/Features/CheckImports/CheckImportsCommand.php b/src/Features/CheckImports/CheckImportsCommand.php index 8c86f453..74435bd1 100644 --- a/src/Features/CheckImports/CheckImportsCommand.php +++ b/src/Features/CheckImports/CheckImportsCommand.php @@ -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; } @@ -86,11 +88,7 @@ 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()), @@ -98,7 +96,7 @@ public function handle() $fileName ); - $paramProvider = $this->getParamProvider(); + $paramProvider = self::getParamProvider(); $checks = $this->checks; unset($checks[1]); @@ -110,7 +108,7 @@ public function handle() $foldersStats = FileIterators::checkFolders( $checks, - $this->getLaravelFolders(), + self::getLaravelFolders(), $paramProvider, $fileName, $folder @@ -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); @@ -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) { @@ -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()); @@ -176,7 +178,7 @@ private function getParamProvider() }; } - private function getFilesStats() + private static function getFilesStats() { $filesCount = ChecksOnPsr4Classes::$checkedFilesCount; @@ -186,7 +188,7 @@ private function getFilesStats() /** * @return array */ - private function getLaravelFolders() + private static function getLaravelFolders() { return [ 'config' => LaravelPaths::configDirs(), diff --git a/src/Features/CheckImports/Checks/CheckClassReferencesAreValid.php b/src/Features/CheckImports/Checks/CheckClassReferencesAreValid.php index 2e9bd4de..df31f64a 100644 --- a/src/Features/CheckImports/Checks/CheckClassReferencesAreValid.php +++ b/src/Features/CheckImports/Checks/CheckClassReferencesAreValid.php @@ -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; @@ -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; @@ -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( @@ -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()); + } }