Passed
Branch master (4a352c)
by Allan
04:38 queued 02:31
created

PatchListUtils::groupItemsByTarget()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 7
nop 1
dl 0
loc 20
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Utils;
7
8
use Vaimo\ComposerPatches\Patch\Definition as Patch;
9
10
class PatchListUtils
11
{
12
    public function sanitizeFileSystem(array $patches)
13
    {
14
        foreach ($patches as $patchGroup) {
15
            foreach ($patchGroup as $patchInfo) {
16
                if (!isset($patchInfo[Patch::TMP]) || !$patchInfo[Patch::TMP]) {
17
                    continue;
18
                }
19
20
                unlink($patchInfo[Patch::PATH]);
21
            }
22
        }
23
    }
24
    
25
    public function applyDefinitionFilter(array $patches, $logicProvider)
26
    {
27
        foreach ($patches as &$packagePatches) {
28
            foreach ($packagePatches as &$patchData) {
29
                $result = $logicProvider($patchData);
30
                
31
                if ($result) {
32
                    continue;
33
                }
34
35
                $patchData = false;
36
            }
37
38
            $packagePatches = array_filter($packagePatches);
39
        }
40
41
        return array_filter($patches);
42
    }
43
    
44
    public function applyDefinitionKeyValueFilter(array $patches, $filter, $key)
45
    {
46
        foreach ($patches as &$packagePatches) {
47
            foreach ($packagePatches as &$patchInfo) {
48
                if (!isset($patchInfo[$key])) {
49
                    $patchInfo = false;
50
                    
51
                    continue;
52
                }
53
                
54
                if ($this->shouldIncludePatch($patchInfo[$key], $filter)) {
55
                    continue;
56
                }
57
58
                $patchInfo = false;
59
            }
60
        }
61
62
        return array_filter(
63
            array_map('array_filter', $patches)
64
        );
65
    }
66
    
67
    private function shouldIncludePatch($value, $filter)
68
    {
69
        if (is_array($value) && preg_grep($filter, $value)) {
70
            return true;
71
        }
72
73
        if (is_string($value) && preg_match($filter, $value)) {
74
            return true;
75
        }
76
77
        if (is_bool($filter) && $value === $filter) {
78
            return true;
79
        }
80
        
81
        return false;
82
    }
83
    
84
    public function filterListByTargets(array $patches, array $targets)
85
    {
86
        foreach ($patches as $target => $group) {
87
            foreach ($group as $path => $patch) {
88
                if (array_intersect($patch[Patch::TARGETS], $targets)) {
89
                    continue;
90
                }
91
92
                unset($patches[$target][$path]);
93
            }
94
        }
95
        
96
        return array_filter($patches);
97
    }
98
    
99
    public function mergeLists(array $listA, array $listB)
100
    {
101
        $result = array();
102
103
        $keys = array_unique(
104
            array_merge(array_keys($listA), array_keys($listB))
105
        );
106
        
107
        foreach ($keys as $key) {
108
            $result[$key] = array_replace(
109
                isset($listA[$key]) ? $listA[$key] : array(),
110
                isset($listB[$key]) ? $listB[$key] : array()
111
            );
112
        }
113
114
        return $result;
115
    }
116
    
117
    public function diffListsByPath(array $listA, array $listB)
118
    {
119
        $pathFlags = array_fill_keys($this->getAllPaths($listB), true);
120
121
        return array_map(function (array $group) use ($pathFlags) {
122
            return array_filter(
123
                $group,
124
                function (array $item) use ($pathFlags) {
125
                    $path = $item[Patch::PATH] ? $item[Patch::PATH] : $item[Patch::URL];
126
                    
127
                    return !isset($pathFlags[$path]);
128
                }
129
            );
130
        }, $listA);
131
    }
132
    
133
    public function intersectListsByPath(array $listA, array $listB)
134
    {
135
        $pathFlags = array_fill_keys($this->getAllPaths($listB), true);
136
137
        return array_map(function (array $group) use ($pathFlags) {
138
            return array_filter(
139
                $group,
140
                function (array $item) use ($pathFlags) {
141
                    $path = $item[Patch::PATH] ? $item[Patch::PATH] : $item[Patch::URL];
142
143
                    return isset($pathFlags[$path]);
144
                }
145
            );
146
        }, $listA);
147
    }
148
149
    public function diffListsByName(array $listA, array $listB)
150
    {
151
        foreach ($listB as $target => $group) {
152
            if (!isset($listA[$target])) {
153
                $listA[$target] = array();
154
            }
155
156
            $listA[$target] = array_diff_key($listA[$target], $group);
157
        }
158
159
        return $listA;
160
    }
161
162
    public function intersectListsByName(array $listA, array $listB)
163
    {
164
        $result = array();
165
        
166
        foreach ($listB as $target => $group) {
167
            if (!isset($listA[$target])) {
168
                continue;
169
            }
170
            
171
            $result[$target] = array_intersect_key($listA[$target], $group);
172
        }
173
174
        return $result;
175
    }
176
177
    private function getAllPaths($patches)
178
    {
179
        return array_reduce(
180
            $patches,
181
            function ($result, array $group) {
182
                return array_merge(
183
                    $result,
184
                    array_values(
185
                        array_map(function (array $item) {
186
                            return $item[Patch::PATH] ? $item[Patch::PATH] : $item[Patch::URL];
187
                        }, $group)
188
                    )
189
                );
190
            },
191
            array()
192
        );
193
    }
194
}
195