ArraysHelper::haveTheSameValue()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 13
ccs 0
cts 7
cp 0
crap 20
rs 10
1
<?php
2
/**
3
 * This file is part of Phiremock.
4
 *
5
 * Phiremock is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * Phiremock is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with Phiremock.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace Mcustiel\Phiremock\Common\Utils;
20
21
class ArraysHelper
22
{
23
    public static function isAssociative(array $array): bool
24
    {
25
        if (empty($array)) {
26
            return false;
27
        }
28
29
        return array_keys($array) !== range(0, \count($array) - 1);
30
    }
31
32
    public static function areRecursivelyEquals(array $array1, array $array2): bool
33
    {
34
        if (\count($array1) !== \count($array2)) {
35
            return false;
36
        }
37
38
        return self::arrayIsContained($array1, $array2);
39
    }
40
41
    public static function arrayIsContained(array $array1, array $array2): bool
42
    {
43
        foreach ($array1 as $key => $value1) {
44
            if (!\array_key_exists($key, $array2)) {
45
                return false;
46
            }
47
            if (!self::haveTheSameTypeAndValue($value1, $array2[$key])) {
48
                return false;
49
            }
50
        }
51
52
        return true;
53
    }
54
55
    public static function haveTheSameTypeAndValue($value1, $value2): bool
56
    {
57
        if (\gettype($value1) !== \gettype($value2)) {
58
            return false;
59
        }
60
61
        return self::haveTheSameValue($value1, $value2);
62
    }
63
64
    public static function haveTheSameValue($value1, $value2): bool
65
    {
66
        if (\is_array($value1)) {
67
            if (!self::areRecursivelyEquals($value1, $value2)) {
68
                return false;
69
            }
70
        } else {
71
            if ($value1 !== $value2) {
72
                return false;
73
            }
74
        }
75
76
        return true;
77
    }
78
}
79