From 160130c5b7226a0b91e7b9ee91e8141e7c6631d4 Mon Sep 17 00:00:00 2001 From: Iman Ghafoori Date: Tue, 19 Dec 2023 18:43:56 +0330 Subject: [PATCH] refactor --- .../CheckImports/CheckImportReporter.php | 178 ------------------ .../CheckImports/CheckImportsCommand.php | 86 ++------- .../CheckImports/Reporters/BladeReport.php | 23 +++ .../Reporters/CheckImportReporter.php | 28 +++ .../Reporters/LaravelFoldersReport.php | 34 ++++ .../CheckImports/Reporters/Psr4Report.php | 73 +++++++ .../CheckImports/Reporters/Reporting.php | 43 +++++ .../CheckImports/Reporters/SummeryReport.php | 32 ++++ src/FileReaders/FilePath.php | 2 +- src/Iterators/FileIterators.php | 60 ++++++ .../CheckImports/CheckImportReporterTest.php | 14 +- 11 files changed, 318 insertions(+), 255 deletions(-) delete mode 100644 src/Features/CheckImports/CheckImportReporter.php create mode 100644 src/Features/CheckImports/Reporters/BladeReport.php create mode 100644 src/Features/CheckImports/Reporters/CheckImportReporter.php create mode 100644 src/Features/CheckImports/Reporters/LaravelFoldersReport.php create mode 100644 src/Features/CheckImports/Reporters/Psr4Report.php create mode 100644 src/Features/CheckImports/Reporters/Reporting.php create mode 100644 src/Features/CheckImports/Reporters/SummeryReport.php create mode 100644 src/Iterators/FileIterators.php diff --git a/src/Features/CheckImports/CheckImportReporter.php b/src/Features/CheckImports/CheckImportReporter.php deleted file mode 100644 index 7061bb29..00000000 --- a/src/Features/CheckImports/CheckImportReporter.php +++ /dev/null @@ -1,178 +0,0 @@ ->> $psr4Stats - * @return string - */ - public static function printPsr4(array $psr4Stats) - { - $output = ''; - foreach ($psr4Stats as $composerPath => $psr4) { - $output .= self::formatComposerPath($composerPath); - $output .= PHP_EOL; - $output .= self::formatPsr4Stats($psr4); - } - - return $output; - } - - public static function formatComposerPath($composerPath): string - { - $composerPath = trim($composerPath, '/'); - $composerPath = $composerPath ? trim($composerPath, '/').'/' : ''; - - return ' ./'.$composerPath.'composer.json'.''; - } - - /** - * @param array> $psr4 - * @return string - */ - public static function formatPsr4Stats($psr4) - { - $maxLen = self::getMaxLength($psr4); - $result = ''; - foreach ($psr4 as $psr4Namespace => $psr4Paths) { - foreach ($psr4Paths as $path => $countClasses) { - if ($countClasses) { - $result .= self::hyphen().''.self::paddedNamespace($maxLen, $psr4Namespace).' '; - $result .= PHP_EOL.' '.self::blue($countClasses).'file'.($countClasses == 1 ? '' : 's').' found ('.self::green('./'.$path).")".PHP_EOL; - } - } - } - - return $result; - } - - /** - * @param array> $psr4 - * @return int - */ - public static function getMaxLength(array $psr4) - { - $lengths = [1]; - foreach ($psr4 as $psr4Namespace => $_) { - $lengths[] = strlen($psr4Namespace); - } - - return max($lengths); - } - - /** - * @param array>>> $foldersStats - * @return string - */ - public static function foldersStats($foldersStats) - { - $output = ''; - foreach ($foldersStats as $fileType => $stats) { - $total = 0; - foreach ($stats as $dir => $files) { - $total += count($files); - } - - $total && ($output .= self::blue($total).$fileType); - - foreach ($stats as $dir => $files) { - $count = count($files); - $count && ($output .= self::addLine($dir, $count)); - } - - $output .= PHP_EOL; - } - - return trim($output, PHP_EOL); - } - - public static function totalImportsMsg($refCount) - { - return ''.$refCount.' imports were checked under:'; - } - - /** - * @param array $stats - * @param int $filesCount - * @return string - */ - public static function getBladeStats($stats, $filesCount): string - { - $output = self::blue($filesCount).'blade'.($filesCount <= 1 ? '' : 's'); - foreach ($stats as $path => $count) { - $count && ($output .= self::addLine($path, $count)); - } - - return $output; - } - - public static function getRouteStats($count) - { - return self::blue($count).' route'.($count <= 1 ? '' : 's'); - } - - public static function getFilesStats($count) - { - return self::blue($count).'class'.($count <= 1 ? '' : 'es'); - } - - public static function normalize($dirPath) - { - return FilePath::normalize(str_replace(base_path(), '.', $dirPath)); - } - - public static function green(string $string) - { - return ''.$string.''; - } - - public static function hyphen() - { - return ' ➖ '; - } - - public static function files($count) - { - return ' ( '.$count.' files )'; - } - - public static function addLine($path, $count) - { - $output = PHP_EOL.' '.self::hyphen(); - $output .= self::green(self::normalize($path)); - $output .= self::files($count); - - return $output; - } - - public static function blue($filesCount) - { - return self::hyphen().''.$filesCount.' '; - } - - public static function paddedNamespace($longest, $namespace) - { - $padLength = $longest - strlen($namespace); - - return $namespace.str_repeat(' ', $padLength); - } - - public static function header(): string - { - return ' ⬛️ Overall:'; - } - - public static function formatErrorSummary($totalCount, $checkedRefCount) - { - return ''.$checkedRefCount.' references were checked, '.$totalCount.' error'.($totalCount == 1 ? '' : 's').' found.'; - } - - public static function format($errorType, $count) - { - return ' 🔸 '.$count.' '.$errorType.($count == 1 ? '' : 's').' found.'; - } -} diff --git a/src/Features/CheckImports/CheckImportsCommand.php b/src/Features/CheckImports/CheckImportsCommand.php index 5d0044e0..57aecfc8 100644 --- a/src/Features/CheckImports/CheckImportsCommand.php +++ b/src/Features/CheckImports/CheckImportsCommand.php @@ -11,14 +11,14 @@ use Imanghafoori\LaravelMicroscope\Features\CheckImports\Checks\CheckClassReferencesAreValid; use Imanghafoori\LaravelMicroscope\Features\CheckImports\Handlers\ClassAtMethodHandler; use Imanghafoori\LaravelMicroscope\Features\CheckImports\Handlers\PrintWrongClassRefs; +use Imanghafoori\LaravelMicroscope\Features\CheckImports\Reporters\CheckImportReporter; use Imanghafoori\LaravelMicroscope\Features\FacadeAlias\FacadeAliasesCheck; use Imanghafoori\LaravelMicroscope\Features\FacadeAlias\FacadeAliasReplacer; use Imanghafoori\LaravelMicroscope\Features\FacadeAlias\FacadeAliasReporter; use Imanghafoori\LaravelMicroscope\FileReaders\FilePath; -use Imanghafoori\LaravelMicroscope\FileReaders\Paths; use Imanghafoori\LaravelMicroscope\ForPsr4LoadedClasses; use Imanghafoori\LaravelMicroscope\Iterators\ChecksOnPsr4Classes; -use Imanghafoori\LaravelMicroscope\LaravelPaths\LaravelPaths; +use Imanghafoori\LaravelMicroscope\Iterators\FileIterators; use Imanghafoori\LaravelMicroscope\SpyClasses\RoutePaths; use Imanghafoori\LaravelMicroscope\Traits\LogsErrors; use Imanghafoori\TokenAnalyzer\ImportsAnalyzer; @@ -89,13 +89,18 @@ public function handle() $paths = array_merge($classMapFiles, $autoloadedFiles, $routeFiles); $paramProvider = $this->getParamProvider(); - $this->checkFilePaths($paths, $paramProvider); - $foldersStats = $this->checkFolders( - $this->getLaravelFolders(), + $checks = $this->checks; + unset($checks[1]); + + FileIterators::checkFilePaths($paths, $paramProvider, $checks); + + $foldersStats = FileIterators::checkFolders( + FileIterators::getLaravelFolders(), $paramProvider, $fileName, - $folder + $folder, + $checks ); $psr4Stats = ForPsr4LoadedClasses::check($this->checks, $paramProvider, $fileName, $folder); @@ -106,20 +111,16 @@ public function handle() $refCount = ImportsAnalyzer::$checkedRefCount; $errorPrinter = ErrorPrinter::singleton($this->output); $this->finishCommand($errorPrinter); - ErrorCounter::$errors = $errorPrinter->errorsList; $messages = []; - $messages[] = CheckImportReporter::totalImportsMsg($refCount); - $messages[] = CheckImportReporter::printPsr4($psr4Stats); + $messages[] = Reporters\CheckImportReporter::totalImportsMsg($refCount); + $messages[] = Reporters\Psr4Report::printPsr4($psr4Stats); $messages[] = CheckImportReporter::header(); - $filesCount && $messages[] = CheckImportReporter::getFilesStats($filesCount); - $bladeCount && $messages[] = CheckImportReporter::getBladeStats($bladeStats, $bladeCount); - $messages[] = CheckImportReporter::foldersStats($foldersStats); + $filesCount && $messages[] = Reporters\CheckImportReporter::getFilesStats($filesCount); + $bladeCount && $messages[] = Reporters\BladeReport::getBladeStats($bladeStats, $bladeCount); + $messages[] = Reporters\LaravelFoldersReport::foldersStats($foldersStats); count($routeFiles) && $messages[] = CheckImportReporter::getRouteStats(count($routeFiles)); - $messages[] = CheckImportReporter::formatErrorSummary(ErrorCounter::getTotalErrors(), ImportsAnalyzer::$checkedRefCount); - $messages[] = CheckImportReporter::format('unused import', ErrorCounter::getExtraImportsCount()); - $messages[] = CheckImportReporter::format('wrong import', ErrorCounter::getExtraWrongCount()); - $messages[] = CheckImportReporter::format('wrong class reference', ErrorCounter::getWrongUsedClassCount()); + $messages[] = Reporters\SummeryReport::summery($errorPrinter->errorsList); if (! $refCount) { $messages = ['No imports were found! with filter: "'. ($fileName ?: $folder).'"']; @@ -138,48 +139,6 @@ public function handle() return $errorPrinter->hasErrors() ? 1 : 0; } - /** - * @param string[] $paths - * @param \Closure $paramProvider - * @return void - */ - private function checkFilePaths($paths, $paramProvider) - { - $checks = $this->checks; - unset($checks[1]); - - foreach ($paths as $dir => $absFilePaths) { - foreach ((array) $absFilePaths as $absFilePath) { - $tokens = token_get_all(file_get_contents($absFilePath)); - foreach ($checks as $check) { - $check::check($tokens, $absFilePath, $paramProvider($tokens)); - } - } - } - } - - /** - * @param array $dirsList - * @param $paramProvider - * @param string $file - * @param string $folder - * @return array>> - */ - private function checkFolders($dirsList, $paramProvider, $file, $folder) - { - $files = []; - foreach ($dirsList as $listName => $dirs) { - $filePaths = Paths::getAbsFilePaths($dirs, $file, $folder); - $this->checkFilePaths($filePaths, $paramProvider); - - foreach ($filePaths as $dir => $filePathList) { - $files[$listName][$dir] = $filePathList; - } - } - - return $files; - } - private function shouldRequestThanks(): bool { $key = 'microscope_thanks_throttle'; @@ -206,15 +165,4 @@ private function getParamProvider() return $imports[0] ?: [$imports[1]]; }; } - - /** - * @return array - */ - private function getLaravelFolders() - { - return [ - 'config' => LaravelPaths::configDirs(), - 'migrations' => LaravelPaths::migrationDirs(), - ]; - } } diff --git a/src/Features/CheckImports/Reporters/BladeReport.php b/src/Features/CheckImports/Reporters/BladeReport.php new file mode 100644 index 00000000..8e43c2e7 --- /dev/null +++ b/src/Features/CheckImports/Reporters/BladeReport.php @@ -0,0 +1,23 @@ + $stats + * @param int $filesCount + * @return string + */ + public static function getBladeStats($stats, $filesCount): string + { + $output = self::blue($filesCount).'blade'.($filesCount === 0 ? '' : 's'); + foreach ($stats as $path => $count) { + $count && ($output .= self::addLine($path, $count)); + } + + return $output; + } +} \ No newline at end of file diff --git a/src/Features/CheckImports/Reporters/CheckImportReporter.php b/src/Features/CheckImports/Reporters/CheckImportReporter.php new file mode 100644 index 00000000..95abb6c6 --- /dev/null +++ b/src/Features/CheckImports/Reporters/CheckImportReporter.php @@ -0,0 +1,28 @@ +'.$refCount.' imports were checked under:'; + } + + public static function getRouteStats($count) + { + return self::blue($count).' route'.($count <= 1 ? '' : 's'); + } + + public static function getFilesStats($count) + { + return self::blue($count).'class'.($count <= 1 ? '' : 'es'); + } + + public static function header(): string + { + return ' ⬛️ Overall:'; + } +} diff --git a/src/Features/CheckImports/Reporters/LaravelFoldersReport.php b/src/Features/CheckImports/Reporters/LaravelFoldersReport.php new file mode 100644 index 00000000..3602a405 --- /dev/null +++ b/src/Features/CheckImports/Reporters/LaravelFoldersReport.php @@ -0,0 +1,34 @@ +>>> $foldersStats + * @return string + */ + public static function foldersStats($foldersStats) + { + $output = ''; + foreach ($foldersStats as $fileType => $stats) { + $total = 0; + foreach ($stats as $dir => $files) { + $total += count($files); + } + + $total && ($output .= self::blue($total).$fileType); + + foreach ($stats as $dir => $files) { + $count = count($files); + $count && ($output .= self::addLine($dir, $count)); + } + + $output .= PHP_EOL; + } + + return trim($output, PHP_EOL); + } +} diff --git a/src/Features/CheckImports/Reporters/Psr4Report.php b/src/Features/CheckImports/Reporters/Psr4Report.php new file mode 100644 index 00000000..000ceb1f --- /dev/null +++ b/src/Features/CheckImports/Reporters/Psr4Report.php @@ -0,0 +1,73 @@ +>> $psr4Stats + * @return string + */ + public static function printPsr4(array $psr4Stats) + { + $output = ''; + foreach ($psr4Stats as $composerPath => $psr4) { + $output .= self::formatComposerPath($composerPath); + $output .= PHP_EOL; + $output .= self::formatPsr4Stats($psr4); + } + + return $output; + } + + public static function formatComposerPath($composerPath): string + { + $composerPath = trim($composerPath, '/'); + $composerPath = $composerPath ? trim($composerPath, '/').'/' : ''; + + return ' ./'.$composerPath.'composer.json'.''; + } + + /** + * @param array> $psr4 + * @return string + */ + public static function formatPsr4Stats($psr4) + { + $maxLen = self::getMaxLength($psr4); + $result = ''; + foreach ($psr4 as $psr4Namespace => $psr4Paths) { + foreach ($psr4Paths as $path => $countClasses) { + if ($countClasses) { + $result .= self::hyphen().''.self::paddedNamespace($maxLen, $psr4Namespace).' '; + $result .= PHP_EOL.' '.self::blue($countClasses).'file'.($countClasses == 1 ? '' : 's').' found ('.self::green('./'.$path).")".PHP_EOL; + } + } + } + + return $result; + } + + public static function paddedNamespace($longest, $namespace) + { + $padLength = $longest - strlen($namespace); + + return $namespace.str_repeat(' ', $padLength); + } + + /** + * @param array> $psr4 + * @return int + */ + public static function getMaxLength(array $psr4) + { + $lengths = [1]; + foreach ($psr4 as $psr4Namespace => $_) { + $lengths[] = strlen($psr4Namespace); + } + + return max($lengths); + } +} diff --git a/src/Features/CheckImports/Reporters/Reporting.php b/src/Features/CheckImports/Reporters/Reporting.php new file mode 100644 index 00000000..7dff144e --- /dev/null +++ b/src/Features/CheckImports/Reporters/Reporting.php @@ -0,0 +1,43 @@ +'.$string.''; + } + + public static function hyphen() + { + return ' ➖ '; + } + + public static function files($count) + { + return ' ( '.$count.' files )'; + } + + public static function addLine($path, $count) + { + $output = PHP_EOL.' '.self::hyphen(); + $output .= self::green(self::normalize($path)); + $output .= self::files($count); + + return $output; + } + + public static function blue($filesCount) + { + return self::hyphen().''.$filesCount.' '; + } + + public static function normalize($dirPath) + { + return FilePath::normalize(str_replace(base_path(), '.', $dirPath)); + } + +} diff --git a/src/Features/CheckImports/Reporters/SummeryReport.php b/src/Features/CheckImports/Reporters/SummeryReport.php new file mode 100644 index 00000000..5cb014dc --- /dev/null +++ b/src/Features/CheckImports/Reporters/SummeryReport.php @@ -0,0 +1,32 @@ +'.$checkedRefCount.' references were checked, '.$totalCount.' error'.($totalCount == 1 ? '' : 's').' found.'; + } + + public static function format($errorType, $count) + { + return ' 🔸 '.$count.' '.$errorType.($count == 1 ? '' : 's').' found.'; + } +} \ No newline at end of file diff --git a/src/FileReaders/FilePath.php b/src/FileReaders/FilePath.php index bc872788..a6625540 100644 --- a/src/FileReaders/FilePath.php +++ b/src/FileReaders/FilePath.php @@ -49,7 +49,7 @@ public static function getRelativePath($absFilePath) * get all ".php" files in directory by giving a path. * * @param string $path Directory path - * @return \Symfony\Component\Finder\Finder + * @return array|\Iterator */ public static function getAllPhpFiles($path, $basePath = '') { diff --git a/src/Iterators/FileIterators.php b/src/Iterators/FileIterators.php new file mode 100644 index 00000000..5b572649 --- /dev/null +++ b/src/Iterators/FileIterators.php @@ -0,0 +1,60 @@ + $absFilePaths) { + foreach ((array) $absFilePaths as $absFilePath) { + $tokens = token_get_all(file_get_contents($absFilePath)); + foreach ($checks as $check) { + $check::check($tokens, $absFilePath, $paramProvider($tokens)); + } + } + } + } + + /** + * @return array + */ + public static function getLaravelFolders() + { + return [ + 'config' => LaravelPaths::configDirs(), + 'migrations' => LaravelPaths::migrationDirs(), + ]; + } + + /** + * @param array $dirsList + * @param $paramProvider + * @param string $file + * @param string $folder + * @param array $checks + * @return array>> + */ + public static function checkFolders($dirsList, $paramProvider, $file, $folder, $checks) + { + $files = []; + foreach ($dirsList as $listName => $dirs) { + $filePaths = Paths::getAbsFilePaths($dirs, $file, $folder); + self::checkFilePaths($filePaths, $paramProvider, $checks); + + foreach ($filePaths as $dir => $filePathList) { + $files[$listName][$dir] = $filePathList; + } + } + + return $files; + } +} diff --git a/tests/CheckImports/CheckImportReporterTest.php b/tests/CheckImports/CheckImportReporterTest.php index c40e6ff5..0e489fad 100644 --- a/tests/CheckImports/CheckImportReporterTest.php +++ b/tests/CheckImports/CheckImportReporterTest.php @@ -2,41 +2,41 @@ namespace Imanghafoori\LaravelMicroscope\Tests\CheckImports; -use Imanghafoori\LaravelMicroscope\Features\CheckImports\CheckImportReporter; +use Imanghafoori\LaravelMicroscope\Features\CheckImports\Reporters\Psr4Report; use PHPUnit\Framework\TestCase; class CheckImportReporterTest extends TestCase { public function testFormatComposerPathWithEmptyPath() { - $result = CheckImportReporter::formatComposerPath(''); + $result = Psr4Report::formatComposerPath(''); $this->assertEquals(' ./composer.json', $result); - $result = CheckImportReporter::formatComposerPath('/'); + $result = Psr4Report::formatComposerPath('/'); $this->assertEquals(' ./composer.json', $result); } public function testFormatComposerPathWithLeadingSlash() { - $result = CheckImportReporter::formatComposerPath('/path/to/composer'); + $result = Psr4Report::formatComposerPath('/path/to/composer'); $this->assertEquals(' ./path/to/composer/composer.json', $result); } public function testFormatComposerPathWithTrailingSlash() { - $result = CheckImportReporter::formatComposerPath('path/to/composer/'); + $result = Psr4Report::formatComposerPath('path/to/composer/'); $this->assertEquals(' ./path/to/composer/composer.json', $result); } public function testFormatComposerPathWithBothLeadingAndTrailingSlashes() { - $result = CheckImportReporter::formatComposerPath('/path/to/composer/'); + $result = Psr4Report::formatComposerPath('/path/to/composer/'); $this->assertEquals(' ./path/to/composer/composer.json', $result); } public function testFormatComposerPathWithoutSlashes() { - $result = CheckImportReporter::formatComposerPath('path/to/composer'); + $result = Psr4Report::formatComposerPath('path/to/composer'); $this->assertEquals(' ./path/to/composer/composer.json', $result); } }