Skip to content

Commit 76df466

Browse files
committed
Add dedicated exception
1 parent 2b9c481 commit 76df466

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

lib/Resolver.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,7 @@ public function resolve(array $config): array
118118

119119
if ($diff = array_diff(array_keys($config), $allowedKeys)) {
120120
$this->throwOrLogError(
121-
new InvalidMap(sprintf(
122-
'Key(s) "%s" are not known, known keys: "%s"',
123-
implode('", "', ($diff)),
124-
implode('", "', $allowedKeys)
125-
))
121+
UnknownKeys::fromKeysAndAllowedKeys(array_values($diff), $allowedKeys)
126122
);
127123

128124
$config = $this->removeKeys($config, $diff);
@@ -227,12 +223,12 @@ public function errors(): ResolverErrors
227223
}
228224

229225
/**
230-
* @return array<string>
226+
* @return list<string>
231227
*/
232228
private function resolveAllowedKeys(): array
233229
{
234230
$allowedKeys = array_merge(array_keys($this->defaults), $this->required);
235-
return $allowedKeys;
231+
return array_values($allowedKeys);
236232
}
237233

238234
private function throwOrLogError(InvalidMap $error): void

lib/UnknownKeys.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Phpactor\MapResolver;
4+
5+
6+
class UnknownKeys extends InvalidMap
7+
{
8+
/**
9+
* @param list<string> $allowedKeys
10+
* @param list<string> $keys
11+
*/
12+
public function __construct(string $message, private array $keys, private array $allowedKeys)
13+
{
14+
parent::__construct($message);
15+
}
16+
17+
/**
18+
* @return list<string>
19+
*/
20+
public function additionalKeys(): array
21+
{
22+
return $this->keys;
23+
}
24+
25+
/**
26+
* @return list<string>
27+
*/
28+
public function allowedKeys(): array
29+
{
30+
return $this->allowedKeys;
31+
}
32+
33+
/**
34+
* @param list<string> $allowedKeys
35+
* @param list<string> $diff
36+
*/
37+
public static function fromKeysAndAllowedKeys(array $diff, array $allowedKeys): self
38+
{
39+
return new self(sprintf(
40+
'Key(s) "%s" are not known, known keys: "%s"',
41+
implode('", "', ($diff)),
42+
implode('", "', $allowedKeys),
43+
), $diff, $allowedKeys);
44+
}
45+
}

0 commit comments

Comments
 (0)