Skip to content

Commit 9b260a9

Browse files
authored
Optimized code for reading package data from composer.lock. (#7258)
1 parent 899b7db commit 9b260a9

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/Composer.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,31 @@ class Composer
3737
*/
3838
public static function getLockContent(): Collection
3939
{
40-
if (! self::$content) {
41-
if (! $path = self::discoverLockFile()) {
42-
throw new RuntimeException('composer.lock not found.');
40+
if (self::$content) {
41+
return self::$content;
42+
}
43+
44+
if (! $path = self::discoverLockFile()) {
45+
throw new RuntimeException('composer.lock not found.');
46+
}
47+
48+
self::$content = collect(json_decode(file_get_contents($path), true));
49+
$packages = self::$content->offsetGet('packages') ?? [];
50+
$packagesDev = self::$content->offsetGet('packages-dev') ?? [];
51+
52+
foreach (array_merge($packages, $packagesDev) as $package) {
53+
$packageName = $package['name'] ?? '';
54+
if (! $packageName) {
55+
continue;
4356
}
4457

45-
self::$content = collect(json_decode(file_get_contents($path), true));
46-
$packages = self::$content->offsetGet('packages') ?? [];
47-
$packagesDev = self::$content->offsetGet('packages-dev') ?? [];
48-
49-
foreach (array_merge($packages, $packagesDev) as $package) {
50-
$packageName = '';
51-
foreach ($package ?? [] as $key => $value) {
52-
if ($key === 'name') {
53-
$packageName = $value;
54-
continue;
55-
}
56-
57-
$packageName && match ($key) {
58-
'extra' => self::$extra[$packageName] = $value,
59-
'scripts' => self::$scripts[$packageName] = $value,
60-
'version' => self::$versions[$packageName] = $value,
61-
default => null,
62-
};
63-
}
58+
foreach ($package as $key => $value) {
59+
match ($key) {
60+
'extra' => self::$extra[$packageName] = $value,
61+
'scripts' => self::$scripts[$packageName] = $value,
62+
'version' => self::$versions[$packageName] = $value,
63+
default => null,
64+
};
6465
}
6566
}
6667

0 commit comments

Comments
 (0)