LocateDefinedSymbolsFromExtensions   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
eloc 15
c 1
b 0
f 1
dl 0
loc 34
ccs 13
cts 13
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 18 3
1
<?php
2
3
namespace ComposerRequireChecker\DefinedSymbolsLocator;
4
5
use ComposerRequireChecker\Exception\UnknownExtensionException;
6
7
class LocateDefinedSymbolsFromExtensions
8
{
9
    /**
10
     * @const string composer does some interpolation on the package name of extensions: str_replace(' ', '-', $name)
11
     *        which means that composer.json must have 'ext-zend-opcache' instead of the correct / exact package name
12
     *        which is 'ext-Zend Opcache'. So the ALTERNATIVES allows us to look up the correct name, case sensitive
13
     *        without the '-'.
14
     * @see https://github.com/maglnet/ComposerRequireChecker/issues/99
15
     */
16
    private const ALTERNATIVES = ['zend-opcache' => 'Zend Opcache'];
17
18
    /**
19
     * @param string[] $extensionNames
20
     * @return string[]
21
     * @throws UnknownExtensionException if the extension cannot be found
22
     */
23 18
    public function __invoke(array $extensionNames): array
24
    {
25 18
        $definedSymbols = [];
26 18
        foreach ($extensionNames as $extensionName) {
27 17
            $extensionName = self::ALTERNATIVES[$extensionName] ?? $extensionName;
28
            try {
29 17
                $extensionReflection = new \ReflectionExtension($extensionName);
30 16
                $definedSymbols = array_merge(
31 16
                    $definedSymbols,
32 16
                    array_keys($extensionReflection->getConstants()),
33 16
                    array_keys($extensionReflection->getFunctions()),
34 16
                    $extensionReflection->getClassNames()
35
                );
36 1
            } catch (\Exception $e) {
37 1
                throw new UnknownExtensionException($e->getMessage());
38
            }
39
        }
40 17
        return $definedSymbols;
41
    }
42
}
43