Skip to content

Commit 899b7db

Browse files
authored
Optimized code for reading extra data from composer.lock. (#7223)
1 parent f401e77 commit 899b7db

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

Diff for: src/Composer.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static function discoverLockFile(): string
8989
return '';
9090
}
9191

92-
public static function getMergedExtra(?string $key = null)
92+
public static function getMergedExtra(?string $key = null): array
9393
{
9494
if (! self::$extra) {
9595
self::getLockContent();
@@ -101,16 +101,16 @@ public static function getMergedExtra(?string $key = null)
101101

102102
$extra = [];
103103

104-
foreach (self::$extra as $project => $config) {
105-
foreach ($config ?? [] as $configKey => $item) {
106-
if ($key === $configKey && $item) {
107-
foreach ($item as $k => $v) {
108-
if (is_array($v)) {
109-
$extra[$k] = array_merge($extra[$k] ?? [], $v);
110-
} else {
111-
$extra[$k][] = $v;
112-
}
113-
}
104+
foreach (self::$extra as $config) {
105+
if (! isset($config[$key])) {
106+
continue;
107+
}
108+
109+
foreach ($config[$key] as $k => $v) {
110+
if (is_array($v)) {
111+
$extra[$k] = array_merge($extra[$k] ?? [], $v);
112+
} else {
113+
$extra[$k][] = $v;
114114
}
115115
}
116116
}

Diff for: tests/ComposerTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,10 @@ public function testHasPackage()
3636
$this->assertTrue(Composer::hasPackage('hyperf/framework'));
3737
$this->assertFalse(Composer::hasPackage('composer/unknown'));
3838
}
39+
40+
public function testGetMergedExtra()
41+
{
42+
$providers = Composer::getMergedExtra('hyperf')['config'] ?? [];
43+
$this->assertNotEmpty($providers);
44+
}
3945
}

0 commit comments

Comments
 (0)