PhpExtensionsAreInstalled   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 81
ccs 0
cts 41
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A name() 0 4 1
A message() 0 6 1
A check() 0 13 2
A getExtensionsRequiredInComposerFile() 0 15 3
1
<?php
2
3
namespace BeyondCode\SelfDiagnosis\Checks;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Str;
9
10
class PhpExtensionsAreInstalled implements Check
11
{
12
13
    const EXT = 'ext-';
14
15
    /** @var Filesystem */
16
    private $filesystem;
17
18
    public function __construct(Filesystem $filesystem)
19
    {
20
        $this->filesystem = $filesystem;
21
    }
22
23
    /** @var Collection */
24
    private $extensions;
25
26
    /**
27
     * The name of the check.
28
     *
29
     * @param array $config
30
     * @return string
31
     */
32
    public function name(array $config): string
33
    {
34
        return trans('self-diagnosis::checks.php_extensions_are_installed.name');
35
    }
36
37
    /**
38
     * The error message to display in case the check does not pass.
39
     *
40
     * @param array $config
41
     * @return string
42
     */
43
    public function message(array $config): string
44
    {
45
        return trans('self-diagnosis::checks.php_extensions_are_installed.message', [
46
            'extensions' => $this->extensions->implode(PHP_EOL),
47
        ]);
48
    }
49
50
    /**
51
     * Perform the actual verification of this check.
52
     *
53
     * @param array $config
54
     * @return bool
55
     */
56
    public function check(array $config): bool
57
    {
58
        $this->extensions = Collection::make(Arr::get($config, 'extensions', []));
59
        if (Arr::get($config, 'include_composer_extensions', false)) {
60
            $this->extensions = $this->extensions->merge($this->getExtensionsRequiredInComposerFile());
61
            $this->extensions = $this->extensions->unique();
62
        }
63
        $this->extensions = $this->extensions->reject(function ($ext) {
64
            return extension_loaded($ext);
65
        });
66
67
        return $this->extensions->isEmpty();
68
    }
69
70
    /**
71
     * @return array
72
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
73
     */
74
    public function getExtensionsRequiredInComposerFile()
75
    {
76
        $installedPackages = json_decode($this->filesystem->get(base_path('vendor/composer/installed.json')), true);
77
78
        $extensions = [];
79
        foreach ($installedPackages as $installedPackage) {
80
            $filtered = Arr::where(array_keys(Arr::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...
81
                return Str::startsWith($value, self::EXT);
82
            });
83
            foreach ($filtered as $extension) {
84
                $extensions[] = Str::replaceFirst(self::EXT, '', $extension);
85
            }
86
        }
87
        return array_unique($extensions);
88
    }
89
90
}
91