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

Directories::buildRegex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 14
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
use Composer\Package\Loader\ArrayLoader;
8
9
class Directories extends Exporter
10
{
11
    public function exportToArray(): array
12
    {
13
        $data = $this->getEvent()->getComposer()->getLocker()->getLockData();
14 2
15
        $packagesData = \array_merge(
16 2
            $data['packages'],
17
            $data['packages-dev']
18 2
        );
19 2
20 2
        $directories = [];
21
22
        foreach ($packagesData as $package) {
23 2
            $package = (new ArrayLoader())->load($package);
24 2
25
            $directories[$package->getName()] = $this
26 2
                ->getEvent()
27 2
                ->getComposer()
28 2
                ->getInstallationManager()
29 2
                ->getInstallPath($package);
30 2
        }
31
32
        $regex = $this->buildRegex($directories);
33 2
34
        return \compact('directories', 'regex');
35
    }
36
37
    private function buildRegex($packages): array
38
    {
39
        $groups = [];
40
41
        foreach ($packages as $package => $directory) {
42
            [$prefix, $bundle] = \explode('/', $package);
43
            $groups[\sprintf('(?i:%s)(?|', $prefix)][] = \sprintf(
44
                '/?(?i:%s) (*MARK:%s)|',
45
                \str_replace('-', '-?', $bundle),
46
                $directory
47
            );
48
        }
49
50
        return $groups;
51
    }
52
}
53