CheckDD   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
B checkForDD() 0 17 8
A handle() 0 17 2
A checkPaths() 0 7 2
A checkPsr4Classes() 0 11 3
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\ddFound;
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 CheckDD extends Command
16
{
17
    public static $checkedCallsNum = 0;
18
19
    protected $signature = 'check:dd {--d|detailed : Show files being checked}';
20
21
    protected $description = 'Checks the debug functions';
22
23
    public function handle()
24
    {
25
        event('microscope.start.command');
26
        $this->info('Checking dd...');
27
28
        $this->checkPaths(RoutePaths::get());
29
        $this->checkPaths(Paths::getAbsFilePaths(LaravelPaths::migrationDirs()));
30
        $this->checkPaths(Paths::getAbsFilePaths(LaravelPaths::seeders()));
31
        $this->checkPaths(Paths::getAbsFilePaths(LaravelPaths::factoryDirs()));
32
        $this->checkPsr4Classes();
33
34
        $this->getOutput()->writeln(' - Finished looking for debug functions. ('.self::$checkedCallsNum.' files checked)');
35
36
        event('microscope.finished.checks', [$this]);
37
38
        return app(ErrorPrinter::class)->hasErrors() ? 1 : 0;
39
    }
40
41
    private function checkForDD($absPath)
42
    {
43
        $tokens = token_get_all(file_get_contents($absPath));
44
45
        foreach ($tokens as $i => $token) {
46
            if (
47
                ($index = FunctionCall::isGlobalCall('dd', $tokens, $i)) ||
48
                ($index = FunctionCall::isGlobalCall('microscope_pretty_print_route', $tokens, $i)) ||
49
                ($index = FunctionCall::isGlobalCall('microscope_dd_listeners', $tokens, $i)) ||
50
                ($index = FunctionCall::isGlobalCall('microscope_write_route', $tokens, $i)) ||
51
                ($index = FunctionCall::isGlobalCall('dump', $tokens, $i)) ||
52
                ($index = FunctionCall::isGlobalCall('ddd', $tokens, $i))
53
            ) {
54
                ddFound::isMissing($absPath, $tokens[$index][2], $tokens[$index][1]);
55
            }
56
        }
57
    }
58
59
    private function checkPaths($paths)
60
    {
61
        foreach ($paths as $filePath) {
62
            self::$checkedCallsNum++;
63
            $this->checkForDD($filePath);
64
        }
65
    }
66
67
    private function checkPsr4Classes()
68
    {
69
        $psr4 = ComposerJson::readAutoload();
70
71
        foreach ($psr4 as $_namespace => $dirPath) {
72
            foreach (FilePath::getAllPhpFiles($dirPath) as $filePath) {
73
                self::$checkedCallsNum++;
74
                $this->checkForDD($filePath->getRealPath());
75
            }
76
        }
77
    }
78
}
79