| 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
|
|||
| 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 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: