ScanForPhpExtensions::getTokensByClassesPathList()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MagentoHackathon\Service;
4
5
use MagentoHackathon\Api\TokenizerInterface;
6
use MagentoHackathon\StringTokenizer;
7
8
class ScanForPhpExtensions
9
{
10
    const EXT = 'ext-';
11
12
    /**
13
     * @var array|null
14
     */
15
    private $phpCheckExtensions;
16
17
    /**
18
     * @var arrayfl
0 ignored issues
show
Documentation Bug introduced by
The doc comment arrayfl at position 0 could not be parsed: Unknown type name 'arrayfl' at position 0 in arrayfl.
Loading history...
19
     */
20
    private $registeredExtensions;
21
    /**
22
     * @var StringTokenizer
23
     */
24
    private $tokenizer;
25
26
    /**
27
     * ScanForPhpExtensions constructor.
28
     * @param TokenizerInterface $tokenizer
29
     * @param array $registeredPhpExtensions
30
     * @param null $phpCheckExtensions
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $phpCheckExtensions is correct as it would always require null to be passed?
Loading history...
31
     */
32
    public function __construct(
33
        TokenizerInterface $tokenizer,
34
        $registeredExtensions = [],
35
        $phpCheckExtensions = null
36
    ) {
37
        $this->tokenizer = $tokenizer;
0 ignored issues
show
Documentation Bug introduced by
$tokenizer is of type MagentoHackathon\Api\TokenizerInterface, but the property $tokenizer was declared to be of type MagentoHackathon\StringTokenizer. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
38
        $this->registeredExtensions = $registeredExtensions;
39
40
        if ($phpCheckExtensions === null) {
0 ignored issues
show
introduced by
The condition $phpCheckExtensions === null is always true.
Loading history...
41
            $phpCheckExtensions = ['json', 'xml', 'pcre', 'gd', 'bcmath'];
42
        }
43
44
        $this->phpCheckExtensions = $phpCheckExtensions;
45
    }
46
47
    /**
48
     * @param array $classesPathList
49
     * @return array
50
     */
51
    public function execute(array $classesPathList): array
52
    {
53
        $results = [];
54
        $tokens = $this->getTokensByClassesPathList($classesPathList);
55
        $registeredPhpExtensions = $this->getRegisteredPhpExtensions();
56
57
        foreach ($this->phpCheckExtensions as $phpExtension) {
58
            if (array_key_exists($phpExtension, $registeredPhpExtensions)) {
59
                continue;
60
            }
61
62
            foreach (get_extension_funcs($phpExtension) as $phpExtensionFunction) {
63
                if (!in_array($phpExtensionFunction, $tokens, false)) {
64
                    continue;
65
                }
66
67
                $results[] = sprintf(
68
                    'Function "%s" requires PHP extension "ext-%s"',
69
                    $phpExtensionFunction,
70
                    $phpExtension
71
                );
72
            }
73
        }
74
75
        return $results;
76
    }
77
78
    /**
79
     * @param array $classesPathList
80
     * @return array
81
     */
82
    private function getTokensByClassesPathList(array $classesPathList): array
83
    {
84
        $stringTokens = [[]];
85
86
        foreach ($classesPathList as $classPath) {
87
            $stringTokens[] = $this->tokenizer->execute($classPath);
88
        }
89
90
        return array_unique(array_merge(...$stringTokens));
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    private function getRegisteredPhpExtensions(): array
97
    {
98
        $registeredPhpExtensions = [];
99
100
        foreach ($this->registeredExtensions as $registeredExtension => $value) {
101
            if (strpos(substr($registeredExtension, 0, 4), self::EXT) === false) {
102
                continue;
103
            }
104
105
            $registeredExtension = str_replace(self::EXT, '', $registeredExtension);
106
            $registeredPhpExtensions[$registeredExtension] = '';
107
        }
108
109
        return $registeredPhpExtensions;
110
    }
111
}
112