Skip to content

Commit 0aae141

Browse files
committed
Ignore all config errors if option given
1 parent a949041 commit 0aae141

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

lib/Resolver.php

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class Resolver
3636
*/
3737
private $ignoreErrors;
3838

39+
/**
40+
* @var InvalidMap[]
41+
*/
42+
private $errors;
43+
3944
public function __construct(bool $ignoreErrors = false)
4045
{
4146
$this->ignoreErrors = $ignoreErrors;
@@ -99,11 +104,15 @@ public function resolve(array $config): array
99104
$allowedKeys = $this->resolveAllowedKeys();
100105

101106
if ($diff = array_diff(array_keys($config), $allowedKeys)) {
102-
throw new InvalidMap(sprintf(
103-
'Key(s) "%s" are not known, known keys: "%s"',
104-
implode('", "', ($diff)),
105-
implode('", "', $allowedKeys)
106-
));
107+
$this->throwOrLogError(
108+
new InvalidMap(sprintf(
109+
'Key(s) "%s" are not known, known keys: "%s"',
110+
implode('", "', ($diff)),
111+
implode('", "', $allowedKeys)
112+
))
113+
);
114+
115+
$config = $this->removeKeys($config, $diff);
107116
}
108117

109118
if ($diff = array_diff(array_keys($this->descriptions), $allowedKeys)) {
@@ -117,10 +126,11 @@ public function resolve(array $config): array
117126
$config = array_merge($this->defaults, $config);
118127

119128
if ($diff = array_diff($this->required, array_keys($config))) {
120-
throw new InvalidMap(sprintf(
129+
$this->throwOrLogError(new InvalidMap(sprintf(
121130
'Key(s) "%s" are required',
122131
implode('", "', $diff)
123-
));
132+
)));
133+
$config = $this->removeKeys($config, $diff);
124134
}
125135

126136
foreach ($config as $key => $value) {
@@ -139,12 +149,12 @@ public function resolve(array $config): array
139149
}
140150

141151
if (false === $valid) {
142-
throw new InvalidMap(sprintf(
152+
$this->throwOrLogError(new InvalidMap(sprintf(
143153
'Type for "%s" expected to be "%s", got "%s"',
144154
$key,
145155
$this->types[$key],
146156
$type
147-
));
157+
)));
148158
}
149159
}
150160
}
@@ -206,4 +216,22 @@ public function definitions(): Definitions
206216

207217
return new Definitions($definitions);
208218
}
219+
220+
private function throwOrLogError(InvalidMap $error): void
221+
{
222+
if (!$this->ignoreErrors) {
223+
throw $error;
224+
}
225+
226+
$this->errors[] = $error;
227+
}
228+
229+
private function removeKeys(array $config, array $keys): array
230+
{
231+
foreach ($keys as $remove) {
232+
unset($config[$remove]);
233+
}
234+
235+
return $config;
236+
}
209237
}

tests/Unit/ResolverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function testIgnoresUnknownKey(): void
4141
'two' => 2,
4242
]);
4343
$result = $resolver->resolve(['three' => 3]);
44-
self::assertEquals([
44+
self::assertEquals($result, [
4545
'one' => 1,
4646
'two' => 2
4747
]);

0 commit comments

Comments
 (0)