Transformer::createSimplifiedList()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 20
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 29
rs 9.6
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Patch\DefinitionList;
7
8
use Vaimo\ComposerPatches\Patch\Definition as Patch;
9
10
class Transformer
11
{
12
    public function createSimplifiedList(array $patches)
13
    {
14
        $groups = $this->createTargetsList($patches);
15
16
        return array_map(function ($group) {
17
            $fingerprints = array_map(function ($item) {
18
                return sprintf(
19
                    '%s, %s:%s',
20
                    isset($item[Patch::LABEL]) ? $item[Patch::LABEL] : '{no label}',
21
                    Patch::HASH,
22
                    isset($item[Patch::HASH]) && $item[Patch::HASH] ? $item[Patch::HASH] : '{no hash}'
23
                );
24
            }, $group);
25
26
            $keys = array_map(
27
                function ($key, $item) {
28
                    return sprintf(
29
                        '%s%s%s',
30
                        $item[Patch::OWNER],
31
                        Patch::SOURCE_INFO_SEPARATOR,
32
                        $key
33
                    );
34
                },
35
                array_keys($group),
36
                $group
37
            );
38
39
            return array_combine($keys, $fingerprints);
40
        }, $groups);
41
    }
42
43
    public function createDetailedList(array $patches)
44
    {
45
        $result = array();
46
47
        $labelInfoMatcher = sprintf('/%s:(?P<hash>.*)/', Patch::HASH);
48
49
        foreach ($patches as $target => $group) {
50
            $result[$target] = array();
51
52
            if (!is_array($group)) {
53
                continue;
54
            }
55
56
            foreach ($group as $sourceInfo => $label) {
57
                $sourceConfig = explode(Patch::SOURCE_INFO_SEPARATOR, $sourceInfo);
58
59
                $path = array_pop($sourceConfig);
60
                $owner = array_pop($sourceConfig);
61
62
                $labelConfig = explode(',', $label);
63
64
                $matches = array();
65
                preg_match($labelInfoMatcher, trim(end($labelConfig)), $matches);
66
67
                $result[$target][$path] = array(
68
                    'path' => $path,
69
                    'targets' => array($target),
70
                    'source' => $path,
71
                    'owner' => $owner ? $owner : Patch::OWNER_UNKNOWN,
72
                    'label' => implode(',', array_slice($labelConfig, 0, -1)),
73
                    'md5' => is_array($matches) && isset($matches['hash']) ? $matches['hash'] : null
74
                );
75
            }
76
        }
77
78
        return $result;
79
    }
80
81
    public function createTargetsList(array $patches)
82
    {
83
        $result = array();
84
85
        foreach ($patches as $originName => $patchGroup) {
86
            if (!is_array($patchGroup)) {
87
                continue;
88
            }
89
90
            foreach ($patchGroup as $patchPath => $patchInfo) {
91
                foreach ($patchInfo[Patch::TARGETS] as $target) {
92
                    if (!isset($result[$target])) {
93
                        $result[$target] = array();
94
                    }
95
96
                    $path = (isset($patchInfo['url']) && $patchInfo['url']) ? $patchInfo['url'] : $patchPath;
97
98
                    $result[$target][$path] = array_replace(
99
                        $patchInfo,
100
                        array(Patch::ORIGIN => $originName)
101
                    );
102
                }
103
            }
104
        }
105
106
        return $result;
107
    }
108
109
    public function groupItemsByTarget(array $patchesList)
110
    {
111
        $result = array();
112
113
        foreach ($patchesList as $origin => $group) {
114
            if (!isset($result[$origin])) {
115
                $result[$origin] = array();
116
            }
117
118
            foreach ($group as $path => $patch) {
119
                foreach ($patch[Patch::TARGETS] as $target) {
120
                    $result[$target][$path] = array_replace(
121
                        $patch,
122
                        array(Patch::ORIGIN => $origin)
123
                    );
124
                }
125
            }
126
        }
127
128
        return array_filter($result);
129
    }
130
131
    public function createOriginList(array $patchesList)
132
    {
133
        $result = array();
134
135
        foreach ($patchesList as $group) {
136
            foreach ($group as $path => $patch) {
137
                $origin = $patch[Patch::ORIGIN];
138
139
                if (!isset($result[$origin])) {
140
                    $result[$origin] = array();
141
                }
142
143
                if (isset($result[$origin][$path])) {
144
                    continue;
145
                }
146
147
                $result[$origin][$path] = array_diff_key($patch, array(Patch::ORIGIN => true));
148
            }
149
        }
150
151
        return array_filter($result);
152
    }
153
}
154