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

Dependencies::exportToArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 21
ccs 13
cts 13
cp 1
crap 2
rs 9.8666
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