1<?php
2
3declare(strict_types=1);
4
5namespace Antlr\Antlr4\Runtime\Comparison;
6
7final class Equality
8{
9    private function __construct()
10    {
11    }
12
13    /**
14     * @param mixed $left
15     * @param mixed $right
16     */
17    public static function equals($left, $right) : bool
18    {
19        if ($left instanceof Equatable && $right instanceof Equatable) {
20            return $left->equals($right);
21        }
22
23        if (\is_array($left) && \is_array($right)) {
24            return self::deeplyEquals($left, $right);
25        }
26
27        return $left === $right;
28    }
29
30    /**
31     * @param array<mixed> $left
32     * @param array<mixed> $right
33     */
34    private static function deeplyEquals(array $left, array $right) : bool
35    {
36        if ($left === $right) {
37            return true;
38        }
39
40        if (\count($left) !== \count($right)) {
41            return false;
42        }
43
44        foreach ($left as $key => $value) {
45            if (!isset($right[$key])) {
46                return false;
47            }
48
49            if (!self::equals($value, $right[$key])) {
50                return false;
51            }
52        }
53
54        return true;
55    }
56}
57