Completed
Push — master ( f45e90...50f395 )
by Jan-Petter
05:20
created

PhpAddOnTrait   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 20
c 1
b 1
f 0
lcom 0
cbo 0
dl 0
loc 77
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B arrayDiffAssocRecursive() 0 16 8
B arrayFilterRecursive() 0 12 5
B arrayMergeRecursiveEx() 0 21 7
1
<?php
2
/**
3
 * vipnytt/RobotsTxtParser
4
 *
5
 * @link https://github.com/VIPnytt/RobotsTxtParser
6
 * @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT)
7
 */
8
9
namespace vipnytt\RobotsTxtParser\Handler;
10
11
/**
12
 * Class PhpAddOnTrait
13
 *
14
 * @package vipnytt\RobotsTxtParser\Handler
15
 */
16
trait PhpAddOnTrait
17
{
18
    /**
19
     * array_diff_assoc_recursive
20
     * @link http://php.net/manual/en/function.array-diff-assoc.php#111675
21
     *
22
     * @param array $array1
23
     * @param array $array2
24
     * @return array
25
     */
26
    private function arrayDiffAssocRecursive(&$array1, &$array2)
27
    {
28
        $difference = [];
29
        foreach ($array1 as $key => $value) {
30
            if (is_array($value)) {
31
                if (!isset($array2[$key]) || !is_array($array2[$key])) {
32
                    $difference[$key] = $value;
33
                } elseif (!empty($new_diff = $this->arrayDiffAssocRecursive($value, $array2[$key]))) {
34
                    $difference[$key] = $new_diff;
35
                }
36
            } elseif (!array_key_exists($key, $array2) || $array2[$key] !== $value) {
37
                $difference[$key] = $value;
38
            }
39
        }
40
        return $difference;
41
    }
42
43
    /**
44
     * array_filter_recursive
45
     * @link http://php.net/manual/en/function.array-filter.php#87581
46
     *
47
     * @param array $array
48
     * @return array
49
     */
50
    private function arrayFilterRecursive(&$array)
51
    {
52
        foreach ($array as $key => $item) {
53
            is_array($item) && $array[$key] = $this->arrayFilterRecursive($item);
54
            if ($array[$key] === null ||
55
                $array[$key] === []
56
            ) {
57
                unset($array[$key]);
58
            }
59
        }
60
        return $array;
61
    }
62
63
    /**
64
     * array_merge_recursive_ex
65
     * @link http://stackoverflow.com/a/25712428/4396537
66
     *
67
     * @param array $array1
68
     * @param array $array2
69
     * @return array
70
     */
71
    private function arrayMergeRecursiveEx(array $array1, array &$array2)
72
    {
73
        $merged = $array1;
74
        foreach ($array2 as $key => &$value) {
75
            if (is_array($value) &&
76
                isset($merged[$key]) &&
77
                is_array($merged[$key])
78
            ) {
79
                $merged[$key] = $this->arrayMergeRecursiveEx($merged[$key], $value);
80
            } else {
81
                if (is_numeric($key)) {
82
                    if (!in_array($value, $merged)) {
83
                        $merged[] = $value;
84
                    }
85
                } else {
86
                    $merged[$key] = $value;
87
                }
88
            }
89
        }
90
        return $merged;
91
    }
92
}
93