Completed
Pull Request — master (#60)
by
unknown
177:15
created

PhpExtensionsAreInstalled   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 82
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

5 Methods

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