Skip to content

Commit bdc8074

Browse files
committed
rename ArrayUtil::diffAssocRecursive to diffDeep / integer keyed values need not match key
1 parent 3c4dcea commit bdc8074

4 files changed

Lines changed: 28 additions & 21 deletions

File tree

src/Debug/Abstraction/Object/Abstraction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function getInheritedValues()
147147
*/
148148
public function getInstanceValues()
149149
{
150-
return ArrayUtil::diffAssocRecursive(
150+
return ArrayUtil::diffDeep(
151151
$this->values,
152152
$this->getInheritedValues()
153153
);

src/Debug/Utility/ArrayUtil.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ public static function copy(array $source, $deep = true)
9797
*
9898
* @throws InvalidArgumentException
9999
*/
100-
public static function diffAssocRecursive(array $array1, $array2) // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
100+
public static function diffDeep(array $array1, $array2) // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
101101
{
102102
$arrays = \array_slice(\func_get_args(), 1);
103103
foreach ($arrays as $i => $array) {
104104
\bdk\Debug\Utility\PhpType::assertType($array, 'array', 'array' . ($i + 2));
105105
}
106-
return \array_reduce($arrays, [__CLASS__, 'diffAssocRecursiveWalk'], $array1);
106+
return \array_reduce($arrays, [__CLASS__, 'diffDeepWalk'], $array1);
107107
}
108108

109109
/**

src/Debug/Utility/ArrayUtilHelperTrait.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,31 +89,38 @@ private static function assertCountable($value, array $path, $method)
8989
*
9090
* @return array An array containing all the values from array that are not present in array2
9191
*/
92-
private static function diffAssocRecursiveWalk(array $array, array $array2)
92+
private static function diffDeepWalk(array $array, array $array2)
9393
{
9494
$diff = array();
95+
$allInt = true;
9596
\array_walk(
9697
$array,
9798
/**
9899
* @param array-key $key
99100
*/
100-
static function ($value, $key) use (&$diff, $array2) {
101+
static function ($value, $key) use (&$allInt, &$diff, $array2) {
102+
$incl = false;
101103
if (\array_key_exists($key, $array2) === false) {
102-
$diff[$key] = $value;
103-
return;
104-
}
105-
if (\is_array($value) && \is_array($array2[$key])) {
106-
$value = self::diffAssocRecursive($value, $array2[$key]);
107-
if ($value) {
108-
$diff[$key] = $value;
109-
}
110-
return;
104+
$incl = true;
105+
} elseif (self::isNonMergeable($value) === false && self::isNonMergeable($array2[$key]) === false) {
106+
$value = self::diffDeep($value, $array2[$key]);
107+
$incl = !empty($value);
108+
} elseif (\is_int($key)) {
109+
$foundIndex = \array_search($value, $array2, true);
110+
$incl = $foundIndex === false || \is_int($foundIndex) === false;
111+
} elseif ($value !== $array2[$key]) {
112+
// not in $array2 or different value
113+
$incl = true;
111114
}
112-
if ($value !== $array2[$key]) {
115+
if ($incl) {
116+
$allInt = $allInt && \is_int($key);
113117
$diff[$key] = $value;
114118
}
115119
}
116120
);
121+
if ($allInt) {
122+
$diff = \array_values($diff);
123+
}
117124
return $diff;
118125
}
119126

tests/Debug/Utility/ArrayUtilTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public function testCopy()
4848
}
4949

5050
/**
51-
* @dataProvider providerDiffAssocRecursive
51+
* @dataProvider providerDiffDeep
5252
*/
53-
public function testDiffAssocRecursive($argsAndExpect)
53+
public function testDiffDeep($argsAndExpect)
5454
{
5555
$argsAndExpect = \func_get_args();
5656
$expect = \array_pop($argsAndExpect);
@@ -62,7 +62,7 @@ public function testDiffAssocRecursive($argsAndExpect)
6262
}
6363
self::assertSame(
6464
$expect,
65-
\call_user_func_array('bdk\Debug\Utility\ArrayUtil::diffAssocRecursive', $argsAndExpect)
65+
\call_user_func_array('bdk\Debug\Utility\ArrayUtil::diffDeep', $argsAndExpect)
6666
);
6767
}
6868

@@ -325,7 +325,7 @@ public function testSpliceAssoc($array, $key, $length, $replace, $expectReturn,
325325
self::assertSame($expectArray, $array, 'updated array not as expected');
326326
}
327327

328-
public static function providerDiffAssocRecursive()
328+
public static function providerDiffDeep()
329329
{
330330
return array(
331331
'test1' => array(
@@ -359,7 +359,7 @@ public static function providerDiffAssocRecursive()
359359
array(
360360
'colors' => array(
361361
'b' => 'brown',
362-
'red',
362+
// 'red',
363363
),
364364
),
365365
),
@@ -370,7 +370,7 @@ public static function providerDiffAssocRecursive()
370370
false,
371371
array(
372372
'expectException' => 'InvalidArgumentException',
373-
'expectExceptionMessage' => 'bdk\Debug\Utility\ArrayUtil::diffAssocRecursive(): $array2 expects array. bool provided',
373+
'expectExceptionMessage' => 'bdk\Debug\Utility\ArrayUtil::diffDeep(): $array2 expects array. bool provided',
374374
),
375375
),
376376
);

0 commit comments

Comments
 (0)