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

UsedEnvironmentVariablesAreDefined::message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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\((.*?)\)#',
76 4
                    str_replace(["\n", "\r"], '', file_get_contents($file)),
77 3
                    $values
78
                );
79
80 4
                if (is_array($values)) {
81 4
                    foreach ($values[1] as $value) {
82 4
                        $result = $this->getResult(
83 4
                            explode(',', str_replace(["'", '"', ' '], '', $value))
84
                        );
85
86 4
                        if (!$result) {
87 4
                            continue;
88
                        }
89
90 4
                        $this->storeResult($result);
91
                    }
92
                }
93
            }
94
        }
95
96 4
        return $this->amount === 0;
97
    }
98
99
    /**
100
     * Get result based on comma separated parsed env() parameters
101
     *
102
     * @param array $values
103
     * @return object|bool
104
     */
105 4
    private function getResult(array $values)
106
    {
107 4
        $envVar = $values[0];
108
109 4
        if (in_array($envVar, $this->processed)) {
110 4
            return false;
111
        }
112
113 4
        $this->processed[] = $envVar;
114
115
        return (object)[
116 4
            'envVar' => $envVar,
117 4
            'hasValue' => env($envVar) !== null,
118 4
            'hasDefault' => isset($values[1]),
119
        ];
120
    }
121
122
    /**
123
     * Store result and optional runtime output
124
     *
125
     * @param $result
126
     */
127 4
    private function storeResult($result)
128
    {
129 4
        if (!$result->hasValue && !$result->hasDefault) {
130 4
            $this->undefined[] = $result->envVar;
131 4
            $this->amount++;
132
        }
133 4
    }
134
135 4
    private function recursiveDirSearch(string $folder, string $pattern): array
136
    {
137 4
        if (!file_exists($folder)) {
138
            return [];
139
        }
140
141 4
        $files = new RegexIterator(
142 4
            new RecursiveIteratorIterator(
143 4
                new RecursiveDirectoryIterator($folder)
144
            ),
145 4
            $pattern, RegexIterator::GET_MATCH
146
        );
147
148 4
        $list = [];
149
150 4
        foreach ($files as $file) {
151 4
            $list = array_merge($list, $file);
152
        }
153
154 4
        return $list;
155
    }
156
}
157