Skip to content

Commit 3f0f7b4

Browse files
authored
Fix #109: Improve the error message by displaying a name of the group where the error occurred when merging
1 parent 4de7db7 commit 3f0f7b4

3 files changed

Lines changed: 20 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 1.1.1 under development
44

5-
- no changes in this release.
5+
- Enh #110: Improve the error message by displaying a name of the group where the error occurred when merging (devanych)
66

77
## 1.1.0 December 31, 2021
88

src/Merger.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private function performMerge(
142142
);
143143

144144
if ($file !== null) {
145-
$this->throwDuplicateKeyErrorException($fullKeyPath, [$file, $context->file()]);
145+
$this->throwDuplicateKeyErrorException($context->group(), $fullKeyPath, [$file, $context->file()]);
146146
}
147147
}
148148

@@ -213,7 +213,7 @@ private function prepareArrayForReverse(
213213
);
214214

215215
if ($file !== null) {
216-
$this->throwDuplicateKeyErrorException($recursiveKeyPath, [$file, $context->file()]);
216+
$this->throwDuplicateKeyErrorException($context->group(), $recursiveKeyPath, [$file, $context->file()]);
217217
}
218218

219219
/** @var mixed */
@@ -250,13 +250,17 @@ private function setValue(Context $context, array $keyPath, array &$array, strin
250250
/**
251251
* Generates a duplicate key error message and throws an exception.
252252
*
253+
* @param string $currentGroupName The name of the group that the error occurred when merging.
253254
* @param string[] $recursiveKeyPath The key path for recursive merging of arrays in configuration files.
254255
* @param string[] $absoluteFilePaths The absolute paths to the files in which duplicates are found.
255256
*
256257
* @throws ErrorException With a duplicate key error message.
257258
*/
258-
private function throwDuplicateKeyErrorException(array $recursiveKeyPath, array $absoluteFilePaths): void
259-
{
259+
private function throwDuplicateKeyErrorException(
260+
string $currentGroupName,
261+
array $recursiveKeyPath,
262+
array $absoluteFilePaths
263+
): void {
260264
$filePaths = array_map(
261265
fn (string $filePath) => ' - ' . $this->paths->relative($filePath),
262266
$absoluteFilePaths,
@@ -269,8 +273,9 @@ private function throwDuplicateKeyErrorException(array $recursiveKeyPath, array
269273
});
270274

271275
$message = sprintf(
272-
"Duplicate key \"%s\" in configs:\n%s",
276+
"Duplicate key \"%s\" in the following configs while building \"%s\" group:\n%s",
273277
implode(' => ', $recursiveKeyPath),
278+
$currentGroupName,
274279
implode("\n", $filePaths),
275280
);
276281

tests/ConfigTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public function testDuplicateRootKeysErrorMessage(): void
344344

345345
$this->expectException(ErrorException::class);
346346
$this->expectErrorMessage(
347-
'Duplicate key "age" in configs:' . "\n" .
347+
'Duplicate key "age" in the following configs while building "params" group:' . "\n" .
348348
' - config/params/a.php' . "\n" .
349349
' - config/params/b.php'
350350
);
@@ -362,7 +362,7 @@ public function testDuplicateRootKeysErrorMessageWithReverseMerge(): void
362362

363363
$this->expectException(ErrorException::class);
364364
$this->expectErrorMessage(
365-
'Duplicate key "age" in configs:' . "\n" .
365+
'Duplicate key "age" in the following configs while building "params" group:' . "\n" .
366366
' - config/params/a.php' . "\n" .
367367
' - config/params/b.php'
368368
);
@@ -378,7 +378,7 @@ public function testDuplicateEnvironmentKeysErrorMessage(): void
378378

379379
$this->expectException(ErrorException::class);
380380
$this->expectErrorMessage(
381-
'Duplicate key "age" in configs:' . "\n" .
381+
'Duplicate key "age" in the following configs while building "params" group:' . "\n" .
382382
' - config/environment/params/a.php' . "\n" .
383383
' - config/environment/params/b.php'
384384
);
@@ -396,7 +396,7 @@ public function testDuplicateEnvironmentKeysErrorMessageWithReverseMerge(): void
396396

397397
$this->expectException(ErrorException::class);
398398
$this->expectErrorMessage(
399-
'Duplicate key "age" in configs:' . "\n" .
399+
'Duplicate key "age" in the following configs while building "params" group:' . "\n" .
400400
' - config/environment/params/a.php' . "\n" .
401401
' - config/environment/params/b.php'
402402
);
@@ -409,7 +409,7 @@ public function testDuplicateVendorKeysErrorMessage(): void
409409

410410
$this->expectException(ErrorException::class);
411411
$this->expectErrorMessage(
412-
'Duplicate key "age" in configs:' . "\n" .
412+
'Duplicate key "age" in the following configs while building "params" group:' . "\n" .
413413
' - vendor/package/a/params.php' . "\n" .
414414
' - vendor/package/b/params.php'
415415
);
@@ -427,7 +427,7 @@ public function testDuplicateVendorKeysErrorMessageWithReverseMerge(): void
427427

428428
$this->expectException(ErrorException::class);
429429
$this->expectErrorMessage(
430-
'Duplicate key "age" in configs:' . "\n" .
430+
'Duplicate key "age" in the following configs while building "params" group:' . "\n" .
431431
' - vendor/package/a/params.php' . "\n" .
432432
' - vendor/package/b/params.php'
433433
);
@@ -444,7 +444,9 @@ public function testDuplicateKeysWithRecursiveKeyPathErrorMessage(): void
444444
);
445445

446446
$this->expectException(ErrorException::class);
447-
$this->expectErrorMessageMatches('~^Duplicate key "name => first-name" in~');
447+
$this->expectErrorMessageMatches(
448+
'~^Duplicate key "name => first-name" in the following configs while building "params" group~',
449+
);
448450
$config->get('params');
449451
}
450452

0 commit comments

Comments
 (0)