Skip to content

Commit 588d7c9

Browse files
raing3norberttech
authored andcommitted
Fixed issue where unbound key matching would not validate keys in pattern that were missing in the value. (#193)
1 parent 267b67a commit 588d7c9

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/Matcher/ArrayMatcher.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@ function ($item) use ($skipPattern) {
152152

153153
private function findNotExistingKeys(array $patterns, array $values) : array
154154
{
155-
if (isset($patterns[self::UNIVERSAL_KEY])) {
156-
return [];
157-
}
158-
159155
$notExistingKeys = \array_diff_key($patterns, $values);
160156

161-
return \array_filter($notExistingKeys, function ($pattern) use ($values) {
157+
return \array_filter($notExistingKeys, function ($pattern, $key) use ($values) {
158+
if ($key === self::UNIVERSAL_KEY) {
159+
return false;
160+
}
161+
162162
if (\is_array($pattern)) {
163163
return empty($pattern) || !$this->match($values, $pattern);
164164
}
@@ -170,7 +170,7 @@ private function findNotExistingKeys(array $patterns, array $values) : array
170170
}
171171

172172
return !$typePattern->hasExpander('optional');
173-
});
173+
}, ARRAY_FILTER_USE_BOTH);
174174
}
175175

176176
private function valueMatchPattern($value, $pattern) : bool

tests/Matcher/ArrayMatcherTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ public static function negativeMatchData()
294294
[[], ['key' => []]],
295295
[[], ['foo' => 'bar']],
296296
[[], ['foo' => ['bar' => []]]],
297+
[['key' => 'val', 'key2' => 'val2'], ['not key' => 'val', '@*@' => '@*@']],
297298
'unbound array should match one or none elements' => [
298299
[
299300
'users' => [

0 commit comments

Comments
 (0)