1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\LaravelMicroscope\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Imanghafoori\LaravelMicroscope\BladeFiles; |
8
|
|
|
use Imanghafoori\LaravelMicroscope\CheckClassReferencesAreValid; |
9
|
|
|
use Imanghafoori\LaravelMicroscope\Checks\CheckClassReferences; |
10
|
|
|
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter; |
11
|
|
|
use Imanghafoori\LaravelMicroscope\FileReaders\Paths; |
12
|
|
|
use Imanghafoori\LaravelMicroscope\LaravelPaths\LaravelPaths; |
13
|
|
|
use Imanghafoori\LaravelMicroscope\Psr4Classes; |
14
|
|
|
use Imanghafoori\LaravelMicroscope\SpyClasses\RoutePaths; |
15
|
|
|
use Imanghafoori\LaravelMicroscope\Traits\LogsErrors; |
16
|
|
|
|
17
|
|
|
class CheckImports extends Command |
18
|
|
|
{ |
19
|
|
|
use LogsErrors; |
20
|
|
|
|
21
|
|
|
protected $signature = 'check:imports {--d|detailed : Show files being checked} {--s|nofix : avoids the automatic fixes}'; |
22
|
|
|
|
23
|
|
|
protected $description = 'Checks the validity of use statements'; |
24
|
|
|
|
25
|
|
|
public function handle(ErrorPrinter $errorPrinter) |
26
|
|
|
{ |
27
|
|
|
event('microscope.start.command'); |
28
|
|
|
$this->info('Checking imports...'); |
29
|
|
|
|
30
|
|
|
$this->option('nofix') && config(['microscope.no_fix' => true]); |
31
|
|
|
|
32
|
|
|
$errorPrinter->printer = $this->output; |
33
|
|
|
|
34
|
|
|
$this->checkFilePaths(RoutePaths::get()); |
35
|
|
|
$this->checkFilePaths(Paths::getAbsFilePaths(app()->configPath())); |
36
|
|
|
$this->checkFilePaths(Paths::getAbsFilePaths(LaravelPaths::seeders())); |
37
|
|
|
$this->checkFilePaths(Paths::getAbsFilePaths(LaravelPaths::migrationDirs())); |
38
|
|
|
$this->checkFilePaths(Paths::getAbsFilePaths(LaravelPaths::factoryDirs())); |
39
|
|
|
|
40
|
|
|
Psr4Classes::check([CheckClassReferencesAreValid::class]); |
41
|
|
|
|
42
|
|
|
// Checks the blade files for class references. |
43
|
|
|
BladeFiles::check([CheckClassReferences::class]); |
44
|
|
|
|
45
|
|
|
$this->finishCommand($errorPrinter); |
46
|
|
|
$this->getOutput()->writeln(' - '.CheckClassReferences::$refCount.' class references were checked within: '.Psr4Classes::$checkedFilesNum.' classes and '.BladeFiles::$checkedFilesNum.' blade files'); |
47
|
|
|
|
48
|
|
|
$errorPrinter->printTime(); |
49
|
|
|
|
50
|
|
View Code Duplication |
if (random_int(1, 7) == 2 && Str::startsWith(request()->server('argv')[1] ?? '', 'check:im')) { |
|
|
|
|
51
|
|
|
ErrorPrinter::thanks($this); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $errorPrinter->hasErrors() ? 1 : 0; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function checkFilePaths($paths) |
58
|
|
|
{ |
59
|
|
|
foreach ($paths as $path) { |
60
|
|
|
$tokens = token_get_all(file_get_contents($path)); |
61
|
|
|
CheckClassReferences::check($tokens, $path); |
62
|
|
|
CheckClassReferencesAreValid::checkAtSignStrings($tokens, $path, true); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.