Issues (5)

src/Exporter/Packages.php (1 issue)

Severity
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
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