Issues (98)

src/Checks/Code/ModulesUsed.php (2 issues)

1
<?php
2
3
namespace Sunnysideup\HealthCheckProvider\Checks\Code;
4
5
use SilverStripe\Control\Director;
6
use Sunnysideup\HealthCheckProvider\Checks\HealthCheckItemRunner;
7
8
class ModulesUsed extends HealthCheckItemRunner
9
{
10
    private static $fields_required_from_installation_file = [
0 ignored issues
show
The private property $fields_required_from_installation_file is not used, and could be removed.
Loading history...
11
        'name',
12
        'version',
13
    ];
14
15
    public function getCalculatedAnswer(): array
16
    {
17
        //get data from
18
        $json = $this->getDataFromComposerFile();
19
        $installed = $this->getDataFromInstalledFile();
20
        return [
21
            'Require' => $json['require'] ?? [],
22
            'Installed' => $installed ?? [],
23
        ];
24
    }
25
26
    protected function getDataFromComposerFile(): array
27
    {
28
        return $this->getDataFromFile('composer.json');
29
    }
30
31
    protected function getDataFromInstalledFile(): array
32
    {
33
        $array = $this->getDataFromFile('vendor/composer/installed.json');
34
        $keysRequired = $this->Config()->get('fields_required_from_installation_file');
35
        foreach ($array as $key => $item) {
36
            foreach (array_keys($item) as $innerKey) {
37
                if (! in_array($innerKey, $keysRequired, true)) {
38
                    unset($array[$key][$innerKey]);
39
                }
40
            }
41
        }
42
        return $array;
43
    }
44
45
    protected function getDataFromFile(string $relativeFilePath): array
46
    {
47
        $json = '{}';
0 ignored issues
show
The assignment to $json is dead and can be removed.
Loading history...
48
        $path = Director::baseFolder() . '/' . $relativeFilePath;
49
        if (file_exists($path)) {
50
            $json = file_get_contents($path);
51
            if ($json) {
52
                $array = @json_decode($json, true);
53
                if ($array && is_array($array)) {
54
                    return $array;
55
                }
56
            }
57
        }
58
        return [];
59
    }
60
61
    protected function nameSpacesRequired(): array
62
    {
63
        return [
64
            'BringYourOwnIdeas\\Maintenance',
65
        ];
66
    }
67
}
68