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

EnvVariablesExists::recursiveDirSearch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 10
cts 11
cp 0.9091
rs 9.584
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3.0067
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
                $values = array_filter(
68 4
                    preg_split("#[\n]+#", shell_exec("tr -d '\n' < $file | grep -oP 'env\(\K[^)]+'"))
69
                );
70
71 4
                foreach ($values as $value) {
72 4
                    $result = $this->getResult(
73 4
                        explode(',', str_replace(["'", '"', ' '], '', $value))
74
                    );
75
76 4
                    $this->storeResult($result);
77
                }
78
            }
79
        }
80
81 4
        return $this->empty === 0;
82
    }
83
84
    /**
85
     * Get result based on comma separated parsed env() parameters
86
     *
87
     * @param array $values
88
     * @return object
89
     */
90 4
    private function getResult(array $values)
91
    {
92
        return (object)[
93 4
            'envVar' => $values[0],
94 4
            'hasValue' => (bool)env($values[0]),
95 4
            'hasDefault' => isset($values[1]),
96
        ];
97
    }
98
99
    /**
100
     * Store result and optional runtime output
101
     *
102
     * @param $result
103
     */
104 4
    private function storeResult($result)
105
    {
106 4
        if (! $result->hasValue && ! $result->hasDefault) {
107 4
            $this->empty++;
108
        }
109 4
    }
110
111 4
    private function recursiveDirSearch(string $folder, string $pattern): array
112
    {
113 4
        if (! file_exists($folder)) {
114
            return [];
115
        }
116
117 4
        $files = new RegexIterator(
118 4
            new RecursiveIteratorIterator(
119 4
                new RecursiveDirectoryIterator($folder)
120
            ),
121 4
            $pattern, RegexIterator::GET_MATCH
122
        );
123
124 4
        $list = [];
125
126 4
        foreach($files as $file) {
127 4
            $list = array_merge($list, $file);
128
        }
129
130 4
        return $list;
131
    }
132
}
133