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
Pull Request — 3.x (#2385)
by
unknown
02:04
created

Route::getPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Slim Framework (https://slimframework.com)
4
 *
5
 * @link      https://github.com/slimphp/Slim
6
 * @copyright Copyright (c) 2011-2017 Josh Lockhart
7
 * @license   https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
8
 */
9
namespace Slim;
10
11
use InvalidArgumentException;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
use Slim\Handlers\Strategies\RequestResponse;
15
use Slim\Interfaces\InvocationStrategyInterface;
16
use Slim\Interfaces\RouteInterface;
17
18
/**
19
 * Route
20
 */
21
class Route extends Routable implements RouteInterface
22
{
23
    use MiddlewareAwareTrait;
24
25
    /**
26
     * HTTP methods supported by this route
27
     *
28
     * @var string[]
29
     */
30
    protected $methods = [];
31
32
    /**
33
     * Route identifier
34
     *
35
     * @var string
36
     */
37
    protected $identifier;
38
39
    /**
40
     * Route name
41
     *
42
     * @var null|string
43
     */
44
    protected $name;
45
46
    /**
47
     * Parent route groups
48
     *
49
     * @var RouteGroup[]
50
     */
51
    protected $groups;
52
53
    private $finalized = false;
54
55
    /**
56
     * Output buffering mode
57
     *
58
     * One of: false, 'prepend' or 'append'
59
     *
60
     * @var boolean|string
61
     */
62
    protected $outputBuffering = 'append';
63
64
    /**
65
     * Route parameters
66
     *
67
     * @var array
68
     */
69
    protected $arguments = [];
70
71
    /**
72
     * The callable payload
73
     *
74
     * @var callable
75
     */
76
    protected $callable;
77
78
    /**
79
     * Create new route
80
     *
81
     * @param string|string[]   $methods The route HTTP methods
82
     * @param string            $pattern The route pattern
83
     * @param callable          $callable The route callable
84
     * @param RouteGroup[]      $groups The parent route groups
85
     * @param int               $identifier The route identifier
86
     */
87
    public function __construct($methods, $pattern, $callable, $groups = [], $identifier = 0)
88
    {
89
        $this->methods  = is_string($methods) ? [$methods] : $methods;
90
        $this->pattern  = $pattern;
91
        $this->callable = $callable;
92
        $this->groups   = $groups;
93
        $this->identifier = 'route' . $identifier;
94
    }
95
96
    /**
97
     * Finalize the route in preparation for dispatching
98
     */
99
    public function finalize()
100
    {
101
        if ($this->finalized) {
102
            return;
103
        }
104
105
        $groupMiddleware = [];
106
        foreach ($this->getGroups() as $group) {
107
            $groupMiddleware = array_merge($group->getMiddleware(), $groupMiddleware);
108
        }
109
110
        $this->middleware = array_merge($this->middleware, $groupMiddleware);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->middleware, $groupMiddleware) of type array is incompatible with the declared type array<integer,callable> of property $middleware.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
111
112
        foreach ($this->getMiddleware() as $middleware) {
113
            $this->addMiddleware($middleware);
114
        }
115
116
        $this->finalized = true;
117
    }
118
119
    /**
120
     * Get route callable
121
     *
122
     * @return callable
123
     */
124
    public function getCallable()
125
    {
126
        return $this->callable;
127
    }
128
129
    /**
130
     * This method enables you to override the Route's callable
131
     *
132
     * @param string|\Closure $callable
133
     */
134
    public function setCallable($callable)
135
    {
136
        $this->callable = $callable;
137
    }
138
139
    /**
140
     * Get route methods
141
     *
142
     * @return string[]
143
     */
144
    public function getMethods()
145
    {
146
        return $this->methods;
147
    }
148
    
149
    /**
150
     * Get route pattern
151
     *
152
     * @return string[]
153
     */
154
    public function getPattern()
155
    {
156
        return $this->pattern;
157
    }
158
159
    /**
160
     * Get parent route groups
161
     *
162
     * @return RouteGroup[]
163
     */
164
    public function getGroups()
165
    {
166
        return $this->groups;
167
    }
168
169
    /**
170
     * Get route name
171
     *
172
     * @return null|string
173
     */
174
    public function getName()
175
    {
176
        return $this->name;
177
    }
178
179
    /**
180
     * Get route identifier
181
     *
182
     * @return string
183
     */
184
    public function getIdentifier()
185
    {
186
        return $this->identifier;
187
    }
188
189
    /**
190
     * Get output buffering mode
191
     *
192
     * @return boolean|string
193
     */
194
    public function getOutputBuffering()
195
    {
196
        return $this->outputBuffering;
197
    }
198
199
    /**
200
     * Set output buffering mode
201
     *
202
     * One of: false, 'prepend' or 'append'
203
     *
204
     * @param boolean|string $mode
205
     *
206
     * @throws InvalidArgumentException If an unknown buffering mode is specified
207
     */
208
    public function setOutputBuffering($mode)
209
    {
210
        if (!in_array($mode, [false, 'prepend', 'append'], true)) {
211
            throw new InvalidArgumentException('Unknown output buffering mode');
212
        }
213
        $this->outputBuffering = $mode;
214
    }
215
216
    /**
217
     * Set route name
218
     *
219
     * @param string $name
220
     *
221
     * @return self
222
     *
223
     * @throws InvalidArgumentException if the route name is not a string
224
     */
225
    public function setName($name)
226
    {
227
        if (!is_string($name)) {
228
            throw new InvalidArgumentException('Route name must be a string');
229
        }
230
        $this->name = $name;
231
        return $this;
232
    }
233
234
    /**
235
     * Set a route argument
236
     *
237
     * @param string $name
238
     * @param string $value
239
     *
240
     * @return self
241
     */
242
    public function setArgument($name, $value)
243
    {
244
        $this->arguments[$name] = $value;
245
        return $this;
246
    }
247
248
    /**
249
     * Replace route arguments
250
     *
251
     * @param array $arguments
252
     *
253
     * @return self
254
     */
255
    public function setArguments(array $arguments)
256
    {
257
        $this->arguments = $arguments;
258
        return $this;
259
    }
260
261
    /**
262
     * Retrieve route arguments
263
     *
264
     * @return array
265
     */
266
    public function getArguments()
267
    {
268
        return $this->arguments;
269
    }
270
271
    /**
272
     * Retrieve a specific route argument
273
     *
274
     * @param string $name
275
     * @param string|null $default
276
     *
277
     * @return mixed
278
     */
279
    public function getArgument($name, $default = null)
280
    {
281
        if (array_key_exists($name, $this->arguments)) {
282
            return $this->arguments[$name];
283
        }
284
        return $default;
285
    }
286
287
    /********************************************************************************
288
     * Route Runner
289
     *******************************************************************************/
290
291
    /**
292
     * Prepare the route for use
293
     *
294
     * @param ServerRequestInterface $request
295
     * @param array $arguments
296
     */
297
    public function prepare(ServerRequestInterface $request, array $arguments)
298
    {
299
        // Add the arguments
300
        foreach ($arguments as $k => $v) {
301
            $this->setArgument($k, $v);
302
        }
303
    }
304
305
    /**
306
     * Run route
307
     *
308
     * This method traverses the middleware stack, including the route's callable
309
     * and captures the resultant HTTP response object. It then sends the response
310
     * back to the Application.
311
     *
312
     * @param ServerRequestInterface $request
313
     * @param ResponseInterface      $response
314
     *
315
     * @return ResponseInterface
316
     */
317
    public function run(ServerRequestInterface $request, ResponseInterface $response)
318
    {
319
        // Finalise route now that we are about to run it
320
        $this->finalize();
321
322
        // Traverse middleware stack and fetch updated response
323
        return $this->callMiddlewareStack($request, $response);
324
    }
325
326
    /**
327
     * Dispatch route callable against current Request and Response objects
328
     *
329
     * This method invokes the route object's callable. If middleware is
330
     * registered for the route, each callable middleware is invoked in
331
     * the order specified.
332
     *
333
     * @param ServerRequestInterface $request  The current Request object
334
     * @param ResponseInterface      $response The current Response object
335
     * @return \Psr\Http\Message\ResponseInterface
336
     * @throws \Exception  if the route callable throws an exception
337
     */
338
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
339
    {
340
        $this->callable = $this->resolveCallable($this->callable);
341
342
        /** @var InvocationStrategyInterface $handler */
343
        $handler = isset($this->container) ? $this->container->get('foundHandler') : new RequestResponse();
344
345
        $newResponse = $handler($this->callable, $request, $response, $this->arguments);
346
347
        if ($newResponse instanceof ResponseInterface) {
348
            // if route callback returns a ResponseInterface, then use it
349
            $response = $newResponse;
350
        } elseif (is_string($newResponse)) {
351
            // if route callback returns a string, then append it to the response
352
            if ($response->getBody()->isWritable()) {
353
                $response->getBody()->write($newResponse);
354
            }
355
        }
356
357
        return $response;
358
    }
359
}
360