GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

DependencySolver::getDependents()   C
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 6
nop 2
1
<?php
2
namespace Kunstmaan\Skylab\Utility;
3
4
class DependencySolver
5
{
6
7
    private $items = array();
8
9
    /**
10
     * @param string $item
11
     * @param string[] $dependencies
12
     */
13
    public function add($item, $dependencies = array())
14
    {
15
        $this->items[$item] = (count($dependencies) > 0) ? $dependencies : null;
16
    }
17
18
    /**
19
     * @return array
20
     */
21
    public function getLoadOrder()
22
    {
23
        $loadOrder = array();
24
        $seen = array();
25
26 View Code Duplication
        foreach ($this->items as $item => $dependencies) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
            $tmp = $this->getDependents($item, $seen);
28
29
            if ($tmp[2] === false) {
30
                $loadOrder = array_merge($loadOrder, $tmp[0]);
31
                $seen = $tmp[1];
32
            }
33
        }
34
35
        $loadOrder = array_filter($loadOrder, function ($skeleton) {
36
            return $skeleton != "base";
37
        });
38
        array_unshift($loadOrder, "base");
39
40
        return $loadOrder;
41
    }
42
43
    /**
44
     * @param $item
45
     * @param  array $seen
46
     * @return array
47
     */
48
    private function getDependents($item, $seen = array())
49
    {
50
        if (array_key_exists($item, $seen)) {
51
            return array(array(), $seen, false);
52
        }
53
54
        if ($this->itemExists($item)) {
55
            $order = array();
56
            $failed = array();
57
            $seen[$item] = true;
58
59
            if ($this->hasDependents($item)) {
60
                foreach ($this->items[$item] as $dependency) {
61
                    $tmp = $this->getDependents($dependency, $seen);
62
63
                    $order = array_merge($tmp[0], $order);
64
                    $seen = $tmp[1];
65
66
                    if ($tmp[2] !== false) {
67
                        $failed = array_merge($tmp[2], $failed);
68
                    }
69
                }
70
            }
71
72
            $order[] = $item;
73
            $failed = (count($failed) > 0) ? $failed : false;
74
75
            return array($order, $seen, $failed);
76
        }
77
78
        return array(array(), array(), array($item));
79
    }
80
81
    /**
82
     * @param $item
83
     * @return bool
84
     */
85
    public function itemExists($item)
86
    {
87
        if (array_key_exists($item, $this->items)) {
88
            return true;
89
        }
90
91
        return false;
92
    }
93
94
    /**
95
     * @param $item
96
     * @return bool
97
     */
98
    private function hasDependents($item)
99
    {
100
        if ($this->itemExists($item) && is_array($this->items[$item])) {
101
            return true;
102
        }
103
104
        return false;
105
    }
106
107
    /**
108
     * @return array
109
     */
110
    public function getFailedItems()
111
    {
112
        $failed = array();
113
        $seen = array();
114
115 View Code Duplication
        foreach ($this->items as $item => $dependencies) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
            $tmp = $this->getDependents($item, $seen);
117
118
            if ($tmp[2] !== false) {
119
                $failed[] = $item;
120
                continue;
121
            }
122
123
            $seen = $tmp[1];
124
        }
125
126
        return $failed;
127
    }
128
}
129