Skip to content

Commit 1baa115

Browse files
teklakctnorberttech
authored andcommitted
Correctly finds not existing keys when checking Array matcher pattern (#176)
* fixes #144 * remove unnecessary if statement * catch multiple exception types in a single catch statement
1 parent 6f46fe0 commit 1baa115

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

src/Matcher/ArrayMatcher.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,22 @@ private function iterateMatch(array $values, array $patterns, string $parentPath
129129

130130
private function isPatternValid(array $pattern, array $values, string $parentPath) : bool
131131
{
132-
if (\is_array($pattern)) {
133-
$skipPattern = static::UNBOUNDED_PATTERN;
132+
$skipPattern = static::UNBOUNDED_PATTERN;
134133

135-
$pattern = \array_filter(
136-
$pattern,
137-
function ($item) use ($skipPattern) {
138-
return $item !== $skipPattern;
139-
}
140-
);
134+
$pattern = \array_filter(
135+
$pattern,
136+
function ($item) use ($skipPattern) {
137+
return $item !== $skipPattern;
138+
}
139+
);
141140

142-
$notExistingKeys = $this->findNotExistingKeys($pattern, $values);
143-
if (\count($notExistingKeys) > 0) {
144-
$keyNames = \array_keys($notExistingKeys);
145-
$path = $this->formatFullPath($parentPath, $this->formatAccessPath($keyNames[0]));
146-
$this->setMissingElementInError('value', $path);
141+
$notExistingKeys = $this->findNotExistingKeys($pattern, $values);
142+
if (\count($notExistingKeys) > 0) {
143+
$keyNames = \array_keys($notExistingKeys);
144+
$path = $this->formatFullPath($parentPath, $this->formatAccessPath($keyNames[0]));
145+
$this->setMissingElementInError('value', $path);
147146

148-
return false;
149-
}
147+
return false;
150148
}
151149

152150
return true;
@@ -162,14 +160,12 @@ private function findNotExistingKeys(array $patterns, array $values) : array
162160

163161
return \array_filter($notExistingKeys, function ($pattern) use ($values) {
164162
if (\is_array($pattern)) {
165-
return !$this->match($values, $pattern);
163+
return empty($pattern) || !$this->match($values, $pattern);
166164
}
167165

168166
try {
169167
$typePattern = $this->parser->parse($pattern);
170-
} catch (Exception $e) {
171-
return true;
172-
} catch (\Throwable $t) {
168+
} catch (Exception | \Throwable $e) {
173169
return true;
174170
}
175171

tests/ExpandersTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public static function expanderExamples()
5454
['', '@[email protected]()', true],
5555
[['foo', 'bar'], '@[email protected]("bar")', true],
5656
[[], '@[email protected]()', true],
57+
[[], ['@string@'], false],
58+
[[], ['@[email protected]()'], true],
5759
[['foo'], '@[email protected]()', false],
5860
[[1, 2, 3], '@[email protected](3)', true],
5961
[[1, 2, 3], '@[email protected](4)', false],
@@ -72,6 +74,8 @@ public static function expanderExamples()
7274
[[], ['unexistent_key' => '@[email protected]()'], true],
7375
[[], ['unexistent_key' => '@[email protected]()'], true],
7476
[[], ['unexistent_key' => '@[email protected]()'], true],
77+
[[], ['unexistent_key' => '@[email protected]()', 'unexistent_second_key' => '@[email protected]()'], true],
78+
[[], ['unexistent_key' => '@[email protected]()', 'unexistent_second_key' => '@string@'], false],
7579
[['Norbert', 'Michał'], '@[email protected]("@string@")', true],
7680
['127.0.0.1', '@[email protected]()', true],
7781
['2001:0db8:0000:42a1:0000:0000:ab1c:0001', '@[email protected]()', true],

tests/Matcher/ArrayMatcherTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ public static function positiveMatchData()
203203
[$simpleArr, $simpleArrPatternWithUniversalKey],
204204
[$simpleArr, $simpleArrPatternWithUniversalKeyAndStringValue],
205205
[[], []],
206+
[[], ['@[email protected]()']],
206207
[['foo' => null], ['foo' => null]],
207208
[['foo' => null], ['foo' => '@null@']],
208209
[['key' => 'val'], ['key' => 'val']],
@@ -287,7 +288,9 @@ public static function negativeMatchData()
287288
[['key' => 'val'], ['key' => 'val2']],
288289
[[1], [2]],
289290
[['foo', 1, 3], ['foo', 2, 3]],
291+
[[], ['key' => []]],
290292
[[], ['foo' => 'bar']],
293+
[[], ['foo' => ['bar' => []]]],
291294
'unbound array should match one or none elements' => [
292295
[
293296
'users' => [

0 commit comments

Comments
 (0)