Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Mar 7, 2024
1 parent af689ca commit 8f0c7f2
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 133 deletions.
17 changes: 15 additions & 2 deletions src/Commands/CheckCompact.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Console\Command;
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\ErrorTypes\CompactCall;
use Imanghafoori\LaravelMicroscope\FileReaders\PhpFinder;
use Imanghafoori\LaravelMicroscope\SpyClasses\RoutePaths;
use Imanghafoori\TokenAnalyzer\FunctionCall;
Expand Down Expand Up @@ -88,10 +87,24 @@ private function checkMethodBodyForCompact($absPath, $methodBody, $vars)

unset($vars['$this']);
$missingVars = array_diff_key($compactVars, $vars);
$missingVars && CompactCall::warn($absPath, $methodBody[$pp][2], $missingVars);

self::compactError(
$absPath,
$methodBody[$pp][2],
$missingVars,
'CompactCall',
'compact() function call has problems man!');
}
}

private static function compactError($path, $lineNumber, $absent, $key, $header)
{
$p = ErrorPrinter::singleton();
$errorData = $p->color(\implode(', ', array_keys($absent))).' does not exist';

$p->addPendingError($path, $lineNumber, $key, $header, $errorData);
}

private function collectSignatureVars($tokens, $i)
{
[, $signatures,$conditionCloseIndex] = Ifs::readCondition($tokens, $i);
Expand Down
49 changes: 3 additions & 46 deletions src/ErrorReporters/ConsolePrinterInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

use Illuminate\Support\Facades\Event;
use Illuminate\Support\Str;
use Imanghafoori\LaravelMicroscope\ErrorTypes\CompactCall;
use Imanghafoori\LaravelMicroscope\ErrorTypes\EnvFound;
use Imanghafoori\LaravelMicroscope\Features\CheckDD\ddFound;
use Imanghafoori\LaravelMicroscope\Features\CheckEnvCalls\EnvFound;
use Imanghafoori\LaravelMicroscope\Features\CheckView\ViewsInstaller;
use Imanghafoori\LaravelMicroscope\Features\RouteOverride\Installer as RouteOverrideInstaller;

Expand Down Expand Up @@ -49,61 +48,19 @@ protected static function getKey($commandType)

public static function boot()
{
Event::listen(ddFound::class, function (ddFound $event) {
$data = $event->data;
ErrorPrinter::singleton()->simplePendError(
$data['name'],
$data['absPath'],
$data['lineNumber'],
'ddFound',
'Debug function found: '
);
});
ddFound::listen();

ViewsInstaller::boot();

self::compactCall();

RouteOverrideInstaller::install();

Event::listen(EnvFound::class, function (EnvFound $event) {
$data = $event->data;
ErrorPrinter::singleton()->simplePendError(
$data['name'],
$data['absPath'],
$data['lineNumber'],
'envFound',
'env() function found: '
);
});
EnvFound::listen();

Event::listen('microscope.finished.checks', function ($command) {
self::finishCommand($command);
});
}

private static function compactCall()
{
Event::listen(CompactCall::class, function ($event) {
$data = $event->data;

self::compactError(
$data['absPath'],
$data['lineNumber'],
$data['name'],
'CompactCall',
'compact() function call has problems man!');
});
}

private static function compactError($path, $lineNumber, $absent, $key, $header)
{
$p = ErrorPrinter::singleton();
$errorData = $p->color(\implode(', ', array_keys($absent))).' does not exist';

$p->addPendingError($path, $lineNumber, $key, $header, $errorData);
}

protected static function printErrorCount($lastTimeCount, $commandType, $errorCount)
{
$lastTimeError = $commandType.' errors, compared to the last run.';
Expand Down
26 changes: 10 additions & 16 deletions src/ErrorReporters/ErrorPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,7 @@ public function hasErrors()

public function logErrors()
{
$errList = $this->errorsList;

foreach ($errList as $list) {
foreach ($this->errorsList as $list) {
foreach ($list as $error) {
$this->printHeader($error->getHeader());
$this->print($error->getErrorData());
Expand All @@ -194,19 +192,7 @@ public function getCount($key)

public function printTime()
{
$this->logErrors && $this->printer->writeln('time: '.round(microtime(true) - microscope_start, 3).' (sec)', 2);
}

public static function thanks($command)
{
$command->line(PHP_EOL.'<fg=blue>|-------------------------------------------------|</>');
$command->line('<fg=blue>|----------- Star Me On Github -----------|</>');
$command->line('<fg=blue>|-------------------------------------------------|</>');
$command->line('<fg=blue>| Hey man, if you have found microscope useful |</>');
$command->line('<fg=blue>| Please consider giving it an star on github. |</>');
$command->line('<fg=blue>| \(^_^)/ Regards, Iman Ghafoori \(^_^)/ |</>');
$command->line('<fg=blue>|-------------------------------------------------|</>');
$command->line('https://github.com/imanghafoori1/microscope');
$this->logErrors && $this->printer->writeln($this->getTimeMessage(), 2);
}

/**
Expand Down Expand Up @@ -257,4 +243,12 @@ private static function is($pattern, $value)

return false;
}

private function getTimeMessage()
{
$duration = microtime(true) - microscope_start;
$duration = round($duration, 3);

return "time: {$duration} (sec)";
}
}
8 changes: 0 additions & 8 deletions src/ErrorTypes/CompactCall.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/ErrorTypes/EnvFound.php

This file was deleted.

22 changes: 10 additions & 12 deletions src/Features/CheckDD/CheckDDCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Console\Command;
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\FileReaders\Paths;
use Imanghafoori\LaravelMicroscope\FileReaders\PhpFinder;
use Imanghafoori\LaravelMicroscope\LaravelPaths\LaravelPaths;
use Imanghafoori\LaravelMicroscope\SpyClasses\RoutePaths;
Expand All @@ -29,17 +28,7 @@ public function handle()

$this->checkPaths(RoutePaths::get());

$paths = [
Paths::getAbsFilePaths(LaravelPaths::migrationDirs(), $file, $folder),
Paths::getAbsFilePaths(LaravelPaths::seedersDir(), $file, $folder),
Paths::getAbsFilePaths(LaravelPaths::factoryDirs(), $file, $folder),
];

foreach ($paths as $path) {
foreach ($path as $p) {
$this->checkPaths($p);
}
}
$this->checkMigrations($file, $folder);

$this->checkPsr4Classes();

Expand Down Expand Up @@ -89,4 +78,13 @@ private function checkPsr4Classes()
}
}
}

private function checkMigrations(string $fileName, string $folderName)
{
$paths = LaravelPaths::getMigrationsFiles($fileName, $folderName);

foreach ($paths as $path) {
$this->checkPaths($path);
}
}
}
16 changes: 16 additions & 0 deletions src/Features/CheckDD/ddFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,25 @@

namespace Imanghafoori\LaravelMicroscope\Features\CheckDD;

use Illuminate\Support\Facades\Event;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\ErrorTypes\MicroEvent;

class ddFound
{
use MicroEvent;

public static function listen()
{
Event::listen(self::class, function (ddFound $event) {
$data = $event->data;
ErrorPrinter::singleton()->simplePendError(
$data['name'],
$data['absPath'],
$data['lineNumber'],
'ddFound',
'Debug function found: '
);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<?php

namespace Imanghafoori\LaravelMicroscope\Commands;
namespace Imanghafoori\LaravelMicroscope\Features\CheckEnvCalls;

use Illuminate\Console\Command;
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\ErrorTypes\EnvFound;
use Imanghafoori\LaravelMicroscope\FileReaders\Paths;
use Imanghafoori\LaravelMicroscope\FileReaders\PhpFinder;
use Imanghafoori\LaravelMicroscope\LaravelPaths\LaravelPaths;
use Imanghafoori\LaravelMicroscope\SpyClasses\RoutePaths;
use Imanghafoori\TokenAnalyzer\FunctionCall;
use Imanghafoori\TokenAnalyzer\TokenManager;

class CheckBadPractice extends Command
class CheckEnvCallsCommand extends Command
{
protected $signature = 'check:bad_practices
{--f|file= : Pattern for file names to scan}
Expand All @@ -24,23 +23,17 @@ class CheckBadPractice extends Command
public function handle()
{
event('microscope.start.command');
$this->info('Checking bad practices...');
$this->info('Checking for env() calls outside config files...');

$this->checkPaths(RoutePaths::get());

$fileName = ltrim($this->option('file'), '=');
$folder = ltrim($this->option('folder'), '=');

$pathsList = [
Paths::getAbsFilePaths(LaravelPaths::migrationDirs(), $fileName, $folder),
Paths::getAbsFilePaths(LaravelPaths::seedersDir(), $fileName, $folder),
Paths::getAbsFilePaths(LaravelPaths::factoryDirs(), $fileName, $folder),
];
$paths = LaravelPaths::getMigrationsFiles($fileName, $folder);

foreach ($pathsList as $paths) {
foreach ($paths as $path) {
$this->checkPaths($path);
}
foreach ($paths as $path) {
$this->checkPaths($path);
}

$this->checkPsr4Classes();
Expand Down
26 changes: 26 additions & 0 deletions src/Features/CheckEnvCalls/EnvFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Imanghafoori\LaravelMicroscope\Features\CheckEnvCalls;

use Illuminate\Support\Facades\Event;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\ErrorTypes\MicroEvent;

class EnvFound
{
use MicroEvent;

public static function listen()
{
Event::listen(self::class, function (EnvFound $event) {
$data = $event->data;
ErrorPrinter::singleton()->simplePendError(
$data['name'],
$data['absPath'],
$data['lineNumber'],
'envFound',
'env() function found: '
);
});
}
}
25 changes: 9 additions & 16 deletions src/Features/CheckImports/CheckImportsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Imanghafoori\LaravelMicroscope\Features\CheckImports;

use DateInterval;
use Illuminate\Console\Command;
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
Expand All @@ -15,6 +14,7 @@
use Imanghafoori\LaravelMicroscope\Features\FacadeAlias\FacadeAliasesCheck;
use Imanghafoori\LaravelMicroscope\Features\FacadeAlias\FacadeAliasReplacer;
use Imanghafoori\LaravelMicroscope\Features\FacadeAlias\FacadeAliasReporter;
use Imanghafoori\LaravelMicroscope\Features\Thanks;
use Imanghafoori\LaravelMicroscope\FileReaders\FilePath;
use Imanghafoori\LaravelMicroscope\ForPsr4LoadedClasses;
use Imanghafoori\LaravelMicroscope\Iterators\BladeFiles;
Expand Down Expand Up @@ -108,7 +108,7 @@ public function handle()
$autoloadedFilesGen = FileIterators::checkFilePaths($autoloadedFilesGen, $paramProvider, $checks);

$foldersStats = FileIterators::checkFolders(
self::getLaravelFolders(),
$this->getLaravelFolders(),
$paramProvider,
$fileName,
$folder,
Expand Down Expand Up @@ -143,28 +143,21 @@ public function handle()

$errorPrinter->printTime();

if ($this->shouldRequestThanks()) {
ErrorPrinter::thanks($this);
if (Thanks::shouldShow()) {
$this->printThanks($this);
}

$this->line('');

return $errorPrinter->hasErrors() ? 1 : 0;
}

private function shouldRequestThanks(): bool
private function printThanks($command)
{
$key = 'microscope_thanks_throttle';

if (cache()->get($key)) {
return false;
$command->line(PHP_EOL);
foreach (Thanks::messages() as $msg) {
$command->line($msg);
}

// $currentCommandName = request()->server('argv')[1] ?? '';
$show = random_int(1, 5) === 2;
$show && cache()->set($key, '_', DateInterval::createFromDateString('3 days'));

return $show;
}

/**
Expand All @@ -189,7 +182,7 @@ private function getFilesStats()
/**
* @return array<string, \Generator>
*/
private static function getLaravelFolders()
private function getLaravelFolders()
{
return [
'config' => LaravelPaths::configDirs(),
Expand Down
Loading

0 comments on commit 8f0c7f2

Please sign in to comment.