Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Dec 4, 2023
1 parent 5ae40cf commit 35389b5
Show file tree
Hide file tree
Showing 20 changed files with 165 additions and 132 deletions.
48 changes: 37 additions & 11 deletions src/ErrorReporters/ErrorPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,46 @@ class ErrorPrinter
{
public static $ignored;

public $errorsList = [
'total' => 0,
];
/**
* @var array
*/
public $errorsList = [];

/**
* @var int
*/
public $total = 0;

/**
* The output interface implementation.
*
* @var \Illuminate\Console\OutputStyle
*/
public $printer;

/**
* @var bool
*/
public $logErrors = true;

/**
* @var string[]
*/
public $pended = [];

/**
* @var int
*/
public $count = 0;

/**
* @var self
*/
public static $instance;

/**
* @var string
*/
public static $basePath;

/**
Expand Down Expand Up @@ -70,7 +96,7 @@ public function print($msg, $path = ' ')

public function printHeader($msg)
{
$number = ++$this->errorsList['total'];
$number = ++$this->total;
($number < 10) && $number = " $number";

$number = '<fg=cyan>'.$number.' </>';
Expand Down Expand Up @@ -121,16 +147,16 @@ public function hasErrors()
public function logErrors()
{
$errList = $this->errorsList;
unset($errList['total']);

foreach ($errList as $list) {
foreach ($list as $error) {
if ($error instanceof PendingError) {
$this->printHeader($error->getHeader());
$this->print($error->getErrorData());
$this->printLink($error->getLinkPath(), $error->getLinkLineNumber());
$this->end();
}
$this->printHeader($error->getHeader());
$this->print($error->getErrorData());
$this->printLink(
$error->getLinkPath(),
$error->getLinkLineNumber()
);
$this->end();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

namespace Imanghafoori\LaravelMicroscope\Commands;
namespace Imanghafoori\LaravelMicroscope\Features\CheckEvents;

use Illuminate\Console\Command;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\SpyClasses\SpyDispatcher;
use Imanghafoori\LaravelMicroscope\Traits\LogsErrors;

class CheckEvents extends Command
Expand Down
21 changes: 21 additions & 0 deletions src/Features/CheckEvents/Installer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Imanghafoori\LaravelMicroscope\Features\CheckEvents;

use Illuminate\Contracts\Queue\Factory as QueueFactoryContract;
use Illuminate\Support\Facades\Event;

class Installer
{
public static function spyEvents()
{
app()->booting(function () {
app()->singleton('events', function ($app) {
return (new SpyDispatcher($app))->setQueueResolver(function () use ($app) {
return $app->make(QueueFactoryContract::class);
});
});
Event::clearResolvedInstance('events');
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Imanghafoori\LaravelMicroscope\SpyClasses;
namespace Imanghafoori\LaravelMicroscope\Features\CheckEvents;

use Exception;
use Illuminate\Events\Dispatcher;
Expand Down
32 changes: 15 additions & 17 deletions src/Features/CheckImports/CheckImportReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ private static function printFileCounts($foldersStats, $bladeStats, int $countRo
public static function printErrorsCount()
{
$totalErrors = ImportsAnalyzer::$extraCorrectImportsCount + ImportsAnalyzer::$wrongImportsCount + ImportsAnalyzer::$wrongClassRefCount;
$output = '<options=bold;fg=yellow>'.ImportsAnalyzer::$checkedRefCount.' refs were checked, '.$totalErrors.' error'.($totalErrors == 1 ? ' ' : 's').' found.</>'.PHP_EOL;
$output .= ' - <fg=yellow>'.ImportsAnalyzer::$extraCorrectImportsCount.' unused</> import'.(ImportsAnalyzer::$extraCorrectImportsCount == 1 ? ' ' : 's').' found.'.PHP_EOL;
$output .= ' - <fg=red>'.ImportsAnalyzer::$wrongImportsCount.' wrong</> import'.(ImportsAnalyzer::$wrongImportsCount <= 1 ? ' ' : 's').' found.'.PHP_EOL;
$output .= ' - <fg=red>'.ImportsAnalyzer::$wrongClassRefCount.' wrong</> class'.(ImportsAnalyzer::$wrongClassRefCount <= 1 ? '' : 'es').' ref found.';
$output = '<options=bold;fg=yellow>'.ImportsAnalyzer::$checkedRefCount.' references were checked, '.$totalErrors.' error'.($totalErrors == 1 ? '' : 's').' found.</>'.PHP_EOL;
$output .= ' - <fg=yellow>'.ImportsAnalyzer::$extraCorrectImportsCount.' unused</> import'.(ImportsAnalyzer::$extraCorrectImportsCount == 1 ? '' : 's').' found.'.PHP_EOL;
$output .= ' - <fg=red>'.ImportsAnalyzer::$wrongImportsCount.' wrong</> import'.(ImportsAnalyzer::$wrongImportsCount <= 1 ? '' : 's').' found.'.PHP_EOL;
$output .= ' - <fg=red>'.ImportsAnalyzer::$wrongClassRefCount.' wrong</> class reference'.(ImportsAnalyzer::$wrongClassRefCount <= 1 ? '' : 's').' found.';

return $output;
}
Expand Down Expand Up @@ -112,9 +112,9 @@ public static function totalImportsMsg()
return '<options=bold;fg=yellow>'.ImportsAnalyzer::$checkedRefCount.' imports were checked under:</>';
}

private static function getBladeStats($stats, $checkedFilesCount): string
private static function getBladeStats($stats, $filesCount): string
{
$output = self::blue($checkedFilesCount).'blade'.($checkedFilesCount <= 1 ? '' : 's');
$output = self::blue($filesCount).'blade'.($filesCount <= 1 ? '' : 's');
$numPaths = count($stats);
$output .= self::hyphen();
$i = 0;
Expand All @@ -127,24 +127,24 @@ private static function getBladeStats($stats, $checkedFilesCount): string
return $output;
}

private static function getRouteStats($routeFilesCount)
private static function getRouteStats($count)
{
return ' - <fg=blue>'.$routeFilesCount.'</> route'.($routeFilesCount <= 1 ? '' : 's').PHP_EOL;
return ' - <fg=blue>'.$count.'</> route'.($count <= 1 ? '' : 's').PHP_EOL;
}

private static function getFilesStats($checkedFilesCount)
private static function getFilesStats($count)
{
return ' - <fg=blue>'.$checkedFilesCount.'</> class'.($checkedFilesCount <= 1 ? '' : 'es').PHP_EOL;
return ' - <fg=blue>'.$count.'</> class'.($count <= 1 ? '' : 'es').PHP_EOL;
}

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

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

private static function hyphen()
Expand All @@ -159,9 +159,7 @@ private static function files($count)

private static function addLine($path, $count, $i, $numPaths)
{
$output = '';
$path = self::normalize($path);
$output .= self::green($path);
$output = self::green(self::normalize($path));
$output .= self::files($count);
if ($i !== $numPaths) {
$output .= self::hyphen();
Expand Down
14 changes: 9 additions & 5 deletions src/Features/CheckImports/ExistenceChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

class ExistenceChecker
{
public static function check($class, $absFilePath): bool
public static function check($import, $absFilePath): bool
{
if (! self::isAbsent($class) || \function_exists($class)) {
if (self::entityExists($import)) {
return true;
}

Expand All @@ -18,15 +18,19 @@ public static function check($class, $absFilePath): bool
return false;
}

if (! self::isAbsent($class) || \function_exists($class)) {
if (self::entityExists($import)) {
return true;
}

return false;
}

private static function isAbsent($class)
private static function entityExists($import)
{
return ! class_exists($class) && ! interface_exists($class) && ! trait_exists($class) && ! (function_exists('enum_exists') && enum_exists($class));
return class_exists($import) ||
interface_exists($import) ||
trait_exists($import) ||
function_exists($import) ||
(function_exists('enum_exists') && enum_exists($import));
}
}
25 changes: 11 additions & 14 deletions src/Features/CheckImports/Handlers/ExtraCorrectImports.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@

class ExtraCorrectImports
{
public static function handle($unusedCorrectImports, $absFilePath)
public static function handle($extraCorrectImports, $absFilePath)
{
foreach ($unusedCorrectImports as $class) {
self::extraImport($absFilePath, $class[0], $class[1]);
}
}
$printer = ErrorPrinter::singleton();

public static function extraImport($absPath, $class, $lineNumber)
{
ErrorPrinter::singleton()->simplePendError(
$class,
$absPath,
$lineNumber,
'extraImport',
'Extra Import:'
);
foreach ($extraCorrectImports as [$class, $lineNumber]) {
$printer->simplePendError(
$class,
$absFilePath,
$lineNumber,
'extraCorrectImport',
'Extra Import:'
);
}
}
}
25 changes: 11 additions & 14 deletions src/Features/CheckImports/Handlers/ExtraWrongImports.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@

class ExtraWrongImports
{
public static function handle($unusedWrongImports, $absPath)
public static function handle($extraWrongImports, $absPath)
{
foreach ($unusedWrongImports as $class) {
self::wrongImport($absPath, $class[0], $class[1]);
}
}
$printer = ErrorPrinter::singleton();

public static function wrongImport($absPath, $class, $lineNumber)
{
ErrorPrinter::singleton()->simplePendError(
"use $class;",
$absPath,
$lineNumber,
'wrongImport',
'Unused & wrong import:'
);
foreach ($extraWrongImports as [$class, $lineNumber]) {
$printer->simplePendError(
"use $class;",
$absPath,
$lineNumber,
'extraWrongImport',
'Unused & wrong import:'
);
}
}
}
6 changes: 3 additions & 3 deletions src/Features/CheckImports/Handlers/FixWrongClassRefs.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ private static function printFixation($absPath, $wrongClass, $lineNumber, $corre
);
}

private static function wrongUsedClassError($absPath, $class, $lineNumber)
private static function wrongUsedClassError($absPath, $class, $line)
{
ErrorPrinter::singleton()->simplePendError(
$class,
$absPath,
$lineNumber,
'wrongUsedClassError',
$line,
'wrongClassRef',
'Class does not exist:'
);
}
Expand Down
16 changes: 7 additions & 9 deletions src/Features/CheckImports/Handlers/PrintWrongClassRefs.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ public static function handle(array $wrongClassRefs, $absFilePath)
$printer = ErrorPrinter::singleton();

foreach ($wrongClassRefs as $classReference) {
$wrongClassRef = $classReference['class'];
$line = $classReference['line'];

self::wrongRef($printer, $wrongClassRef, $absFilePath, $line);
$printer->simplePendError(
$classReference['class'],
$absFilePath,
$classReference['line'],
'wrongClassReference',
'Class Reference does not exist:'
);
}
}

private static function wrongRef($printer, $wrongClassRef, $absFilePath, $line): void
{
$printer->simplePendError($wrongClassRef, $absFilePath, $line, 'wrongReference', 'Inline class Ref does not exist:');
}
}
17 changes: 17 additions & 0 deletions src/Features/CheckRoutes/Installer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Imanghafoori\LaravelMicroscope\Features\CheckRoutes;

use Illuminate\Support\Facades\Route;

class Installer
{
public static function spyRouter()
{
$router = new SpyRouter(app('events'), app());
app()->singleton('router', function ($app) use ($router) {
return $router;
});
Route::swap($router);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace Imanghafoori\LaravelMicroscope\Features\RouteOverride;
namespace Imanghafoori\LaravelMicroscope\Features\CheckRoutes;

use Illuminate\Routing\RouteCollection;
use Imanghafoori\LaravelMicroscope\Features\RouteOverride\RouteDefinitionConflict;

class SpyRouteCollection extends RouteCollection
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Imanghafoori\LaravelMicroscope\Features\RouteOverride;
namespace Imanghafoori\LaravelMicroscope\Features\CheckRoutes;

use Closure;
use Illuminate\Routing\Router;
Expand All @@ -14,7 +14,7 @@ class SpyRouter extends Router
public $routePaths = [];

/**
* @var \Imanghafoori\LaravelMicroscope\Features\RouteOverride\SpyRouteCollection
* @var \Imanghafoori\LaravelMicroscope\Features\CheckRoutes\SpyRouteCollection
*/
private $routesSpy = null;

Expand Down
Loading

0 comments on commit 35389b5

Please sign in to comment.