Versions::buildRegex()   A
last analyzed

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
final class Versions 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
        $packageVersions = array_map(
26
            static function (array $data): string {
27 2
                return $data['version'];
28 2
            },
29 2
            $packagesData
30
        );
31
32 2
        if (false === $versions = array_combine($packageNames, $packageVersions)) {
33 2
            return [];
34
        }
35 2
36
        ksort($versions);
37 2
38
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('package_na...>buildRegex($versions)) returns the type array<string,array> which is incompatible with the return type mandated by drupol\ComposerPackages\...erface::exportToArray() of string[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
39
            'package_names' => $packageNames,
40
            'regex' => $this->buildRegex($versions),
41
        ];
42
    }
43 2
44
    /**
45 2
     * @param array<string, string> $versions
46
     *
47 2
     * @return array<string, array<int, string>>
48 2
     */
49 2
    private function buildRegex(array $versions): array
50
    {
51
        $groups = [];
52 2
53
        foreach ($versions as $package => $version) {
54
            [$prefix, $bundle] = explode('/', $package);
55
            $groups[sprintf('(?i:%s)(?|', $prefix)][] = sprintf('/?(?i:%s) (*MARK:%s)|', $bundle, $version);
56
        }
57
58
        return $groups;
59
    }
60
}
61