Skip to content

Commit

Permalink
Assert::isEqual() refactoring, used switch
Browse files Browse the repository at this point in the history
  • Loading branch information
dg authored and milo committed Mar 11, 2019
1 parent 3409b62 commit 135e02c
Showing 1 changed file with 36 additions and 37 deletions.
73 changes: 36 additions & 37 deletions src/Framework/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}


Expand Down

0 comments on commit 135e02c

Please sign in to comment.