Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Dec 27, 2023
1 parent 5d74d9d commit 69692c7
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 66 deletions.
29 changes: 14 additions & 15 deletions src/Features/CheckImports/CheckImportsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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 = ['<options=bold;fg=yellow>No imports were found!</> with filter: <fg=red>"'.($fileName ?: $folder).'"</>'];
}

Expand Down
4 changes: 2 additions & 2 deletions src/Features/CheckImports/Reporters/CheckImportReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class CheckImportReporter
{
use Reporting;

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

public static function getRouteStats($count)
Expand Down
2 changes: 1 addition & 1 deletion src/Features/CheckView/CheckViewsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
23 changes: 10 additions & 13 deletions src/FileReaders/FilePath.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
Expand All @@ -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;
}

Expand All @@ -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;
}
}
Expand Down
18 changes: 7 additions & 11 deletions src/FileReaders/Paths.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
}
}
26 changes: 12 additions & 14 deletions src/Iterators/BladeFiles/CheckBladePaths.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/Iterators/ChecksOnPsr4Classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Iterators/FiltersFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down

0 comments on commit 69692c7

Please sign in to comment.