Skip to content

Commit

Permalink
scan classmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Mar 16, 2024
1 parent 3d6d227 commit 01fc8d0
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
12 changes: 8 additions & 4 deletions src/Commands/CheckDynamicWhereMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Concerns\QueriesRelationships;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Str;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
Expand Down Expand Up @@ -69,7 +70,7 @@ public function handle(ErrorPrinter $errorPrinter)
event('microscope.start.command');
$this->info('Soaring like an eagle...');

Filters::$filters['is_sub_class_of'] = IsSubClassOf::class;
Filters::$filters['is_subclass_of'] = IsSubClassOf::class;

return $this->patternCommand($errorPrinter);
}
Expand All @@ -92,16 +93,19 @@ private function getPatterns(): array

return [
'pattern_name_1' => [
'search' => '::<name>(',
'replace' => '::query()->where(<1>, ',
'search' => '<class_ref>::<name>(',
'replace' => '<1>::query()->where(<2>, ',
'filters' => [
1 => [
'is_subclass_of' => Model::class,
],
2 => [
[$dynamicWhere, null],
],
],
'mutator' => $mutator,
],
'pattern_name_3' => [
'pattern_name_2' => [
'search' => ')-><name>(<in_between>)',
'replace' => ')->where(<1>, <2>)',
'filters' => [
Expand Down
16 changes: 10 additions & 6 deletions src/Commands/CheckEarlyReturns.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Imanghafoori\LaravelMicroscope\Features\CheckImports\Reporters\Psr4Report;
use Imanghafoori\LaravelMicroscope\FileReaders\FilePath;
use Imanghafoori\LaravelMicroscope\ForPsr4LoadedClasses;
use Imanghafoori\LaravelMicroscope\Iterators\ClassMapIterator;

class CheckEarlyReturns extends Command
{
Expand All @@ -29,9 +30,9 @@ public function handle()

$fileName = ltrim($this->option('file'), '=');
$folder = ltrim($this->option('folder'), '=');
$psr4Stats = self::applyCheckEarly($fileName, $folder, $this->option('nofix'));
[$psr4Stats, $classMapStats] = self::applyCheckEarly($fileName, $folder, $this->option('nofix'));
$this->getOutput()->writeln(implode(PHP_EOL, [
Psr4Report::printAutoload($psr4Stats, []),
Psr4Report::printAutoload($psr4Stats, $classMapStats),
]));

return ErrorPrinter::singleton()->hasErrors() ? 1 : 0;
Expand All @@ -47,7 +48,7 @@ private function startWarning()

private static function getParams($nofix): array
{
$params = [
return [
'nofix' => $nofix,
'nofixCallback' => function ($absPath) {
$this->line('<fg=red> - '.FilePath::getRelativePath($absPath).'</fg=red>');
Expand All @@ -56,12 +57,15 @@ private static function getParams($nofix): array
$this->warn(PHP_EOL.$tries.' fixes applied to: '.class_basename($filePath));
},
];

return $params;
}

public static function applyCheckEarly(string $fileName, string $folder, $nofix): array
{
return ForPsr4LoadedClasses::check([CheckEarlyReturn::class], self::getParams($nofix), $fileName, $folder);
$check = [CheckEarlyReturn::class];
$params = self::getParams($nofix);
$psr4stats = ForPsr4LoadedClasses::check($check, $params, $fileName, $folder);
$classMapStats = ClassMapIterator::iterate(base_path(), $check, $params, $fileName, $folder);

return [$psr4stats, $classMapStats];
}
}
11 changes: 8 additions & 3 deletions src/Commands/CheckEndIf.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\Features\CheckImports\Reporters\Psr4Report;
use Imanghafoori\LaravelMicroscope\ForPsr4LoadedClasses;
use Imanghafoori\LaravelMicroscope\Iterators\ClassMapIterator;

class CheckEndIf extends Command
{
Expand All @@ -25,10 +26,10 @@ public function handle(ErrorPrinter $errorPrinter)

$errorPrinter->printer = $this->output;

$psr4Stats = self::applyRubySyntaxCheck($fileName, $folder);
[$psr4Stats, $classMapStats] = self::applyRubySyntaxCheck($fileName, $folder);

$this->getOutput()->writeln(implode(PHP_EOL, [
Psr4Report::printAutoload($psr4Stats, []),
Psr4Report::printAutoload($psr4Stats, $classMapStats),
]));

return ErrorPrinter::singleton()->hasErrors() ? 1 : 0;
Expand All @@ -44,6 +45,10 @@ private function startWarning()

public static function applyRubySyntaxCheck(string $fileName, string $folder)
{
return ForPsr4LoadedClasses::check([CheckRubySyntax::class], [], $fileName, $folder);
$check = [CheckRubySyntax::class];
$psr4stats = ForPsr4LoadedClasses::check($check, [], $fileName, $folder);
$classMapStats = ClassMapIterator::iterate(base_path(), $check, [], $fileName, $folder);

return [$psr4stats, $classMapStats];
}
}
7 changes: 5 additions & 2 deletions src/Commands/CheckPsr12.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Imanghafoori\LaravelMicroscope\Features\ActionComments\ActionsComments;
use Imanghafoori\LaravelMicroscope\Features\CheckImports\Reporters\Psr4Report;
use Imanghafoori\LaravelMicroscope\ForPsr4LoadedClasses;
use Imanghafoori\LaravelMicroscope\Iterators\ClassMapIterator;
use Imanghafoori\LaravelMicroscope\Traits\LogsErrors;

class CheckPsr12 extends Command
Expand All @@ -34,10 +35,12 @@ public function handle(ErrorPrinter $errorPrinter)
$fileName = ltrim($this->option('file'), '=');
$folder = ltrim($this->option('folder'), '=');

$psr4Stats = ForPsr4LoadedClasses::check([CurlyBraces::class], [], $fileName, $folder);
$check = [CurlyBraces::class];
$psr4Stats = ForPsr4LoadedClasses::check($check, [], $fileName, $folder);
$classMapStats = ClassMapIterator::iterate(base_path(), $check, [], $fileName, $folder);

$this->getOutput()->writeln(implode(PHP_EOL, [
Psr4Report::printAutoload($psr4Stats, []),
Psr4Report::printAutoload($psr4Stats, $classMapStats),
]));

$this->finishCommand($errorPrinter);
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/EnforceHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function handle(ErrorPrinter $errorPrinter)
event('microscope.start.command');
$this->info('Soaring like an eagle...');

Filters::$filters['is_sub_class_of'] = IsSubClassOf::class;
Filters::$filters['is_subclass_of'] = IsSubClassOf::class;
Filters::$filters['full_namespace_pattern'] = FullNamespaceIs::class;
Filters::$filters['namespace_pattern'] = NamespaceIs::class;

Expand All @@ -49,7 +49,7 @@ private function getPatterns(): array
'filters' => [
1 => [
'full_namespace_pattern' => 'Illuminate\\Support\\*',
'is_sub_class_of' => Facade::class,
'is_subclass_of' => Facade::class,
'in_array' => ['Auth', 'Session', 'Config', 'Cache', 'Redirect', 'Request'],
],
],
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/EnforceQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function handle(ErrorPrinter $errorPrinter)
event('microscope.start.command');
$this->info('Soaring like an eagle...');

Filters::$filters['is_sub_class_of'] = IsSubClassOf::class;
Filters::$filters['is_subclass_of'] = IsSubClassOf::class;

return $this->patternCommand($errorPrinter);
}
Expand All @@ -38,7 +38,7 @@ private function getPatterns(): array
'replace' => '<1>::query()-><2>',
'filters' => [
1 => [
'is_sub_class_of' => Model::class,
'is_subclass_of' => Model::class,
],
2 => [
'in_array' => $this->getMethods(),
Expand Down

0 comments on commit 01fc8d0

Please sign in to comment.