Packages::exportToArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2.0014

Importance

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