Directories::buildRegex()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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