Passed
Push — master ( f2c287...f89b2b )
by Pol
06:01
created

Dependencies   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 44
ccs 20
cts 20
cp 1
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A exportToArray() 0 21 2
A getDependenciesOf() 0 12 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\ComposerPackages\Exporter;
6
7
use Composer\Package\Loader\ArrayLoader;
8
use Composer\Package\PackageInterface;
9
10
class Dependencies extends Exporter
11
{
12
    /**
13
     * @return array
14
     */
15 2
    public function exportToArray(): array
16
    {
17 2
        $data = $this->getEvent()->getComposer()->getLocker()->getLockData();
18
19 2
        $packagesData = \array_merge(
20 2
            $data['packages'],
21 2
            $data['packages-dev']
22
        );
23
24 2
        $packageDeps = [];
25
26 2
        foreach ($packagesData as $package) {
27 2
            $package = (new ArrayLoader())->load($package);
28 2
            $packageName = $package->getName();
29 2
            $packageDeps += [$packageName => []];
30 2
            $this->getDependenciesOf($packageDeps[$package->getName()], $package);
31
32 2
            $packageDeps[$packageName] = \array_values($packageDeps[$packageName]);
33
        }
34
35 2
        return \compact('packageDeps');
36
    }
37
38
    /**
39
     * @param array $carry
40
     * @param \Composer\Package\PackageInterface $package
41
     */
42 2
    protected function getDependenciesOf(array &$carry, PackageInterface $package): void
43
    {
44 2
        foreach (\array_keys($package->getRequires()) as $key) {
45 2
            if ('php' === $key) {
46 1
                continue;
47
            }
48
49 2
            if (0 === \mb_strpos((string) $key, 'ext-')) {
50 1
                continue;
51
            }
52
53 2
            $carry += [$key => $key];
54
        }
55 2
    }
56
}
57