Test Failed
Push — master ( 0ba812...546388 )
by Mr
02:44
created

Arrays::searchMd()   C

Complexity

Conditions 11
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 12
nc 3
nop 3
dl 0
loc 21
rs 6.4715
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace DrMVC\Helpers;
4
5
/**
6
 * Class Arrays
7
 * @package DrMVC\Helpers
8
 */
9
class Arrays
10
{
11
    /**
12
     * Order array by arguments
13
     *
14
     * @return  array
15
     */
16
    public static function orderBy(): array
17
    {
18
        $args = \func_get_args();
19
        $data = array_shift($args);
20
        foreach ($args as $n => $field) {
21
            if (\is_string($field)) {
22
                $tmp = [];
23
                foreach ($data as $key => $row) {
24
                    $tmp[$key] = $row[$field];
25
                }
26
                $args[$n] = $tmp;
27
            }
28
        }
29
        $args[] = &$data;
30
        array_multisort($args);
31
        return array_pop($args);
32
    }
33
34
    /**
35
     * Check if array is multidimensional
36
     *
37
     * @param   array $array
38
     * @return  bool
39
     */
40
    public static function isMulti($array): bool
41
    {
42
        // First we need know what value is array
43
        if (\is_array($array)) {
0 ignored issues
show
introduced by
The condition is_array($array) is always true.
Loading history...
44
            // And if some element is array then $array is multidimensional
45
            foreach ($array as $value) {
46
                if (\is_array($value)) {
47
                    return true;
48
                }
49
            }
50
        }
51
        return false;
52
    }
53
54
    /**
55
     * Check if two arrays is equal, with same keys
56
     *
57
     * @param   array $a
58
     * @param   array $b
59
     * @return  bool
60
     */
61
    public static function equal(array $a, array $b): bool
62
    {
63
        // Both arrays should be... arrays
64
        $is_arrays = (\is_array($a) && \is_array($b));
0 ignored issues
show
introduced by
The condition is_array($b) is always true.
Loading history...
65
        // Elements count should be equal
66
        $count_equal = (\count($a) === \count($b));
67
        // Between the keys should not be differences
68
        $difference = (array_diff($a, $b) === array_diff($b, $a));
69
70
        return ($is_arrays && $count_equal && $difference);
71
    }
72
73
    /**
74
     * Read any files and folder into some directory
75
     *
76
     * @param   string $path - Source directory with files
77
     * @return  array
78
     */
79
    public static function dirToArr($path): array
80
    {
81
        $result = [];
82
        $dirContent = scandir($path, null);
83
84
        foreach ($dirContent as $key => $value) {
85
            // Exclude system dots folders
86
            if (!\in_array($value, ['.', '..'])) {
87
                if (is_dir($path . DIRECTORY_SEPARATOR . $value)) {
88
                    $result[$value] = self::dirToArr($path . DIRECTORY_SEPARATOR . $value);
89
                } else {
90
                    $result[] = $value;
91
                }
92
            }
93
        }
94
95
        return $result;
96
    }
97
98
    /**
99
     * Find nested array inside two-dimensional array
100
     *
101
     * @param array|object $multidimensional - Where need to find
102
     * @param array|object $target - What we need to find
103
     * @param bool $isObject - Method work mode, if object then true, if array then false
104
     * @return bool|array
105
     */
106
    public static function searchMd($multidimensional, $target, $isObject = true)
107
    {
108
        if (empty($target) || empty($multidimensional)) {
109
            return false;
110
        }
111
112
        $output = array_map(
113
            function($element) use ($target, $isObject) {
114
                $exist = true;
115
                foreach ($target as $skey => $svalue) {
116
                    $exist = $isObject
117
                        ? ($exist && isset($element->$skey) && ($element->$skey === $svalue))
118
                        : ($exist && isset($element[$skey]) && ($element[$skey] === $svalue));
119
                }
120
                return $exist ? $element : null;
121
            },
122
            $multidimensional
0 ignored issues
show
Bug introduced by
It seems like $multidimensional can also be of type object; however, parameter $arr1 of array_map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

122
            /** @scrutinizer ignore-type */ $multidimensional
Loading history...
123
        );
124
125
        // If output is not empty, false by default
126
        return !empty($output) ? array_filter($output) : false;
127
    }
128
129
}
130