Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Dec 18, 2023
1 parent 553c892 commit 3f72b74
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 128 deletions.
37 changes: 32 additions & 5 deletions src/BladeFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,29 @@ class BladeFiles
{
public static $checkedFilesCount = 0;

/**
* @param $checkers
* @param $params
* @param $fileName
* @param $folder
* @return array<string, int>
*/
public static function check($checkers, $params = [], $fileName = '', $folder = '')
{
$stats = [];
$compiler = app('microscope.blade.compiler');
method_exists($compiler, 'withoutComponentTags') && $compiler->withoutComponentTags();

$hints = self::getNamespacedPaths();
$hints['random_key_69471'] = View::getFinder()->getPaths();

foreach ($hints as $paths) {
$stats = [];
foreach (self::getViews() as $paths) {
$stats = array_merge($stats, self::checkPaths($paths, $checkers, $fileName, $folder, $params));
}

return $stats;
}

/**
* @return array<string, string>
*/
private static function getNamespacedPaths()
{
$hints = View::getFinder()->getHints();
Expand All @@ -35,6 +42,15 @@ private static function getNamespacedPaths()
return $hints;
}

/**
* @param string[] $paths
* @param $checkers
* @param $fileName
* @param $folder
* @param $params
*
* @return array<string, int>
*/
public static function checkPaths($paths, $checkers, $fileName, $folder, $params)
{
$stats = [];
Expand Down Expand Up @@ -73,4 +89,15 @@ public static function checkPaths($paths, $checkers, $fileName, $folder, $params

return $stats;
}

/**
* @return array<string, string>
*/
private static function getViews()
{
$hints = self::getNamespacedPaths();
$hints['random_key_69471'] = View::getFinder()->getPaths();

return $hints;
}
}
150 changes: 54 additions & 96 deletions src/Features/CheckImports/CheckImportReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,130 +2,70 @@

namespace Imanghafoori\LaravelMicroscope\Features\CheckImports;

use Imanghafoori\LaravelMicroscope\BladeFiles;
use Imanghafoori\LaravelMicroscope\FileReaders\FilePath;
use Imanghafoori\LaravelMicroscope\Iterators\ChecksOnPsr4Classes;
use Imanghafoori\TokenAnalyzer\ImportsAnalyzer;

class CheckImportReporter
{
public static function report($psr4Stats, $foldersStats, $bladeStats, int $routeFilesCount)
{
return [
self::totalImportsMsg(ImportsAnalyzer::$checkedRefCount),
self::printPsr4($psr4Stats),
self::printFileCounts(
$foldersStats,
$bladeStats,
$routeFilesCount,
ChecksOnPsr4Classes::$checkedFilesCount,
BladeFiles::$checkedFilesCount
),
];
}

private static function printFileCounts($foldersStats, $bladeStats, $routesCount, $Psr4Count, $bladesCount): string
{
$output = ' <fg=blue>Overall:'."</>\n";
$output .= self::compileCheckedFilesStats($Psr4Count);
$output .= self::compileBladeStats($bladeStats, $bladesCount);
$output .= self::compileFolderStats($foldersStats);
$output .= self::getRouteStats($routesCount);

return $output;
}

private static function compileCheckedFilesStats($checkedFilesCount): string
{
return $checkedFilesCount ? self::getFilesStats($checkedFilesCount) : '';
}

private static function compileBladeStats($bladeStats, $checkedBladesCount): string
{
return $bladeStats ? self::getBladeStats($bladeStats, $checkedBladesCount) : '';
}

private static function compileFolderStats($foldersStats): string
{
return $foldersStats ? self::foldersStats($foldersStats) : '';
}

public static function printErrorsCount($checkedRefCount)
{
$output = self::formatErrorSummary(ErrorCounter::getTotalErrors(), $checkedRefCount);
$output .= self::format('unused import', ErrorCounter::getExtraImportsCount());
$output .= self::format('wrong import', ErrorCounter::getExtraWrongCount());
$output .= self::format('wrong class reference', ErrorCounter::getWrongUsedClassCount());

return $output;
}

private static function formatErrorSummary($totalCount, $checkedRefCount): string
{
return '<options=bold;fg=yellow>'.$checkedRefCount.' references were checked, '.$totalCount.' error'.($totalCount == 1 ? '' : 's').' found.</>'.PHP_EOL;
}

private static function format($errorType, $count)
{
return ' - <fg=yellow>'.$count.' '.$errorType.($count == 1 ? '' : 's').' found.'.PHP_EOL;
}

/**
* @param array<string, array<string, string[]>> $psr4Stats
* @param array<string, array<string, array<string, int>>> $psr4Stats
* @return string
*/
public static function printPsr4(array $psr4Stats)
{
$output = '';
foreach ($psr4Stats as $composerPath => $psr4) {
$output .= self::formatComposerPath($composerPath);
$output .= self::formatComposerPath($composerPath).PHP_EOL;
$output .= self::formatPsr4Stats($psr4);
}

return $output;
}

private static function formatComposerPath($composerPath): string
public static function formatComposerPath($composerPath): string
{
$composerPath = trim($composerPath, '/');
$composerPath = $composerPath ? trim($composerPath, '/').'/' : '';

return ' <fg=blue>./'.$composerPath.'composer.json'.'</>'.PHP_EOL;
return ' <fg=blue>./'.$composerPath.'composer.json'.'</>';
}

/**
* @param array<string, string[]> $psr4
* @param array<string, array<string, int>> $psr4
* @return string
*/
private static function formatPsr4Stats(array $psr4)
public static function formatPsr4Stats(array $psr4)
{
$maxLen = self::getMaxLength($psr4);
$result = '';
foreach ($psr4 as $psr4Namespace => $psr4Paths) {
foreach ($psr4Paths as $path => $countClasses) {
$result .= self::hyphen().'<fg=red>'.self::paddedNamespace($maxLen, $psr4Namespace).' </>';
$result .= self::blue(' '.self::paddedClassCount($countClasses)).'file'.($countClasses == 1 ? '' : 's').' found ('.self::green('./'.$path).")\n";
$result .= PHP_EOL.' '.self::blue($countClasses).'file'.($countClasses == 1 ? '' : 's').' found ('.self::green('./'.$path).")".PHP_EOL;
}
}

return $result;
}

/**
* @param array<string, string[]> $psr4
* @param array<string, array<string, int>> $psr4
* @return int
*/
private static function getMaxLength(array $psr4)
public static function getMaxLength(array $psr4)
{
$lengths = [1];
foreach ($psr4 as $psr4Namespace => $psr4Paths) {
foreach ($psr4 as $psr4Namespace => $_) {
$lengths[] = strlen($psr4Namespace);
}

return max($lengths);
}

private static function foldersStats($foldersStats)
/**
* @param array<string, array<string, array<string, array<int, string>>>> $foldersStats
* @return string
*/
public static function foldersStats($foldersStats)
{
$output = '';
foreach ($foldersStats as $fileType => $stats) {
Expand All @@ -144,79 +84,97 @@ private static function foldersStats($foldersStats)
$output .= PHP_EOL;
}

return $output;
return trim($output, PHP_EOL);
}

public static function totalImportsMsg($checkedRefCount)
public static function totalImportsMsg($refCount)
{
return '<options=bold;fg=yellow>'.$checkedRefCount.' imports were checked under:</>';
return '<options=bold;fg=yellow>'.$refCount.' imports were checked under:</>';
}

private static function getBladeStats($stats, $filesCount): string
/**
* @param array<string, int> $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));
}

$output .= PHP_EOL;

return $output;
}

private static function getRouteStats($count)
public static function getRouteStats($count)
{
return self::blue($count).' route'.($count <= 1 ? '' : 's').PHP_EOL;
return self::blue($count).' route'.($count <= 1 ? '' : 's');
}

private static function getFilesStats($count)
public static function getFilesStats($count)
{
return self::blue($count).' class'.($count <= 1 ? '' : 'es').PHP_EOL;
return self::blue($count).'class'.($count <= 1 ? '' : 'es');
}

private static function normalize($dirPath)
public static function normalize($dirPath)
{
return FilePath::normalize(str_replace(base_path(), '.', $dirPath));
}

private static function green(string $string)
public static function green(string $string)
{
return '<fg=green>'.$string.'</>';
}

private static function hyphen()
public static function hyphen()
{
return ' - ';
return ' ';
}

private static function files($count)
public static function files($count)
{
return ' ( '.$count.' files )';
}

private static function addLine($path, $count)
public static function addLine($path, $count)
{
$output = self::hyphen();
$output = PHP_EOL.' '.self::hyphen();
$output .= self::green(self::normalize($path));
$output .= self::files($count);

return $output;
}

private static function blue($checkedFilesNum)
public static function blue($filesCount)
{
return self::hyphen().'<fg=blue>'.$checkedFilesNum.'</> ';
return self::hyphen().'<fg=blue>'.$filesCount.'</> ';
}

private static function paddedNamespace($longest, $namespace)
public static function paddedNamespace($longest, $namespace)
{
$padLength = $longest - strlen($namespace);

return $namespace.str_repeat(' ', $padLength);
}

private static function paddedClassCount($countClasses)
public static function paddedClassCount($countClasses)
{
return str_pad((string) $countClasses, 3, ' ', STR_PAD_LEFT);
}

public static function header(): string
{
return ' ⬛️ <fg=blue>Overall:</>';
}

public static function formatErrorSummary($totalCount, $checkedRefCount)
{
return '<options=bold;fg=yellow>'.$checkedRefCount.' references were checked, '.$totalCount.' error'.($totalCount == 1 ? '' : 's').' found.</>';
}

public static function format($errorType, $count)
{
return ' 🔸 <fg=yellow>'.$count.'</> '.$errorType.($count == 1 ? '' : 's').' found.';
}
}
35 changes: 24 additions & 11 deletions src/Features/CheckImports/CheckImportsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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\SpyClasses\RoutePaths;
use Imanghafoori\LaravelMicroscope\Traits\LogsErrors;
Expand All @@ -41,7 +42,7 @@ class CheckImportsCommand extends Command
protected $customMsg = 'All imports are Correct! \(^_^)/';

/**
* @var string[]
* @var array<int, class-string<\Imanghafoori\LaravelMicroscope\Iterators\Check>>
*/
private $checks = [
1 => CheckClassAtMethod::class,
Expand Down Expand Up @@ -100,20 +101,25 @@ public function handle()
$psr4Stats = ForPsr4LoadedClasses::check($this->checks, $paramProvider, $fileName, $folder);
$bladeStats = BladeFiles::check($this->checks, $paramProvider, $fileName, $folder);

$checkedFilesCount = ChecksOnPsr4Classes::$checkedFilesCount;
$errorPrinter = ErrorPrinter::singleton($this->output);
$this->finishCommand($errorPrinter);

$messages = CheckImportReporter::report(
$psr4Stats,
$foldersStats,
$bladeStats,
count($routeFiles)
);

ErrorCounter::$errors = $errorPrinter->errorsList;
$messages[] = CheckImportReporter::printErrorsCount(ImportsAnalyzer::$checkedRefCount);

$this->getOutput()->writeln(implode(PHP_EOL, $messages));
$messages = [];
$messages[] = CheckImportReporter::totalImportsMsg(ImportsAnalyzer::$checkedRefCount);
$messages[] = CheckImportReporter::printPsr4($psr4Stats);
$messages[] = CheckImportReporter::header();
$checkedFilesCount && $messages[] = CheckImportReporter::getFilesStats($checkedFilesCount);
$bladeStats && $messages[] = CheckImportReporter::getBladeStats($bladeStats, BladeFiles::$checkedFilesCount);
$foldersStats && $messages[] = CheckImportReporter::foldersStats($foldersStats);
$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());

$this->getOutput()->writeln(implode(PHP_EOL, array_filter($messages)));

$errorPrinter->printTime();

Expand Down Expand Up @@ -141,6 +147,13 @@ private function checkFilePaths($paths, $paramProvider)
}
}

/**
* @param $dirsList
* @param $paramProvider
* @param $file
* @param $folder
* @return array<string, array<string, array<string, array<int, string>>>>
*/
private function checkFolders($dirsList, $paramProvider, $file, $folder)
{
$files = [];
Expand Down
Loading

0 comments on commit 3f72b74

Please sign in to comment.