Completed
Pull Request — master (#6)
by
unknown
13:58
created

Dependencies::buildRegex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 10
ccs 2
cts 2
cp 1
crap 2
rs 10
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
    public function exportToArray(): array
13
    {
14
        $data = $this->getEvent()->getComposer()->getLocker()->getLockData();
15 2
16
        $packagesData = array_merge(
17 2
            $data['packages'],
18
            $data['packages-dev']
19 2
        );
20 2
21 2
        $packageDeps = [];
22
23
        foreach ($packagesData as $package) {
24 2
            $package = (new ArrayLoader())->load($package);
25
            $packageName = $package->getName();
26 2
            $packageDeps += [$packageName => []];
27 2
            $this->getDependenciesOf($packageDeps[$package->getName()], $package);
28 2
29 2
            $packageDeps[$packageName] = array_values($packageDeps[$packageName]);
30 2
        }
31
32 2
        $regex = $this->buildRegex($packageDeps);
33
34
        return compact('packageDeps', 'regex');
35 2
    }
36
37
    protected function getDependenciesOf(array &$carry, PackageInterface $package): void
38
    {
39
        foreach (array_keys($package->getRequires()) as $key) {
40
            if ('php' === $key) {
41
                continue;
42 2
            }
43
44 2
            if (0 === mb_strpos((string) $key, 'ext-')) {
45 2
                continue;
46 1
            }
47
48
            $carry += [$key => $key];
49 2
        }
50 1
    }
51
52
    private function buildRegex($packages): array
53 2
    {
54
        $groups = [];
55 2
56
        foreach ($packages as $package => $dependencies) {
57
            [$prefix, $bundle] = explode('/', $package);
58
            $groups[sprintf('(?i:%s)(?|', $prefix)][] = sprintf('/?(?i:%s) (*MARK:%s)|', str_replace('-', '-?', $bundle), $package);
59
        }
60
61
        return $groups;
62
    }
63
}
64