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.
Completed
Push — master ( 3c316c...decbbd )
by Roderik
03:14
created

SkeletonProvider::getSkeletons()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Kunstmaan\Skylab\Provider;
4
5
use Cilex\Application;
6
use Kunstmaan\Skylab\Skeleton\AbstractSkeleton;
7
use Kunstmaan\Skylab\Utility\DependencySolver;
8
use RuntimeException;
9
10
/**
11
 * SkeletonProvider
12
 */
13
class SkeletonProvider extends AbstractProvider
14
{
15
16
    /**
17
     * Registers services on the given app.
18
     *
19
     * @param Application $app An Application instance
20
     */
21
    public function register(Application $app)
22
    {
23
        $app['skeleton'] = $this;
24
        $this->app = $app;
25
    }
26
27
    /**
28
     * @param \ArrayObject $project The project
29
     * @param AbstractSkeleton $skeleton The skeleton
30
     * @return bool
31
     */
32
    public function hasSkeleton(\ArrayObject $project, AbstractSkeleton $skeleton)
33
    {
34
        return isset($project["skeletons"]) && array_key_exists($skeleton->getName(), $project["skeletons"]);
35
    }
36
37
38
    /**
39
     * @param \ArrayObject     $project  The project
40
     * @param AbstractSkeleton $skeleton The skeleton
41
     */
42
    public function applySkeleton(\ArrayObject $project, AbstractSkeleton $skeleton)
43
    {
44
        $dependencies = new DependencySolver();
45
        $this->resolveDependencies($skeleton, $dependencies);
46
        foreach ($dependencies->getLoadOrder() as $theSkeletonName) {
47
            $theSkeleton = $this->findSkeleton($theSkeletonName);
48
            if (!$this->hasSkeleton($project, $theSkeleton)) {
49
                $this->dialogProvider->logTask("Running skeleton create for " . $theSkeleton->getName());
50
                $theSkeleton->create($project);
51
                $project["skeletons"][$theSkeleton->getName()] = $theSkeleton->getName();
52
            }
53
        }
54
    }
55
56
    /**
57
     * @param  AbstractSkeleton                           $theSkeleton
58
     * @param  \Kunstmaan\Skylab\Utility\DependencySolver $dependencies
59
     * @return \ArrayObject
0 ignored issues
show
Documentation introduced by
Should the return type not be \ArrayObject|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
60
     */
61
    private function resolveDependencies(AbstractSkeleton $theSkeleton, DependencySolver $dependencies)
62
    {
63
        if (!$dependencies->itemExists($theSkeleton->getName())) {
64
            $skeletonDeps = $theSkeleton->dependsOn();
65
            $dependencies->add($theSkeleton->getName(), $skeletonDeps);
66
            foreach ($skeletonDeps as $skeletonDependencyName) {
67
                $aSkeleton = $this->findSkeleton($skeletonDependencyName);
68
                if ($aSkeleton) {
69
                    $this->resolveDependencies($aSkeleton, $dependencies);
70
                }
71
            }
72
        }
73
    }
74
75
    /**
76
     * @param  string                $skeletonname
77
     * @return AbstractSkeleton
78
     * @throws \RuntimeException
79
     */
80
    public function findSkeleton($skeletonname)
81
    {
82
        if (isset($this->app["config"]["skeletons"][$skeletonname])) {
83
            $skeleton = $this->app["config"]["skeletons"][$skeletonname];
84
            if ($skeleton === false) {
85
                return false;
86
            } else {
87
                return new $skeleton($this->app);
88
            }
89
        }
90
        throw new RuntimeException("Skeleton " . $skeletonname . " not found!");
91
    }
92
93
    /**
94
     * @return void
95
     */
96
    public function listSkeletons()
97
    {
98
        foreach (array_keys($this->app["config"]["skeletons"]) as $name) {
99
            $this->dialogProvider->logTask($name);
100
        }
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function getSkeletons()
107
    {
108
        foreach (array_keys($this->app["config"]["skeletons"]) as $name) {
109
            $skeletons[] = $name;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$skeletons was never initialized. Although not strictly required by PHP, it is generally a good practice to add $skeletons = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
110
        }
111
112
        return $skeletons;
0 ignored issues
show
Bug introduced by
The variable $skeletons does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
113
    }
114
115
    /**
116
     * @param \Closure $callback
117
     * @param \ArrayObject $skeletons
0 ignored issues
show
Documentation introduced by
Should the type for parameter $skeletons not be null|\ArrayObject?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
118
     */
119
    public function skeletonLoop($callback, \ArrayObject $skeletons = null)
120
    {
121
        if (!$skeletons) {
122
            $skeletons = new \ArrayObject(array_keys($this->app["config"]["skeletons"]));
123
        }
124
        $dependencies = new DependencySolver();
125
        foreach ($skeletons as $skeleton) {
126
            $theSkeleton = $this->findSkeleton($skeleton);
127
            if ($theSkeleton) {
128
                $this->resolveDependencies($theSkeleton, $dependencies);
129
            }
130
        }
131
        foreach ($dependencies->getLoadOrder() as $skeletonName) {
132
            $skeleton = $this->findSkeleton($skeletonName);
133
            if ($skeleton) {
134
                $callback($skeleton);
135
            }
136
        }
137
    }
138
139
}
140