Passed
Pull Request — master (#5)
by Pol
10:22
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
    /**
10
     * @return array
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
        $packageNames = \array_map(
22
            static function (array $data) {
23 2
                return $data['name'];
24 2
            },
25 2
            $packagesData
26
        );
27
28 2
        $packageVersions = \array_map(
29
            static function (array $data) {
30 2
                return $data['version'];
31 2
            },
32 2
            $packagesData
33
        );
34
35 2
        $versions = \array_combine($packageNames, $packageVersions);
36
        $regex = $this->buildRegex($versions);
37 2
38
        return \compact('versions', 'regex');
39
    }
40
41
    private function buildRegex($versions): array
42
    {
43
        asort($versions);
44
45
        foreach($versions as $package => $version) {
46
            [$prefix, $bundle] = explode('/', $package);
47
            $groups[sprintf('(?i:%s)(?|', $prefix)][] = sprintf('/?(?i:%s) (*MARK:%s)|', $bundle, $version);
48
        }
49
50
        return $groups;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $groups seems to be defined by a foreach iteration on line 45. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
51
    }
52
}
53