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

Packages::buildRegex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 0
cp 0
crap 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\ComposerPackages\Exporter;
6
7
class Packages extends Exporter
8
{
9
    public function exportToArray(): array
10
    {
11
        $data = $this->getEvent()->getComposer()->getLocker()->getLockData();
12 2
13
        $packagesData = array_merge(
14 2
            $data['packages'],
15
            $data['packages-dev']
16 2
        );
17 2
18 2
        $packageNames = array_map(
19
            static function (array $data) {
20
                return $data['name'];
21 2
            },
22
            $packagesData
23 2
        );
24 2
25 2
        if (false !== $packages = array_combine($packageNames, $packagesData)) {
26
            ksort($packages);
27
28 2
            $regex = $this->buildRegex($packages);
29
30 2
            return compact('packages', 'regex');
31
        }
32
33
        return [];
34
    }
35
36
    private function buildRegex($packages): array
37
    {
38
        $groups = [];
39
40
        foreach ($packages as $package) {
41
            [$prefix, $bundle] = explode('/', $package['name']);
42
            $groups[sprintf('(?i:%s)(?|', $prefix)][] = sprintf('/?(?i:%s) (*MARK:%s)|', str_replace('-', '-?', $bundle), $package['name']);
43
        }
44
45
        return $groups;
46
    }
47
}
48