Skip to content

Commit

Permalink
better report for check:import
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Dec 18, 2023
1 parent 3f72b74 commit 36000a9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
18 changes: 8 additions & 10 deletions src/Features/CheckImports/CheckImportReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public static function printPsr4(array $psr4Stats)
{
$output = '';
foreach ($psr4Stats as $composerPath => $psr4) {
$output .= self::formatComposerPath($composerPath).PHP_EOL;
$output .= self::formatComposerPath($composerPath);
$output .= PHP_EOL;
$output .= self::formatPsr4Stats($psr4);
}

Expand All @@ -33,14 +34,16 @@ public static function formatComposerPath($composerPath): string
* @param array<string, array<string, int>> $psr4
* @return string
*/
public static function formatPsr4Stats(array $psr4)
public static function formatPsr4Stats($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 .= PHP_EOL.' '.self::blue($countClasses).'file'.($countClasses == 1 ? '' : 's').' found ('.self::green('./'.$path).")".PHP_EOL;
if ($countClasses) {
$result .= self::hyphen().'<fg=red>'.self::paddedNamespace($maxLen, $psr4Namespace).' </>';
$result .= PHP_EOL.' '.self::blue($countClasses).'file'.($countClasses == 1 ? '' : 's').' found ('.self::green('./'.$path).")".PHP_EOL;
}
}
}

Expand Down Expand Up @@ -74,7 +77,7 @@ public static function foldersStats($foldersStats)
$total += count($files);
}

$output .= self::blue($total).$fileType;
$total && ($output .= self::blue($total).$fileType);

foreach ($stats as $dir => $files) {
$count = count($files);
Expand Down Expand Up @@ -158,11 +161,6 @@ public static function paddedNamespace($longest, $namespace)
return $namespace.str_repeat(' ', $padLength);
}

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

public static function header(): string
{
return ' ⬛️ <fg=blue>Overall:</>';
Expand Down
43 changes: 28 additions & 15 deletions src/Features/CheckImports/CheckImportsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CheckImportsCommand extends Command

protected $description = 'Checks the validity of use statements';

protected $customMsg = 'All imports are Correct! \(^_^)/';
protected $customMsg = '';

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

$checkedFilesCount = ChecksOnPsr4Classes::$checkedFilesCount;
$filesCount = ChecksOnPsr4Classes::$checkedFilesCount;
$bladeCount = BladeFiles::$checkedFilesCount;
$refCount = ImportsAnalyzer::$checkedRefCount;
$errorPrinter = ErrorPrinter::singleton($this->output);
$this->finishCommand($errorPrinter);
ErrorCounter::$errors = $errorPrinter->errorsList;

$messages = [];
$messages[] = CheckImportReporter::totalImportsMsg(ImportsAnalyzer::$checkedRefCount);
$messages[] = CheckImportReporter::totalImportsMsg($refCount);
$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));
$filesCount && $messages[] = CheckImportReporter::getFilesStats($filesCount);
$bladeCount && $messages[] = CheckImportReporter::getBladeStats($bladeStats, $bladeCount);
$messages[] = CheckImportReporter::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());

if (! $refCount) {
$messages = ['<options=bold;fg=yellow>No imports were found!</> with filter: <fg=red>"'. ($fileName ?: $folder).'"</>'];
}

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

$errorPrinter->printTime();
Expand All @@ -132,6 +138,11 @@ public function handle()
return $errorPrinter->hasErrors() ? 1 : 0;
}

/**
* @param string[] $paths
* @param \Closure $paramProvider
* @return void
*/
private function checkFilePaths($paths, $paramProvider)
{
$checks = $this->checks;
Expand All @@ -148,11 +159,11 @@ private function checkFilePaths($paths, $paramProvider)
}

/**
* @param $dirsList
* @param array<string, string[]> $dirsList
* @param $paramProvider
* @param $file
* @param $folder
* @return array<string, array<string, array<string, array<int, string>>>>
* @param string $file
* @param string $folder
* @return array<string, array<string, array<string, string[]>>>
*/
private function checkFolders($dirsList, $paramProvider, $file, $folder)
{
Expand Down Expand Up @@ -184,6 +195,9 @@ private function shouldRequestThanks(): bool
return $show;
}

/**
* @return \Closure
*/
private function getParamProvider()
{
return function ($tokens) {
Expand All @@ -193,10 +207,9 @@ private function getParamProvider()
};
}

private function reportAll($psr4Stats, $foldersStats, $bladeStats, $routeCounts, $errors)
{
}

/**
* @return array<string, string[]>
*/
private function getLaravelFolders()
{
return [
Expand Down
6 changes: 6 additions & 0 deletions src/FileReaders/FilePath.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ public static function contains($absFilePath, $excludeFile, $excludeFolder)
return false;
}

/**
* @param $paths
* @param $includeFile
* @param $includeFolder
* @return string[]
*/
public static function removeExtraPaths($paths, $includeFile, $includeFolder)
{
$results = [];
Expand Down
3 changes: 3 additions & 0 deletions src/LaravelPaths/LaravelPaths.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

class LaravelPaths
{
/**
* @return string[]
*/
public static function configDirs()
{
return array_merge([config_path()], config('microscope.additional_config_paths', []));
Expand Down

0 comments on commit 36000a9

Please sign in to comment.