Completed
Push — master ( eb792d...2bb0ab )
by Phil
03:36
created

src/Route.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace League\Route;
4
5
use InvalidArgumentException;
6
use League\Route\ContainerAwareInterface;
7
use League\Route\ContainerAwareTrait;
8
use League\Route\Http\RequestAwareInterface;
9
use League\Route\Http\ResponseAwareInterface;
10
use League\Route\Middleware\ExecutionChain;
11
use League\Route\Middleware\StackAwareInterface as MiddlewareAwareInterface;
12
use League\Route\Middleware\StackAwareTrait as MiddlewareAwareTrait;
13
use League\Route\Strategy\StrategyAwareInterface;
14
use League\Route\Strategy\StrategyAwareTrait;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
18
class Route implements ContainerAwareInterface, MiddlewareAwareInterface, StrategyAwareInterface
19
{
20
    use ContainerAwareTrait;
21
    use MiddlewareAwareTrait;
22
    use RouteConditionTrait;
23
    use StrategyAwareTrait;
24
25
    /**
26
     * @var string|callable
27
     */
28
    protected $callable;
29
30
    /**
31
     * @var \League\Route\RouteGroup
32
     */
33
    protected $group;
34
35
    /**
36
     * @var string[]
37
     */
38
    protected $methods = [];
39
40
    /**
41
     * @var string
42
     */
43
    protected $path;
44
45
    /**
46
     * Dispatch the route via the attached strategy.
47
     *
48
     * @param array $vars
49
     *
50
     * @return \League\Route\Middleware\ExecutionChain
51
     */
52 15
    public function getExecutionChain(array $vars)
53
    {
54 15
        $callable = $this->getStrategy()->getCallable($this, $vars);
55
56
57 15
        foreach ($this->getMiddlewareStack() as $middleware) {
58
            $execChain->middleware($middleware);
0 ignored issues
show
The variable $execChain seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
59 15
        }
60
61 15
        $execChain = (new ExecutionChain)->middleware($callable);
62
63 15
        return $execChain;
64
    }
65
66
    /**
67
     * Get the callable.
68
     *
69
     * @throws \RuntimeException
70
     *
71
     * @return callable
72
     */
73 33
    public function getCallable()
74
    {
75 33
        $callable = $this->callable;
76
77 33
        if (is_string($callable) && strpos($callable, '::') !== false) {
78 6
            $callable = explode('::', $callable);
79 6
        }
80
81 33
        if (is_array($callable) && isset($callable[0]) && is_object($callable[0])) {
82 3
            $callable = [$callable[0], $callable[1]];
83 3
        }
84
85 33
        if (is_array($callable) && isset($callable[0]) && is_string($callable[0])) {
86 6
            $class = ($this->getContainer()->has($callable[0]))
87 6
                   ? $this->getContainer()->get($callable[0])
88 6
                   : new $callable[0];
89
90 6
            $callable = [$class, $callable[1]];
91 6
        }
92
93 33
        if (! is_callable($callable)) {
94 3
            throw new InvalidArgumentException('Could not resolve a callable for this route');
95
        }
96
97 30
        return $callable;
98
    }
99
100
    /**
101
     * Set the callable.
102
     *
103
     * @param string|callable $callable
104
     *
105
     * @return \League\Route\Route
106
     */
107 48
    public function setCallable($callable)
108
    {
109 48
        $this->callable = $callable;
110
111 48
        return $this;
112
    }
113
114
    /**
115
     * Get the parent group.
116
     *
117
     * @return \League\Route\RouteGroup
118
     */
119 3
    public function getParentGroup()
120
    {
121 3
        return $this->group;
122
    }
123
124
    /**
125
     * Set the parent group.
126
     *
127
     * @param \League\Route\RouteGroup $group
128
     *
129
     * @return \League\Route\Route
130
     */
131 6
    public function setParentGroup(RouteGroup $group)
132
    {
133 6
        $this->group = $group;
134
135 6
        return $this;
136
    }
137
138
    /**
139
     * Get the path.
140
     *
141
     * @return string
142
     */
143 27
    public function getPath()
144
    {
145 27
        return $this->path;
146
    }
147
148
    /**
149
     * Set the path.
150
     *
151
     * @param string $path
152
     *
153
     * @return \League\Route\Route
154
     */
155 33
    public function setPath($path)
156
    {
157 33
        $this->path = $path;
158
159 33
        return $this;
160
    }
161
162
    /**
163
     * Get the methods.
164
     *
165
     * @return string[]
166
     */
167 27
    public function getMethods()
168
    {
169 27
        return $this->methods;
170
    }
171
172
    /**
173
     * Get the methods.
174
     *
175
     * @param string[] $methods
176
     *
177
     * @return \League\Route\Route
178
     */
179 33
    public function setMethods(array $methods)
180
    {
181 33
        $this->methods = $methods;
182
183 33
        return $this;
184
    }
185
}
186