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

EnvVariablesExists::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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 EnvVariablesExists implements Check
12
{
13
    /**
14
     * Stores processed var names
15
     *
16
     * @var array
17
     */
18
    private $processed = [];
19
20
    /**
21
     * The empty results of performed scan
22
     *
23
     * @var array
24
     */
25
    public $undefined = 0;
26
27
    /**
28
     * The name of the check.
29
     *
30
     * @param array $config
31
     * @return string
32
     */
33
    public function name(array $config): string
34
    {
35
        return trans('self-diagnosis::checks.env_variables_exist.name');
36
    }
37
38
    /**
39
     * The error message to display in case the check does not pass.
40
     *
41
     * @param array $config
42
     * @return string
43
     */
44
    public function message(array $config): string
45
    {
46
        return trans('self-diagnosis::checks.env_variables_exist.message', [
47
            'undefined' => $this->undefined,
48
        ]);
49
    }
50
51
    /**
52
     * Directories to start recursive search for env()'s from
53
     * Defaults to config_path()
54
     *
55
     * @var string $dir
56
     */
57
    public $paths;
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
        $this->paths = Collection::make(Arr::get($config, 'directories', []));
0 ignored issues
show
Documentation Bug introduced by
It seems like \Illuminate\Support\Coll...directories', array())) of type object<Illuminate\Support\Collection> is incompatible with the declared type string of property $paths.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
69
70 4
        foreach ($this->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->undefined === 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++;
131
        }
132 4
    }
133
134 4
    private function recursiveDirSearch(string $folder, string $pattern): array
135
    {
136 4
        if (!file_exists($folder)) {
137
            return [];
138
        }
139
140 4
        $files = new RegexIterator(
141 4
            new RecursiveIteratorIterator(
142 4
                new RecursiveDirectoryIterator($folder)
143
            ),
144 4
            $pattern, RegexIterator::GET_MATCH
145
        );
146
147 4
        $list = [];
148
149 4
        foreach ($files as $file) {
150 4
            $list = array_merge($list, $file);
151
        }
152
153 4
        return $list;
154
    }
155
}
156