PatchListUtils::diffListsByPath()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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