Completed
Pull Request — master (#6)
by
unknown
23:53 queued 08:54
created

Versions::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 Versions 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
        $packageVersions = array_map(
26
            static function (array $data) {
27
                return $data['version'];
28 2
            },
29
            $packagesData
30 2
        );
31 2
32 2
        if (false !== $versions = array_combine($packageNames, $packageVersions)) {
33
            ksort($versions);
34
35 2
            $regex = $this->buildRegex($versions);
36
37 2
            return compact('packageNames', 'regex');
38
        }
39
40
        return [];
41
    }
42
43
    private function buildRegex($versions): array
44
    {
45
        $groups = [];
46
47
        foreach ($versions as $package => $version) {
48
            [$prefix, $bundle] = explode('/', $package);
49
            $groups[sprintf('(?i:%s)(?|', $prefix)][] = sprintf('/?(?i:%s) (*MARK:%s)|', $bundle, $version);
50
        }
51
52
        return $groups;
53
    }
54
}
55