From 69692c751e9cb784530c5c94fcaa4d1d2b2a9542 Mon Sep 17 00:00:00 2001 From: Iman Ghafoori Date: Wed, 27 Dec 2023 19:31:40 +0330 Subject: [PATCH] bug fix --- .../CheckImports/CheckImportsCommand.php | 29 +++++++++---------- .../Reporters/CheckImportReporter.php | 4 +-- src/Features/CheckView/CheckViewsCommand.php | 2 +- src/FileReaders/FilePath.php | 23 +++++++-------- src/FileReaders/Paths.php | 18 +++++------- src/Iterators/BladeFiles/CheckBladePaths.php | 26 ++++++++--------- src/Iterators/ChecksOnPsr4Classes.php | 14 ++++----- src/Iterators/FiltersFiles.php | 6 ++-- 8 files changed, 56 insertions(+), 66 deletions(-) diff --git a/src/Features/CheckImports/CheckImportsCommand.php b/src/Features/CheckImports/CheckImportsCommand.php index 567c47ee..7cc8aad5 100644 --- a/src/Features/CheckImports/CheckImportsCommand.php +++ b/src/Features/CheckImports/CheckImportsCommand.php @@ -83,9 +83,9 @@ public function handle() $folder = ltrim($this->option('folder'), '='); $folder = rtrim($folder, '/\\'); - $routeFiles = FilePath::removeExtraPaths(RoutePaths::get(), $fileName, $folder); + $routeFiles = FilePath::removeExtraPaths(RoutePaths::get(), $folder, $fileName); $classMapFiles = ComposerJson::getClassMaps(base_path()); - $autoloadedFiles = FilePath::removeExtraPaths(ComposerJson::autoloadedFilesList(base_path()), $fileName, $folder); + $autoloadedFiles = FilePath::removeExtraPaths(ComposerJson::autoloadedFilesList(base_path()), $folder, $fileName); $paramProvider = $this->getParamProvider(); @@ -110,27 +110,26 @@ public function handle() $filesCount = ChecksOnPsr4Classes::$checkedFilesCount; $refCount = ImportsAnalyzer::$checkedRefCount; $errorPrinter = ErrorPrinter::singleton($this->output); - - $messages = []; - $messages[] = Reporters\CheckImportReporter::totalImportsMsg($refCount); - Reporters\Psr4Report::$callback = function () use ($errorPrinter) { $errorPrinter->flushErrors(); }; - $messages[] = Reporters\Psr4Report::printAutoload($psr4Stats, $classMapStats); - $messages[] = CheckImportReporter::header(); - $filesCount && $messages[] = Reporters\CheckImportReporter::getFilesStats($filesCount); - $messages[] = Reporters\BladeReport::getBladeStats($bladeStats); - $messages[] = Reporters\LaravelFoldersReport::foldersStats($foldersStats); - $routeFiles && $messages[] = CheckImportReporter::getRouteStats($routeFiles); + $messages = []; + $messages[0] = Reporters\CheckImportReporter::totalImportsMsg(); + $messages[1] = Reporters\Psr4Report::printAutoload($psr4Stats, $classMapStats); + $messages[2] = CheckImportReporter::header(); + $filesCount && $messages[3] = Reporters\CheckImportReporter::getFilesStats($filesCount); + $messages[4] = Reporters\BladeReport::getBladeStats($bladeStats); + $messages[5] = Reporters\LaravelFoldersReport::foldersStats($foldersStats); + + $routeFiles && $messages[6] = CheckImportReporter::getRouteStats($routeFiles); $count = iterator_to_array($autoloadedFiles); - $count && $messages[] = CheckImportReporter::getAutoloadedFiles($count); + $count && $messages[7] = CheckImportReporter::getAutoloadedFiles($count); - $messages[] = Reporters\SummeryReport::summery($errorPrinter->errorsCounts); + $messages[8] = Reporters\SummeryReport::summery($errorPrinter->errorsCounts); - if (! $refCount) { + if (! ImportsAnalyzer::$checkedRefCount) { $messages = ['No imports were found! with filter: "'.($fileName ?: $folder).'"']; } diff --git a/src/Features/CheckImports/Reporters/CheckImportReporter.php b/src/Features/CheckImports/Reporters/CheckImportReporter.php index 0339197e..e6725a0d 100644 --- a/src/Features/CheckImports/Reporters/CheckImportReporter.php +++ b/src/Features/CheckImports/Reporters/CheckImportReporter.php @@ -6,9 +6,9 @@ class CheckImportReporter { use Reporting; - public static function totalImportsMsg($refCount) + public static function totalImportsMsg() { - return ''.$refCount.' imports were checked under:'; + return 'Imports were checked under:'; } public static function getRouteStats($count) diff --git a/src/Features/CheckView/CheckViewsCommand.php b/src/Features/CheckView/CheckViewsCommand.php index 547a5644..d900deb7 100644 --- a/src/Features/CheckView/CheckViewsCommand.php +++ b/src/Features/CheckView/CheckViewsCommand.php @@ -27,7 +27,7 @@ public function handle(ErrorPrinter $errorPrinter) $errorPrinter->printer = $this->output; $this->checkRoutePaths( - FilePath::removeExtraPaths(RoutePaths::get(), $fileName, $folder) + FilePath::removeExtraPaths(RoutePaths::get(), $folder, $fileName) ); $this->checkPsr4($fileName, $folder); $this->checkBladeFiles(); diff --git a/src/FileReaders/FilePath.php b/src/FileReaders/FilePath.php index 094df780..47bb18e6 100644 --- a/src/FileReaders/FilePath.php +++ b/src/FileReaders/FilePath.php @@ -66,10 +66,7 @@ public static function getAllPhpFiles($path, $basePath = '') $path = $basePath.DIRECTORY_SEPARATOR.$path; try { - $finder = Finder::create()->files()->name(self::$fileName.'.php')->in($path); - self::$directory && $finder->path(self::$directory); - - return $finder; + return Finder::create()->files()->name(self::$fileName.'.php')->in($path); } catch (Exception $e) { return []; } @@ -83,19 +80,19 @@ public static function getFolderFile($absFilePath): array return [$fileName, implode('/', $segments)]; } - public static function contains($absFilePath, $excludeFile, $excludeFolder) + public static function contains($filePath, $folder, $file) { - if (! $excludeFile && ! $excludeFolder) { + if (! $file && ! $folder) { return true; } - [$fileName, $folderPath] = self::getFolderFile($absFilePath); + [$fileName, $folderPath] = self::getFolderFile($filePath); - if ($excludeFile && mb_strpos($fileName, $excludeFile) !== false) { + if ($file && mb_strpos($fileName, $file) !== false) { return true; } - if ($excludeFolder && mb_strpos($folderPath, $excludeFolder) !== false) { + if ($folder && mb_strpos($folderPath, $folder) !== false) { return true; } @@ -104,14 +101,14 @@ public static function contains($absFilePath, $excludeFile, $excludeFolder) /** * @param $paths - * @param $includeFile - * @param $includeFolder + * @param $file + * @param $folder * @return \Generator */ - public static function removeExtraPaths($paths, $includeFile, $includeFolder) + public static function removeExtraPaths($paths, $folder, $file) { foreach ($paths as $absFilePath) { - if (self::contains($absFilePath, $includeFile, $includeFolder)) { + if (self::contains($absFilePath, $folder, $file)) { yield $absFilePath; } } diff --git a/src/FileReaders/Paths.php b/src/FileReaders/Paths.php index 3cb6a0c9..16de3db7 100644 --- a/src/FileReaders/Paths.php +++ b/src/FileReaders/Paths.php @@ -3,10 +3,12 @@ namespace Imanghafoori\LaravelMicroscope\FileReaders; use Exception; +use Imanghafoori\LaravelMicroscope\Iterators\FiltersFiles; use Symfony\Component\Finder\Finder; class Paths { + use FiltersFiles; /** * @param $dirs * @param null|string $includeFileName @@ -22,27 +24,21 @@ public static function getAbsFilePaths($dirs, $includeFileName = null, $includeF $includeFolder && ($includeFolder = str_replace('\\', '/', $includeFolder)); is_string($dirs) && ($dirs = [$dirs]); foreach ($dirs as $dir) { - yield $dir => self::getPathsInDir($dir, $includeFileName, $includeFolder); + yield $dir => self::filterFiles(self::getPathsInDir($dir, $includeFileName), $includeFolder); } } /** * @param $dir * @param $fileName - * @param $folder - * @return \iterable + * @return \Symfony\Component\Finder\Finder */ - private static function getPathsInDir($dir, $fileName, $folder) + private static function getPathsInDir($dir, $fileName) { try { - $files = Finder::create()->files()->name(($fileName ?: '*').'.php')->in($dir); - foreach ($files as $absFilePath => $f) { - if (FilePath::contains($absFilePath, $fileName, $folder)) { - yield $absFilePath; - } - } + return Finder::create()->files()->name(($fileName ?: '*').'.php')->in($dir); } catch (Exception $e) { - return []; + return Finder::create(); } } } diff --git a/src/Iterators/BladeFiles/CheckBladePaths.php b/src/Iterators/BladeFiles/CheckBladePaths.php index 70f46738..fc6f9d2f 100644 --- a/src/Iterators/BladeFiles/CheckBladePaths.php +++ b/src/Iterators/BladeFiles/CheckBladePaths.php @@ -13,37 +13,35 @@ class CheckBladePaths public static $scanned = []; /** - * @param \Generator $paths + * @param \Generator $dirs * @param array $checkers * @param string $includeFile * @param string $includeFolder * @param array|callable $params * @return \Generator */ - public static function checkPaths($paths, $checkers, $includeFile, $includeFolder, $params) + public static function checkPaths($dirs, $checkers, $includeFile, $includeFolder, $params) { - foreach (self::filterUnwantedBlades($paths) as $path) { - $files = self::findFiles($path, $includeFile, $includeFolder); - $count = self::applyChecks($files, $params, $checkers); + foreach (self::filterUnwantedBlades($dirs) as $dirPath) { + $finder = self::findFiles($dirPath, $includeFile); + $filteredFiles = self::filterFiles($finder, $includeFolder); + $count = self::applyChecks($filteredFiles, $params, $checkers); - yield $path => $count; + yield $dirPath => $count; } } /** - * @param string $path - * @return \IteratorAggregate + * @param string $path + * @param null $fileName + * @return \Symfony\Component\Finder\Finder */ - public static function findFiles($path, $fileName = null, $folderName = null): Finder + public static function findFiles($path, $fileName = null): Finder { - $finder = Finder::create() + return Finder::create() ->name(($fileName ?: '*').'.blade.php') ->files() ->in($path); - - $folderName && $finder->path($folderName); - - return $finder; } /** diff --git a/src/Iterators/ChecksOnPsr4Classes.php b/src/Iterators/ChecksOnPsr4Classes.php index 59fb3d79..e6febd76 100644 --- a/src/Iterators/ChecksOnPsr4Classes.php +++ b/src/Iterators/ChecksOnPsr4Classes.php @@ -31,7 +31,7 @@ public static function apply($checks, $params, $includeFile, $includeFolder) $includeFolder && FilePath::$directory = $includeFolder; $stats = []; foreach (ComposerJson::readAutoload() as $composerPath => $psr4) { - $stats[$composerPath] = self::processGetStats($psr4, $checks, $params); + $stats[$composerPath] = self::processGetStats($psr4, $checks, $params, $includeFolder); } try { @@ -53,10 +53,10 @@ private static function getParams($params, array $tokens, $absFilePath, $psr4Pat * @param array $params * @return int */ - private static function applyChecksInPath($psr4Namespace, $psr4Path, $checks, $params): int + private static function applyChecksInPath($psr4Namespace, $psr4Path, $checks, $params, $includeFolder): int { $filesCount = 0; - $phpFiles = FilePath::getAllPhpFiles($psr4Path); + $phpFiles = self::filterFiles(FilePath::getAllPhpFiles($psr4Path), $includeFolder); foreach ($phpFiles as $phpFilePath) { $filesCount++; self::applyChecks($phpFilePath, $params, $psr4Path, $psr4Namespace, $checks); @@ -87,17 +87,17 @@ private static function applyChecks($phpFilePath, $params, $psr4Path, $psr4Names } } - private static function processGetStats($psr4, array $checks, $params) + private static function processGetStats($psr4, array $checks, $params, $includeFolder) { foreach ($psr4 as $psr4Namespace => $psr4Paths) { - yield $psr4Namespace => self::processPaths($psr4Namespace, $psr4Paths, $checks, $params); + yield $psr4Namespace => self::processPaths($psr4Namespace, $psr4Paths, $checks, $params, $includeFolder); } } - private static function processPaths($psr4Namespace, $psr4Paths, $checks, $params) + private static function processPaths($psr4Namespace, $psr4Paths, $checks, $params, $includeFolder) { foreach ((array) $psr4Paths as $psr4Path) { - $filesCount = self::applyChecksInPath($psr4Namespace, $psr4Path, $checks, $params); + $filesCount = self::applyChecksInPath($psr4Namespace, $psr4Path, $checks, $params, $includeFolder); self::$checkedFilesCount += $filesCount; yield $psr4Path => $filesCount; diff --git a/src/Iterators/FiltersFiles.php b/src/Iterators/FiltersFiles.php index 51d61c0b..98c76611 100644 --- a/src/Iterators/FiltersFiles.php +++ b/src/Iterators/FiltersFiles.php @@ -14,10 +14,10 @@ trait FiltersFiles * @param string $folder * @return \Generator */ - private static function filterFiles(Finder $files, $fileName, $folder) + private static function filterFiles(Finder $files, $folder, $fileName = null) { - return self::filterItems($files, function ($file) use ($fileName, $folder) { - return FilePath::contains($file->getPathname(), $fileName, $folder); + return self::filterItems($files, function ($file) use ($folder, $fileName) { + return FilePath::contains($file->getPathname(), $folder, $fileName); }); }