Passed
Push — master ( 11eaad...3f8f84 )
by Allan
02:05
created

Analyser::getAllTargets()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
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\Patch\DefinitionList;
7
8
use Vaimo\ComposerPatches\Patch\Definition as Patch;
9
10
class Analyser
11
{
12
    public function getAllTargets(array $patches)
13
    {
14
        $patchTargets = array();
15
16
        foreach ($patches as $patchGroup) {
17
            foreach ($patchGroup as $patchInfo) {
18
                $patchTargets[] = $patchInfo[Patch::TARGETS];
19
            }
20
        }
21
22
        $targetList = array_reduce($patchTargets, 'array_merge', array());
23
24
        return array_values(
25
            array_unique($targetList)
26
        );
27
    }
28
29
    public function getRelatedPatches(array $patchesList, array $targets)
30
    {
31
        $scanTargets = $targets;
32
33
        $targetsStack = array();
34
35
        $result = array();
36
37
        do {
38
            $patchTargets = array();
39
40
            foreach ($patchesList as $owner => $patches) {
41
                foreach ($patches as $patchPath => $patchInfo) {
42
                    if (!array_intersect($patchInfo[Patch::TARGETS], $scanTargets)) {
43
                        continue;
44
                    }
45
46
                    if (!isset($result[$owner])) {
47
                        $result[$owner] = array();
48
                    }
49
50
                    $result[$owner][$patchPath] = $patchInfo;
51
52
                    $patchTargets[] = $patchInfo[Patch::TARGETS];
53
                }
54
            }
55
56
            $targetsUpdates = array_reduce($patchTargets, 'array_merge', array());
57
58
            $targetsStack = array_unique(
59
                array_merge($targetsStack, $scanTargets)
60
            );
61
62
            $scanTargets = array_diff($targetsUpdates, $targetsStack, $scanTargets);
63
        } while (!empty($scanTargets));
64
65
        return $result;
66
    }
67
68
    public function extractValue(array $patches, array $keys)
69
    {
70
        return array_reduce(
71
            $patches,
72
            function (array $result, array $items) use ($keys) {
73
                $values = array_values(
74
                    array_map(function ($item) use ($keys) {
75
                        foreach ($keys as $key) {
76
                            if (!isset($item[$key])) {
77
                                continue;
78
                            }
79
80
                            if (!$item[$key]) {
81
                                continue;
82
                            }
83
84
                            return $item[$key];
85
                        }
86
87
                        return null;
88
                    }, $items)
89
                );
90
91
                return array_merge($result, $values);
92
            },
93
            array()
94
        );
95
    }
96
97
    public function extractDictionary(array $patches, array $keys)
98
    {
99
        $keyFlags = array_fill_keys($keys, null);
100
        
101
        return array_reduce(
102
            $patches,
103
            function (array $result, array $items) use ($keyFlags) {
104
                $values = array_values(
105
                    array_map(function ($item) use ($keyFlags) {
106
                        return array_replace(
107
                            $keyFlags,
108
                            array_intersect_key($item, $keyFlags)
109
                        );
110
                    }, $items)
111
                );
112
113
                return array_merge($result, $values);
114
            },
115
            array()
116
        );
117
    }
118
}
119