Completed
Push — master ( 0ef38d...204458 )
by Marcel
12:24 queued 07:08
created

PhpExtensionsAreInstalled::check()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BeyondCode\SelfDiagnosis\Checks;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Collection;
7
8
class PhpExtensionsAreInstalled implements Check
9
{
10
11
    const EXT = 'ext-';
12
13
    /** @var Filesystem */
14
    private $filesystem;
15
16
    public function __construct(Filesystem $filesystem)
17
    {
18
        $this->filesystem = $filesystem;
19
    }
20
21
    /** @var Collection */
22
    private $extensions;
23
24
    /**
25
     * The name of the check.
26
     *
27
     * @return string
28
     */
29
    public function name(): string
30
    {
31
        return 'The required PHP extensions are installed';
32
    }
33
34
    /**
35
     * The error message to display in case the check does not pass.
36
     *
37
     * @return string
38
     */
39
    public function message(): string
40
    {
41
        return 'The following extensions are missing: ' . PHP_EOL . $this->extensions->implode(PHP_EOL);
42
    }
43
44
    /**
45
     * Perform the actual verification of this check.
46
     *
47
     * @return bool
48
     */
49
    public function check(): bool
50
    {
51
        $this->extensions = Collection::make([
52
            'openssl',
53
            'PDO',
54
            'mbstring',
55
            'tokenizer',
56
            'xml',
57
            'ctype',
58
            'json'
59
        ]);
60
        $this->extensions = $this->extensions->merge($this->getExtensionsRequiredInComposerFile());
61
        $this->extensions = $this->extensions->unique();
62
        $this->extensions = $this->extensions->reject(function ($ext) {
63
            return extension_loaded($ext);
64
        });
65
66
        return $this->extensions->isEmpty();
67
    }
68
69
    /**
70
     * @return array
71
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
72
     */
73
    public function getExtensionsRequiredInComposerFile()
74
    {
75
        $installedPackages = json_decode($this->filesystem->get(base_path('vendor/composer/installed.json')), true);
76
77
        $extensions = [];
78
        foreach ($installedPackages as $installedPackage) {
79
            $filtered = array_where(array_keys(array_get($installedPackage, 'require', [])), function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
                return starts_with($value, self::EXT);
81
            });
82
            foreach ($filtered as $extension) {
83
                $extensions[] = str_replace_first(self::EXT, '', $extension);
84
            }
85
        }
86
        return array_unique($extensions);
87
    }
88
89
}
90