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 — dev ( d7dbcb...64e823 )
by Jérémy
03:42 queued 01:52
created

Container::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace CapMousse\ReactRestify\Container;
4
5
use ReflectionClass;
6
use ReflectionMethod;
7
use ReflectionFunction;
8
use ReflectionFunctionAbstract;
9
10
class Container
11
{
12
    private static $instance;
13
14
    /**
15
     * Resolved types
16
     * @var array
17
     */
18
    protected $definitions = [];
19
20
    /**
21
     * Globally available container
22
     * 
23
     * @return static
24
     */
25
    public static function getInstance()
26
    {
27
        if (is_null(self::$instance)) {
28
            static::$instance = new static;
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
29
        }
30
31
        return static::$instance;
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
32
    }
33
34
    /**
35
     * Add item to the container
36
     * @param string $alias    
37
     * @param mixed|null $concrete
38
     */
39
    public function add($alias, $concrete = null)
40
    {
41
        if (is_null($concrete)) {
42
            $concrete = $alias;
43
        }
44
45
        $instance = $this->build($concrete);
46
47
        if (!$instance) {
48
            $this->definitions[$alias] = $instance;
49
        }
50
    }
51
52
    /**
53
     * Check if item is available in container
54
     * @param  string  $alias
55
     * @return boolean
56
     */
57
    public function has($alias)
58
    {
59
        if (array_key_exists($alias, $this->definitions)) {
60
            return true;
61
        }
62
63
        return false;
64
    }
65
66
    /**
67
     * Get an item of the container
68
     * @param  string $alias 
69
     * @return mixed
70
     */
71
    public function get($alias)
72
    {
73
        return $this->definitions[$alias];
74
    }
75
76
    /**
77
     * Call method with given parameters
78
     * @param  string|callable  $action     
79
     * @param  array            $args 
80
     * @return mixed
81
     */
82
    public function call ($action, array $args = [])
83
    {
84
        if (is_string($action)) {
85
            list($class, $method) = explode('@', $action);
86
            $action = [$this->build($class), $method];
87
        }
88
89
        $reflection = $this->getActionReflection($action);
90
91
        return call_user_func_array($action, $this->getParameters($reflection, $args));
92
    }
93
94
    /**
95
     * Get reflection from an action
96
     * @param  array|callable $action
97
     * @return ReflectionFunctionAbstract
98
     */
99
    public function getActionReflection($action)
100
    {
101
        if(is_array($action)) {
102
            list($class, $method) = $action;
103
            return new ReflectionMethod($class, $method);
104
        }
105
106
        return new ReflectionFunction($action);
107
    }
108
109
    /**
110
     * Create a new container for asked class
111
     * @param  string $concrete
0 ignored issues
show
Bug introduced by
There is no parameter named $concrete. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
112
     * @return Container
113
     */
114
    public function build ($class)
115
    {
116
        $reflection = new ReflectionClass($class);
117
        $parameters = [];
118
119
        if (!is_null($reflection->getConstructor())) {
120
            $parameters = $this->getParameters($reflection->getConstructor());
121
        }
122
123
        return new $class(...$parameters);
124
    }
125
126
    /**
127
     * Get reflection parameters
128
     * @param  ReflectionFunctionAbstract $reflection 
129
     * @param  array                      $args       
130
     * @return array                                 
131
     */
132
    public function getParameters(ReflectionFunctionAbstract $reflection, $args = [])
133
    {
134
        $dependencies = $reflection->getParameters();
135
        $parameters = [];
136
137
        foreach ($dependencies as $parameter) {
138
            $class = $parameter->getClass();
139
140
            if ($class && $this->has($class->getName())) {
141
                $parameters[] = $this->get($class->getName());
142
                continue;
143
            }
144
145
            if ($class && array_key_exists($class->getName(), $args)) {
146
                $parameters[] = $args[$class->getName()];
147
                continue;
148
            }
149
150
            if (array_key_exists($parameter->getName(), $args)) {
0 ignored issues
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
151
                $parameters[] = $args[$parameter->getName()];
0 ignored issues
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
152
            }
153
        }
154
155
        return $parameters;
156
    }
157
}