Completed
Pull Request — master (#84)
by Maarten
03:56
created

UsedEnvironmentVariablesAreDefined   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 148
ccs 45
cts 48
cp 0.9375
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 4 1
A message() 0 7 1
A check() 0 34 5
A getResult() 0 16 2
A storeResult() 0 7 3
A recursiveDirSearch() 0 21 3
1
<?php
2
3
namespace BeyondCode\SelfDiagnosis\Checks;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Collection;
7
use RecursiveDirectoryIterator;
8
use RecursiveIteratorIterator;
9
use RegexIterator;
10
11
class UsedEnvironmentVariablesAreDefined implements Check
12
{
13
    /**
14
     * Stores processed var names
15
     *
16
     * @var array
17
     */
18
    private $processed = [];
19
20
    /**
21
     * Stores undefined var names
22
     *
23
     * @var array
24
     */
25
    public $undefined = [];
26
27
    /**
28
     * The amount of undefined .env variables
29
     *
30
     * @var integer
31
     */
32
    public $amount = 0;
33
34
    /**
35
     * The name of the check.
36
     *
37
     * @param array $config
38
     * @return string
39
     */
40
    public function name(array $config): string
41
    {
42
        return trans('self-diagnosis::checks.used_env_variables_are_defined.name');
43
    }
44
45
    /**
46
     * The error message to display in case the check does not pass.
47
     *
48
     * @param array $config
49
     * @return string
50
     */
51 4
    public function message(array $config): string
52
    {
53 4
        return trans('self-diagnosis::checks.used_env_variables_are_defined.message', [
54 4
            'amount' => $this->amount,
55 4
            'undefined' => implode(PHP_EOL, $this->undefined),
56
        ]);
57
    }
58
59
    /**
60
     * Perform the actual verification of this check.
61
     *
62
     * @param array $config
63
     * @return bool
64
     * @throws \Exception
65
     */
66 4
    public function check(array $config): bool
67
    {
68 4
        $paths = Collection::make(Arr::get($config, 'directories', []));
69
70 4
        foreach ($paths as $path) {
71 4
            $files = $this->recursiveDirSearch($path, '/.*?.php/');
72
73 4
            foreach ($files as $file) {
74 4
                preg_match_all(
75 4
                    '# env\((.*?)\)| getenv\((.*?)\)#',
76 4
                    str_replace(["\n", "\r"], '', file_get_contents($file)),
77 3
                    $values
78
                );
79
80 4
                $values = array_filter(
81 4
                    array_merge($values[1], $values[2])
82
                );
83
84 4
                foreach ($values as $value) {
85 4
                    $result = $this->getResult(
86 4
                        explode(',', str_replace(["'", '"', ' '], '', $value))
87
                    );
88
89 4
                    if (!$result) {
90 4
                        continue;
91
                    }
92
93 4
                    $this->storeResult($result);
94
                }
95
            }
96
        }
97
98 4
        return $this->amount === 0;
99
    }
100
101
    /**
102
     * Get result based on comma separated parsed env() parameters
103
     *
104
     * @param array $values
105
     * @return object|bool
106
     */
107 4
    private function getResult(array $values)
108
    {
109 4
        $envVar = $values[0];
110
111 4
        if (in_array($envVar, $this->processed)) {
112 4
            return false;
113
        }
114
115 4
        $this->processed[] = $envVar;
116
117
        return (object)[
118 4
            'envVar' => $envVar,
119 4
            'hasValue' => env($envVar) !== null,
120 4
            'hasDefault' => isset($values[1]),
121
        ];
122
    }
123
124
    /**
125
     * Store result and optional runtime output
126
     *
127
     * @param $result
128
     */
129 4
    private function storeResult($result)
130
    {
131 4
        if (!$result->hasValue && !$result->hasDefault) {
132 4
            $this->undefined[] = $result->envVar;
133 4
            $this->amount++;
134
        }
135 4
    }
136
137 4
    private function recursiveDirSearch(string $folder, string $pattern): array
138
    {
139 4
        if (!file_exists($folder)) {
140
            return [];
141
        }
142
143 4
        $files = new RegexIterator(
144 4
            new RecursiveIteratorIterator(
145 4
                new RecursiveDirectoryIterator($folder)
146
            ),
147 4
            $pattern, RegexIterator::GET_MATCH
148
        );
149
150 4
        $list = [];
151
152 4
        foreach ($files as $file) {
153 4
            $list = array_merge($list, $file);
154
        }
155
156 4
        return $list;
157
    }
158
}
159