Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Dec 19, 2023
1 parent 350e973 commit 160130c
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 255 deletions.
178 changes: 0 additions & 178 deletions src/Features/CheckImports/CheckImportReporter.php

This file was deleted.

86 changes: 17 additions & 69 deletions src/Features/CheckImports/CheckImportsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 = ['<options=bold;fg=yellow>No imports were found!</> with filter: <fg=red>"'. ($fileName ?: $folder).'"</>'];
Expand All @@ -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<string, string[]> $dirsList
* @param $paramProvider
* @param string $file
* @param string $folder
* @return array<string, array<string, array<string, string[]>>>
*/
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';
Expand All @@ -206,15 +165,4 @@ private function getParamProvider()
return $imports[0] ?: [$imports[1]];
};
}

/**
* @return array<string, string[]>
*/
private function getLaravelFolders()
{
return [
'config' => LaravelPaths::configDirs(),
'migrations' => LaravelPaths::migrationDirs(),
];
}
}
23 changes: 23 additions & 0 deletions src/Features/CheckImports/Reporters/BladeReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Imanghafoori\LaravelMicroscope\Features\CheckImports\Reporters;

class BladeReport
{
use Reporting;

/**
* @param array<string, int> $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;
}
}
28 changes: 28 additions & 0 deletions src/Features/CheckImports/Reporters/CheckImportReporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Imanghafoori\LaravelMicroscope\Features\CheckImports\Reporters;

class CheckImportReporter
{
use Reporting;

public static function totalImportsMsg($refCount)
{
return '<options=bold;fg=yellow>'.$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 ' ⬛️ <fg=blue>Overall:</>';
}
}
34 changes: 34 additions & 0 deletions src/Features/CheckImports/Reporters/LaravelFoldersReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Imanghafoori\LaravelMicroscope\Features\CheckImports\Reporters;

class LaravelFoldersReport
{
use Reporting;

/**
* @param array<string, array<string, array<string, array<int, string>>>> $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);
}
}
Loading

0 comments on commit 160130c

Please sign in to comment.