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   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 125
Duplicated Lines 14.4 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 0
dl 18
loc 125
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 4 2
A getLoadOrder() 8 21 3
C getDependents() 0 32 7
A itemExists() 0 8 2
A hasDependents() 0 8 3
A getFailedItems() 10 18 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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