Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Feb 26, 2024
1 parent 7d15837 commit b2f1dc7
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 140 deletions.
7 changes: 2 additions & 5 deletions src/ClassListProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
namespace Imanghafoori\LaravelMicroscope;

use ImanGhafoori\ComposerJson\ComposerJson;
use Imanghafoori\LaravelMicroscope\FileReaders\FilePath;
use Imanghafoori\LaravelMicroscope\FileReaders\PhpFinder;
use Imanghafoori\TokenAnalyzer\Str;

use function str_replace;
use function trim;

class ClassListProvider
{
/**
Expand Down Expand Up @@ -37,7 +34,7 @@ public static function get()

private static function calculate($psr4Path, $baseComposerPath, $namespace): void
{
foreach (FilePath::getAllPhpFiles($psr4Path, $baseComposerPath) as $classFilePath) {
foreach (PhpFinder::getAllPhpFiles($psr4Path, $baseComposerPath) as $classFilePath) {
$fileName = $classFilePath->getFilename();
if (substr_count($fileName, '.') > 1) {
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/CheckBadPractice.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\ErrorTypes\EnvFound;
use Imanghafoori\LaravelMicroscope\FileReaders\FilePath;
use Imanghafoori\LaravelMicroscope\FileReaders\PhpFinder;
use Imanghafoori\LaravelMicroscope\FileReaders\Paths;
use Imanghafoori\LaravelMicroscope\LaravelPaths\LaravelPaths;
use Imanghafoori\LaravelMicroscope\SpyClasses\RoutePaths;
Expand Down Expand Up @@ -83,7 +83,7 @@ private function checkPsr4Classes()
foreach (ComposerJson::readAutoload() as $psr4) {
foreach ($psr4 as $dirPaths) {
foreach ((array) $dirPaths as $dirPath) {
foreach (FilePath::getAllPhpFiles($dirPath) as $filePath) {
foreach (PhpFinder::getAllPhpFiles($dirPath) as $filePath) {
if (! in_array($path = $filePath->getRealPath(), $configs)) {
$this->checkForEnv($path);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/CheckCompact.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\ErrorTypes\CompactCall;
use Imanghafoori\LaravelMicroscope\FileReaders\FilePath;
use Imanghafoori\LaravelMicroscope\FileReaders\PhpFinder;
use Imanghafoori\LaravelMicroscope\SpyClasses\RoutePaths;
use Imanghafoori\TokenAnalyzer\FunctionCall;
use Imanghafoori\TokenAnalyzer\Ifs;
Expand Down Expand Up @@ -63,7 +63,7 @@ private function checkPsr4Classes()
{
foreach (ComposerJson::readAutoload() as $psr4) {
foreach ($psr4 as $_namespace => $dirPath) {
foreach (FilePath::getAllPhpFiles($dirPath) as $filePath) {
foreach (PhpFinder::getAllPhpFiles($dirPath) as $filePath) {
$this->checkPathForCompact($filePath->getRealPath());
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/CheckEarlyReturns.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Console\Command;
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\FileReaders\PhpFinder;
use Imanghafoori\LaravelMicroscope\FileReaders\FilePath;
use Imanghafoori\TokenAnalyzer\Refactor;
use Symfony\Component\Console\Terminal;
Expand All @@ -30,7 +31,7 @@ public function handle()
foreach (ComposerJson::readAutoload() as $autoload) {
foreach ($autoload as $psr4Namespace => $psr4Paths) {
foreach ((array) $psr4Paths as $psr4Path) {
$files = FilePath::getAllPhpFiles($psr4Path);
$files = PhpFinder::getAllPhpFiles($psr4Path);
foreach ($files as $file) {
$path = $file->getRealPath();
$tokens = token_get_all(file_get_contents($path));
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/CheckEndIf.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Console\Command;
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\FileReaders\FilePath;
use Imanghafoori\LaravelMicroscope\FileReaders\PhpFinder;
use Imanghafoori\TokenAnalyzer\Refactor;
use Imanghafoori\TokenAnalyzer\SyntaxNormalizer;

Expand All @@ -25,7 +25,7 @@ public function handle()
$fixedFilesCount = 0;
foreach (ComposerJson::readAutoload() as $psr4) {
foreach ($psr4 as $psr4Path) {
$files = FilePath::getAllPhpFiles($psr4Path);
$files = PhpFinder::getAllPhpFiles($psr4Path);
foreach ($files as $file) {
$path = $file->getRealPath();
$tokens = token_get_all(file_get_contents($path));
Expand Down
4 changes: 2 additions & 2 deletions src/Features/CheckDD/CheckDDCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Console\Command;
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\FileReaders\FilePath;
use Imanghafoori\LaravelMicroscope\FileReaders\PhpFinder;
use Imanghafoori\LaravelMicroscope\FileReaders\Paths;
use Imanghafoori\LaravelMicroscope\LaravelPaths\LaravelPaths;
use Imanghafoori\LaravelMicroscope\SpyClasses\RoutePaths;
Expand Down Expand Up @@ -81,7 +81,7 @@ private function checkPsr4Classes()
foreach (ComposerJson::readAutoload() as $psr4) {
foreach ($psr4 as $_namespace => $dirPaths) {
foreach ((array) $dirPaths as $dirPath) {
foreach (FilePath::getAllPhpFiles($dirPath) as $filePath) {
foreach (PhpFinder::getAllPhpFiles($dirPath) as $filePath) {
self::$checkedCallsNum++;
$this->checkForDD($filePath->getRealPath());
}
Expand Down
14 changes: 13 additions & 1 deletion src/Features/CheckImports/CheckImportsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Imanghafoori\LaravelMicroscope\Iterators\ChecksOnPsr4Classes;
use Imanghafoori\LaravelMicroscope\Iterators\ClassMapIterator;
use Imanghafoori\LaravelMicroscope\Iterators\FileIterators;
use Imanghafoori\LaravelMicroscope\LaravelPaths\LaravelPaths;
use Imanghafoori\LaravelMicroscope\SpyClasses\RoutePaths;
use Imanghafoori\LaravelMicroscope\Traits\LogsErrors;
use Imanghafoori\TokenAnalyzer\ImportsAnalyzer;
Expand Down Expand Up @@ -107,7 +108,7 @@ public function handle()
$autoloadedFilesGen = FileIterators::checkFilePaths($autoloadedFilesGen, $paramProvider, $checks);

$foldersStats = FileIterators::checkFolders(
FileIterators::getLaravelFolders(),
self::getLaravelFolders(),
$paramProvider,
$fileName,
$folder,
Expand Down Expand Up @@ -184,4 +185,15 @@ private function getFilesStats()

return $filesCount ? CheckImportReporter::getFilesStats($filesCount) : '';
}

/**
* @return array<string, \Generator>
*/
private static function getLaravelFolders()
{
return [
'config' => LaravelPaths::configDirs(),
'migrations' => LaravelPaths::migrationDirs(),
];
}
}
15 changes: 14 additions & 1 deletion src/Features/Psr4/ClassRefCorrector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static function fixAllRefs($changes, $paths, $beforeFix, $afterFix)
self::$beforeFix = $beforeFix;
foreach ($paths as $path) {
foreach ($path as $p) {
self::fix($p, $changes);
self::applyFix($p, $changes);
}
}
}
Expand Down Expand Up @@ -78,4 +78,17 @@ private static function str_contains($haystack, $needles)

return false;
}

private static function applyFix($path, $changes)
{
if (! is_string($path)) {
foreach (iterator_to_array($path) as $_p) {
foreach ($_p as $t) {
self::fix($t, $changes);
}
}
} else {
self::fix($path, $changes);
}
}
}
18 changes: 8 additions & 10 deletions src/Features/Psr4/FilePathsForReferenceFix.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Imanghafoori\LaravelMicroscope\Features\Psr4;

use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson;
use Imanghafoori\LaravelMicroscope\FileReaders\FilePath;
use Imanghafoori\LaravelMicroscope\FileReaders\PhpFinder;
use Imanghafoori\LaravelMicroscope\FileReaders\Paths;
use Imanghafoori\LaravelMicroscope\LaravelPaths\LaravelPaths;
use Imanghafoori\LaravelMicroscope\SpyClasses\RoutePaths;
Expand All @@ -25,20 +25,18 @@ public static function getFiles()
$paths['routes'] = RoutePaths::get();
$paths['blades'] = LaravelPaths::allBladeFiles();

$dirs = [
LaravelPaths::migrationDirs(),
LaravelPaths::configDirs(),
//LaravelPaths::factoryDirs(),
//LaravelPaths::seedersDir(),
];
$paths = self::collectFilesInNonPsr4Paths($paths, $dirs);
// $dirs = [
// LaravelPaths::migrationDirs(),
// LaravelPaths::configDirs(),
// ];
// $paths['others'] = self::collectFilesInNonPsr4Paths($dirs);

self::$pathsForReferenceFix = $paths;

return $paths;
}

private static function collectFilesInNonPsr4Paths($paths, $dirs)
private static function collectFilesInNonPsr4Paths($dirs)
{
foreach ($dirs as $dir) {
yield from Paths::getAbsFilePaths($dir);
Expand All @@ -49,7 +47,7 @@ private static function getPsr4()
{
foreach (ComposerJson::readAutoload() as $autoload) {
foreach ($autoload as $psr4Path) {
foreach (FilePath::getAllPhpFiles($psr4Path) as $file) {
foreach (PhpFinder::getAllPhpFiles($psr4Path) as $file) {
yield $file->getRealPath();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Features/ServiceProviderGenerator/CheckCodeGeneration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Console\Command;
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\FileReaders\FilePath;
use Imanghafoori\LaravelMicroscope\FileReaders\PhpFinder;

class CheckCodeGeneration extends Command
{
Expand All @@ -20,7 +20,7 @@ public function handle()

foreach (ComposerJson::readAutoload() as $psr4) {
foreach ($psr4 as $psr4Namespace => $psr4Path) {
$files = FilePath::getAllPhpFiles($psr4Path);
$files = PhpFinder::getAllPhpFiles($psr4Path);
GenerateCode::serviceProvider($files, $psr4Path, $psr4Namespace, $this);
}
}
Expand Down
24 changes: 3 additions & 21 deletions src/FileReaders/FilePath.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@

namespace Imanghafoori\LaravelMicroscope\FileReaders;

use Exception;
use JetBrains\PhpStorm\Pure;
use Symfony\Component\Finder\Finder;

class FilePath
{
public static $basePath = '';

public static $fileName = '*';

/**
* Normalize file path to standard formal
* For a path like: "/usr/laravel/app\Http\..\..\database" returns "/usr/laravel/database".
Expand Down Expand Up @@ -47,31 +41,19 @@ public static function normalize($path)
#[Pure]
public static function getRelativePath($absFilePath)
{
return trim(str_replace(self::$basePath, '', $absFilePath), '/\\');
return trim(str_replace(PhpFinder::$basePath, '', $absFilePath), '/\\');
}

/**
* get all ".php" files in directory by giving a path.
* Get all ".php" files in directory by giving a path.
*
* @param string $path Directory path
* @return \Symfony\Component\Finder\Finder
*/
#[Pure]
public static function getAllPhpFiles($path, $basePath = '')
{
if ($basePath === '') {
$basePath = self::$basePath;
}

$basePath = rtrim($basePath, '/\\');
$path = ltrim($path, '/\\');
$path = $basePath.DIRECTORY_SEPARATOR.$path;

try {
return Finder::create()->files()->name(self::$fileName.'.php')->in($path);
} catch (Exception $e) {
return [];
}
return PhpFinder::getAllPhpFiles($path, $basePath);
}

#[Pure]
Expand Down
37 changes: 37 additions & 0 deletions src/FileReaders/PhpFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Imanghafoori\LaravelMicroscope\FileReaders;

use JetBrains\PhpStorm\Pure;
use Symfony\Component\Finder\Finder as SymfonyFinder;

class PhpFinder
{
public static $basePath = '';

public static $fileName = '*';

/**
* get all ".php" files in directory by giving a path.
*
* @param string $path Directory path
* @return \Symfony\Component\Finder\Finder
*/
#[Pure]
public static function getAllPhpFiles($path, $basePath = '')
{
if ($basePath === '') {
$basePath = self::$basePath;
}

$basePath = rtrim($basePath, '/\\');
$path = ltrim($path, '/\\');
$path = $basePath.DIRECTORY_SEPARATOR.$path;

try {
return SymfonyFinder::create()->files()->name(self::$fileName.'.php')->in($path);
} catch (Exception $e) {
return [];
}
}
}
Loading

0 comments on commit b2f1dc7

Please sign in to comment.