Dependencies   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 65
ccs 31
cts 31
cp 1
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A exportToArray() 0 23 2
A getDependenciesOf() 0 12 4
A buildRegex() 0 14 2
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
final class Dependencies extends Exporter
11
{
12 2
    public function exportToArray(): array
13
    {
14 2
        $data = $this->getEvent()->getComposer()->getLocker()->getLockData();
15
16 2
        $packagesData = array_merge(
17 2
            $data['packages'],
18 2
            $data['packages-dev']
19
        );
20
21 2
        $packageDeps = [];
22
23 2
        foreach ($packagesData as $package) {
24 2
            $package = (new ArrayLoader())->load($package);
25 2
            $packageName = $package->getName();
26 2
            $packageDeps += [$packageName => []];
27 2
            $this->getDependenciesOf($packageDeps[$package->getName()], $package);
28
29 2
            $packageDeps[$packageName] = array_values($packageDeps[$packageName]);
30
        }
31
32 2
        return [
0 ignored issues
show
introduced by
The expression return array('packageDep...ildRegex($packageDeps)) returns an array which contains values of type array which are incompatible with the return type string mandated by drupol\ComposerPackages\...erface::exportToArray().
Loading history...
33
            'packageDeps' => $packageDeps,
34 2
            'regex' => $this->buildRegex($packageDeps),
35
        ];
36
    }
37 2
38
    /**
39 2
     * @param array<string> $carry
40 2
     */
41 1
    protected function getDependenciesOf(array &$carry, PackageInterface $package): void
42
    {
43
        foreach (array_keys($package->getRequires()) as $key) {
44 2
            if ('php' === $key) {
45 1
                continue;
46
            }
47
48 2
            if (0 === strpos((string) $key, 'ext-')) {
49
                continue;
50 2
            }
51
52 2
            $carry += [$key => $key];
53
        }
54 2
    }
55
56 2
    /**
57 2
     * @param array<string, array> $packages
58 2
     *
59 2
     * @return array<string, array<int, string>>
60 2
     */
61 2
    private function buildRegex(array $packages): array
62
    {
63
        $groups = [];
64
65 2
        foreach ($packages as $package => $dependencies) {
66
            [$prefix, $bundle] = explode('/', $package);
67
            $groups[sprintf('(?i:%s)(?|', $prefix)][] = sprintf(
68
                '/?(?i:%s) (*MARK:%s)|',
69
                str_replace('-', '-?', $bundle),
70
                $package
71
            );
72
        }
73
74
        return $groups;
75
    }
76
}
77