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

EnvVariablesExists   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 126
ccs 33
cts 39
cp 0.8462
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 4 1
A message() 0 6 1
A check() 0 28 5
A getResult() 0 8 1
A storeResult() 0 6 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 EnvVariablesExists implements Check
12
{
13
    /**
14
     * The empty results of performed scan
15
     *
16
     * @var array
17
     */
18
    public $empty = 0;
19
20
    /**
21
     * The name of the check.
22
     *
23
     * @param array $config
24
     * @return string
25
     */
26
    public function name(array $config): string
27
    {
28
        return trans('self-diagnosis::checks.env_variables_exist.name');
29
    }
30
31
    /**
32
     * The error message to display in case the check does not pass.
33
     *
34
     * @param array $config
35
     * @return string
36
     */
37
    public function message(array $config): string
38
    {
39
        return trans('self-diagnosis::checks.env_variables_exist.message', [
40
            'empty' => $this->empty,
41
        ]);
42
    }
43
44
    /**
45
     * Directories to start recursive search for env()'s from
46
     * Defaults to config_path()
47
     *
48
     * @var string $dir
49
     */
50
    public $paths;
51
52
    /**
53
     * Perform the actual verification of this check.
54
     *
55
     * @param array $config
56
     * @return bool
57
     * @throws \Exception
58
     */
59 4
    public function check(array $config): bool
60
    {
61 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...
62
63 4
        foreach ($this->paths as $path) {
64 4
            $files = $this->recursiveDirSearch($path,  '/.*?.php/');
65
66 4
            foreach ($files as $file) {
67 4
                preg_match_all(
68 4
                    '#env\((.*?)\)#',
69 4
                    str_replace(["\n", "\r"], '', file_get_contents($file)),
70 3
                    $values
71
                );
72
73 4
                if (is_array($values)) {
74 4
                    foreach ($values[1] as $value) {
75 4
                        $result = $this->getResult(
76 4
                            explode(',', str_replace(["'", '"', ' '], '', $value))
77
                        );
78
79 4
                        $this->storeResult($result);
80
                    }
81
                }
82
            }
83
        }
84
85 4
        return $this->empty === 0;
86
    }
87
88
    /**
89
     * Get result based on comma separated parsed env() parameters
90
     *
91
     * @param array $values
92
     * @return object
93
     */
94 4
    private function getResult(array $values)
95
    {
96
        return (object)[
97 4
            'envVar' => $values[0],
98 4
            'hasValue' => (bool)env($values[0]),
99 4
            'hasDefault' => isset($values[1]),
100
        ];
101
    }
102
103
    /**
104
     * Store result and optional runtime output
105
     *
106
     * @param $result
107
     */
108 4
    private function storeResult($result)
109
    {
110 4
        if (! $result->hasValue && ! $result->hasDefault) {
111 4
            $this->empty++;
112
        }
113 4
    }
114
115 4
    private function recursiveDirSearch(string $folder, string $pattern): array
116
    {
117 4
        if (! file_exists($folder)) {
118
            return [];
119
        }
120
121 4
        $files = new RegexIterator(
122 4
            new RecursiveIteratorIterator(
123 4
                new RecursiveDirectoryIterator($folder)
124
            ),
125 4
            $pattern, RegexIterator::GET_MATCH
126
        );
127
128 4
        $list = [];
129
130 4
        foreach($files as $file) {
131 4
            $list = array_merge($list, $file);
132
        }
133
134 4
        return $list;
135
    }
136
}
137