Skip to content

Commit 2be8695

Browse files
Revert "Add support for indexed arrays to ArraySubset"
This reverts commit 0a25ecb.
1 parent 44898b7 commit 2be8695

File tree

2 files changed

+12
-89
lines changed

2 files changed

+12
-89
lines changed

src/Framework/Constraint/ArraySubset.php

Lines changed: 8 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -57,105 +57,28 @@ public function __construct(iterable $subset, bool $strict = false)
5757
*/
5858
public function evaluate($other, $description = '', $returnResult = false)
5959
{
60-
// Anonymous function that checks whether the given array is
61-
// associative.
62-
$is_associative = function (array $array): bool {
63-
return \array_reduce(\array_keys($array), function (bool $carry, $key): bool {
64-
return $carry || \is_string($key);
65-
}, false);
66-
};
67-
68-
// Anonymous function that compares the two given values using either
69-
// strict or loose comparisons.
70-
$strict = $this->strict;
71-
$compare = function ($first, $second) use ($strict): bool {
72-
return $strict ? $first === $second : $first == $second;
73-
};
74-
75-
// Anonymous function that sorts the given multidimensional array.
76-
$deep_sort = function (array &$array) use (&$deep_sort, $is_associative): void {
77-
foreach ($array as &$value) {
78-
if (\is_array($value)) {
79-
$deep_sort($value);
80-
}
81-
}
82-
83-
if ($is_associative($array)) {
84-
\ksort($array);
85-
} else {
86-
\sort($array);
87-
}
88-
};
89-
90-
$array_intersect_recursive = function (array $array, array $subset) use (&$array_intersect_recursive, $is_associative, $compare): array {
91-
$intersect = [];
92-
93-
if ($is_associative($subset)) {
94-
// If the subset is an associative array, get the intersection
95-
// while preserving the keys.
96-
foreach ($subset as $key => $subset_value) {
97-
if (\array_key_exists($key, $array)) {
98-
$array_value = $array[$key];
99-
100-
if (\is_array($subset_value) && \is_array($array_value)) {
101-
$intersect[$key] = $array_intersect_recursive($array_value, $subset_value);
102-
} elseif ($compare($subset_value, $array_value)) {
103-
$intersect[$key] = $array_value;
104-
}
105-
}
106-
}
107-
} else {
108-
// If the subset is an indexed array, loop over all entries in
109-
// the haystack and check if they match the ones in the subset.
110-
foreach ($array as $array_value) {
111-
if (\is_array($array_value)) {
112-
foreach ($subset as $key => $subset_value) {
113-
if (\is_array($subset_value)) {
114-
$recursed = $array_intersect_recursive($array_value, $subset_value);
115-
116-
if (!empty($recursed)) {
117-
$intersect[$key] = $recursed;
118-
}
119-
}
120-
}
121-
} else {
122-
foreach ($subset as $key => $subset_value) {
123-
if (!\is_array($subset_value) && $compare(
124-
$subset_value,
125-
$array_value
126-
)) {
127-
$intersect[$key] = $array_value;
128-
129-
break;
130-
}
131-
}
132-
}
133-
}
134-
}
135-
136-
return $intersect;
137-
};
138-
13960
//type cast $other & $this->subset as an array to allow
14061
//support in standard array functions.
14162
$other = $this->toArray($other);
14263
$this->subset = $this->toArray($this->subset);
14364

144-
$intersect = $array_intersect_recursive($other, $this->subset);
145-
$deep_sort($intersect);
146-
$deep_sort($this->subset);
65+
$patched = \array_replace_recursive($other, $this->subset);
14766

148-
$result = $compare($intersect, $this->subset);
67+
if ($this->strict) {
68+
$result = $other === $patched;
69+
} else {
70+
$result = $other == $patched;
71+
}
14972

15073
if ($returnResult) {
15174
return $result;
15275
}
15376

15477
if (!$result) {
15578
$f = new ComparisonFailure(
156-
$this->subset,
79+
$patched,
15780
$other,
158-
\print_r($this->subset, true),
81+
\print_r($patched, true),
15982
\print_r($other, true)
16083
);
16184

tests/Framework/Constraint/ArraySubsetTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static function evaluateDataProvider()
9595
'10',
9696
],
9797
'other' => [
98-
0 => '1',
98+
0 => '1',
9999
'a' => [
100100
'aa' => '2',
101101
'ab' => [5, 4, 3],
@@ -112,7 +112,7 @@ public static function evaluateDataProvider()
112112
'10',
113113
],
114114
'other' => [
115-
0 => '1',
115+
0 => '1',
116116
'a' => [
117117
'aa' => '2',
118118
'ab' => [5, 4, 3],
@@ -129,7 +129,7 @@ public static function evaluateDataProvider()
129129
'10',
130130
],
131131
'other' => new \ArrayObject([
132-
0 => '1',
132+
0 => '1',
133133
'a' => [
134134
'aa' => '2',
135135
'ab' => [5, 4, 3],
@@ -146,7 +146,7 @@ public static function evaluateDataProvider()
146146
'10',
147147
]),
148148
'other' => [
149-
0 => '1',
149+
0 => '1',
150150
'a' => [
151151
'aa' => '2',
152152
'ab' => [5, 4, 3],

0 commit comments

Comments
 (0)