Completed
Push — master ( c3235d...667f42 )
by Maarten
01:16
created

EnvScan   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 84
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 19 2
B showOutput() 0 34 6
1
<?php
2
3
namespace Mtolhuys\LaravelEnvScanner\Commands;
4
5
use Illuminate\Console\Command;
6
use Mtolhuys\LaravelEnvScanner\LaravelEnvScanner;
7
8
class EnvScan extends Command
9
{
10
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = '
17
        env:scan 
18
            { --d|dir= : Specify directory to scan (defaults to your config folder) }
19
            { --a|all : Show result containing all used variables }
20
    ';
21
22
    private $scanner;
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Check environmental variables used in your app';
30
31
    /**
32
     * Execute the console command.
33
     *
34
     * @return mixed
35
     * @throws \Exception
36
     */
37
    public function handle()
38
    {
39
        $this->scanner = new LaravelEnvScanner(
40
            $this->option('dir')
41
        );
42
43
        if (! file_exists($this->scanner->dir)) {
44
            $this->error("{$this->scanner->dir} does not exist");
45
            exit();
46
        }
47
48
        $this->output->write(
49
            "<fg=green>Scanning:</fg=green> <fg=white>{$this->scanner->dir}/...</fg=white>\n"
50
        );
51
52
        $this->scanner->scan();
53
54
        $this->showOutput();
55
    }
56
57
    private function showOutput(): void
58
    {
59
        foreach ($this->scanner->warnings as $warning) {
60
            $this->warn("Warning: <fg=red>{$warning->invocation}</fg=red> found in {$warning->location}");
61
        }
62
63
        if ($this->option('all')) {
64
            if (empty($this->scanner->results['rows'])) {
65
                $this->line('Nothing there...');
66
67
                return;
68
            }
69
70
            $this->table([
71
                "Locations ({$this->scanner->results['locations']})",
72
                "Defined ({$this->scanner->results['defined']})",
73
                "Depending on default ({$this->scanner->results['depending_on_default']})",
74
                "Undefined ({$this->scanner->results['undefined']})",
75
            ], $this->scanner->results['rows']);
76
77
            return;
78
        }
79
80
        if (empty($this->scanner->warnings) && $this->scanner->results['undefined'] === 0) {
81
            $this->info('Looking good!');
82
83
            return;
84
        }
85
86
        $this->warn(
87
            "<fg=red>{$this->scanner->results['undefined']} undefined variable(s) found in {$this->scanner->dir}/...</fg=red>"
88
        );
89
        $this->table([], $this->scanner->undefined);
90
    }
91
}
92