Passed
Push — master ( ccfe7b...0df6e7 )
by Allan
02:16
created

PatchListUtils::sanitizeFileSystem()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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