Skip to content
Permalink
Browse files

Assert::isEqual() refactoring, used switch

  • Loading branch information...
dg authored and milo committed Feb 19, 2019
1 parent 3409b62 commit 135e02c8f3967b7114724a0ac73316da903f9204
Showing with 36 additions and 37 deletions.
  1. +36 βˆ’37 src/Framework/Assert.php
@@ -563,50 +563,49 @@ public static function expandMatchingPatterns(string $pattern, $actual): array
*/
private static function isEqual($expected, $actual, int $level = 0, $objects = null): bool
{
if ($level > 10) {
throw new \Exception('Nesting level too deep or recursive dependency.');
}
if ($expected instanceof Expect) {
$expected($actual);
return true;
}
if (is_float($expected) && is_float($actual) && is_finite($expected) && is_finite($actual)) {
$diff = abs($expected - $actual);
return ($diff < self::EPSILON) || ($diff / max(abs($expected), abs($actual)) < self::EPSILON);
}
switch (true) {
case $level > 10:
throw new \Exception('Nesting level too deep or recursive dependency.');
if (is_object($expected) && is_object($actual) && get_class($expected) === get_class($actual)) {
$objects = $objects ? clone $objects : new \SplObjectStorage;
if (isset($objects[$expected])) {
return $objects[$expected] === $actual;
} elseif ($expected === $actual) {
case $expected instanceof Expect:
$expected($actual);
return true;
}
$objects[$expected] = $actual;
$objects[$actual] = $expected;
$expected = (array) $expected;
$actual = (array) $actual;
}
if (is_array($expected) && is_array($actual)) {
ksort($expected, SORT_STRING);
ksort($actual, SORT_STRING);
if (array_keys($expected) !== array_keys($actual)) {
return false;
}
case is_float($expected) && is_float($actual) && is_finite($expected) && is_finite($actual):
$diff = abs($expected - $actual);
return ($diff < self::EPSILON) || ($diff / max(abs($expected), abs($actual)) < self::EPSILON);
foreach ($expected as $value) {
if (!self::isEqual($value, current($actual), $level + 1, $objects)) {
case is_object($expected) && is_object($actual) && get_class($expected) === get_class($actual):
$objects = $objects ? clone $objects : new \SplObjectStorage;
if (isset($objects[$expected])) {
return $objects[$expected] === $actual;
} elseif ($expected === $actual) {
return true;
}
$objects[$expected] = $actual;
$objects[$actual] = $expected;
$expected = (array) $expected;
$actual = (array) $actual;
// break omitted
case is_array($expected) && is_array($actual):
ksort($expected, SORT_STRING);
ksort($actual, SORT_STRING);
if (array_keys($expected) !== array_keys($actual)) {
return false;
}
next($actual);
}
return true;
}
return $expected === $actual;
foreach ($expected as $value) {
if (!self::isEqual($value, current($actual), $level + 1, $objects)) {
return false;
}
next($actual);
}
return true;
default:
return $expected === $actual;
}
}

0 comments on commit 135e02c

Please sign in to comment.
You can’t perform that action at this time.