|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\LaravelMicroscope\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson; |
|
7
|
|
|
use Imanghafoori\LaravelMicroscope\Analyzers\FunctionCall; |
|
8
|
|
|
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter; |
|
9
|
|
|
use Imanghafoori\LaravelMicroscope\ErrorTypes\EnvFound; |
|
10
|
|
|
use Imanghafoori\LaravelMicroscope\FileReaders\Paths; |
|
11
|
|
|
use Imanghafoori\LaravelMicroscope\LaravelPaths\FilePath; |
|
12
|
|
|
use Imanghafoori\LaravelMicroscope\LaravelPaths\LaravelPaths; |
|
13
|
|
|
use Imanghafoori\LaravelMicroscope\SpyClasses\RoutePaths; |
|
14
|
|
|
|
|
15
|
|
|
class CheckBadPractice extends Command |
|
16
|
|
|
{ |
|
17
|
|
|
protected $signature = 'check:bad_practices'; |
|
18
|
|
|
|
|
19
|
|
|
protected $description = 'Checks for bad practices'; |
|
20
|
|
|
|
|
21
|
|
|
public function handle() |
|
22
|
|
|
{ |
|
23
|
|
|
event('microscope.start.command'); |
|
24
|
|
|
$this->info('Checking bad practices...'); |
|
25
|
|
|
|
|
26
|
|
|
$this->checkPaths(RoutePaths::get()); |
|
27
|
|
|
$this->checkPaths(Paths::getAbsFilePaths(LaravelPaths::migrationDirs())); |
|
28
|
|
|
$this->checkPaths(Paths::getAbsFilePaths(LaravelPaths::factoryDirs())); |
|
29
|
|
|
$this->checkPaths(Paths::getAbsFilePaths(LaravelPaths::seeders())); |
|
30
|
|
|
$this->checkPsr4Classes(); |
|
31
|
|
|
|
|
32
|
|
|
event('microscope.finished.checks', [$this]); |
|
33
|
|
|
$this->info('&It is recommended use env() calls, only and only in config files.'); |
|
34
|
|
|
$this->info('Otherwise you can NOT cache your config files using "config:cache"'); |
|
35
|
|
|
$this->info('https://laravel.com/docs/5.5/configuration#configuration-caching'); |
|
36
|
|
|
|
|
37
|
|
|
return app(ErrorPrinter::class)->hasErrors() ? 1 : 0; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
private function checkForEnv($absPath) |
|
41
|
|
|
{ |
|
42
|
|
|
$tokens = token_get_all(file_get_contents($absPath)); |
|
43
|
|
|
|
|
44
|
|
|
foreach ($tokens as $i => $token) { |
|
45
|
|
|
if (($index = FunctionCall::isGlobalCall('env', $tokens, $i))) { |
|
46
|
|
|
EnvFound::isMissing($absPath, $tokens[$index][2], $tokens[$index][1]); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function checkPaths($paths) |
|
52
|
|
|
{ |
|
53
|
|
|
foreach ($paths as $filePath) { |
|
54
|
|
|
$this->checkForEnv($filePath); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function checkPsr4Classes() |
|
59
|
|
|
{ |
|
60
|
|
|
$psr4 = ComposerJson::readAutoload(); |
|
61
|
|
|
|
|
62
|
|
|
foreach ($psr4 as $_namespace => $dirPath) { |
|
63
|
|
|
foreach (FilePath::getAllPhpFiles($dirPath) as $filePath) { |
|
64
|
|
|
$this->checkForEnv($filePath->getRealPath()); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|