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

Analyser::getAllTargets()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 15
rs 10
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\Patch\DefinitionList;
7
8
use Vaimo\ComposerPatches\Patch\Definition as Patch;
9
10
class Analyser
11
{
12
    public function getAllTargets(array $patches)
13
    {
14
        $targetList = array();
15
16
        foreach ($patches as $patchGroup) {
17
            foreach ($patchGroup as $patchInfo) {
18
                $targetList = array_merge(
19
                    $targetList,
20
                    $patchInfo[Patch::TARGETS]
21
                );
22
            }
23
        }
24
25
        return array_values(
26
            array_unique($targetList)
27
        );
28
    }
29
30
    public function getRelatedPatches(array $patchesList, array $targets)
31
    {
32
        $scanTargets = $targets;
33
34
        $targetsStack = array();
35
36
        $result = array();
37
38
        do {
39
            $targetsUpdates = array();
40
41
            foreach ($patchesList as $owner => $patches) {
42
                foreach ($patches as $patchPath => $patchInfo) {
43
                    if (!array_intersect($patchInfo[Patch::TARGETS], $scanTargets)) {
44
                        continue;
45
                    }
46
47
                    if (!isset($result[$owner])) {
48
                        $result[$owner] = array();
49
                    }
50
51
                    $result[$owner][$patchPath] = $patchInfo;
52
53
                    $targetsUpdates = array_merge($targetsUpdates, $patchInfo[Patch::TARGETS]);
54
                }
55
            }
56
57
            $targetsStack = array_unique(
58
                array_merge($targetsStack, $scanTargets)
59
            );
60
61
            $scanTargets = array_diff($targetsUpdates, $targetsStack, $scanTargets);
62
        } while ($scanTargets);
0 ignored issues
show
Bug Best Practice introduced by
The expression $scanTargets of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
63
64
        return $result;
65
    }
66
67
    public function extractValue(array $patches, array $keys)
68
    {
69
        return array_reduce(
70
            $patches,
71
            function (array $result, array $items) use ($keys) {
72
                $values = array_values(
73
                    array_map(function ($item) use ($keys) {
74
                        foreach ($keys as $key) {
75
                            if (!isset($item[$key])) {
76
                                continue;
77
                            }
78
79
                            if (!$item[$key]) {
80
                                continue;
81
                            }
82
83
                            return $item[$key];
84
                        }
85
86
                        return null;
87
                    }, $items)
88
                );
89
90
                return array_merge($result, $values);
91
            },
92
            array()
93
        );
94
    }
95
}
96