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 (#1963)
by Glenn
04:23 queued 01:07
created

Route::setCallable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Slim Framework (http://slimframework.com)
4
 *
5
 * @link      https://github.com/slimphp/Slim
6
 * @copyright Copyright (c) 2011-2016 Josh Lockhart
7
 * @license   https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
8
 */
9
namespace Slim;
10
11
use Exception;
12
use InvalidArgumentException;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Message\ResponseInterface;
15
use Slim\Exception\SlimException;
16
use Slim\Handlers\Strategies\RequestResponse;
17
use Slim\Interfaces\InvocationStrategyInterface;
18
use Slim\Interfaces\RouteInterface;
19
20
/**
21
 * Route
22
 */
23
class Route extends Routable implements RouteInterface
24
{
25
    use MiddlewareAwareTrait;
26
27
    /**
28
     * HTTP methods supported by this route
29
     *
30
     * @var string[]
31
     */
32
    protected $methods = [];
33
34
    /**
35
     * Route identifier
36
     *
37
     * @var string
38
     */
39
    protected $identifier;
40
41
    /**
42
     * Route name
43
     *
44
     * @var null|string
45
     */
46
    protected $name;
47
48
    /**
49
     * Parent route groups
50
     *
51
     * @var RouteGroup[]
52
     */
53
    protected $groups;
54
55
    private $finalized = false;
56
57
    /**
58
     * Output buffering mode
59
     *
60
     * One of: false, 'prepend' or 'append'
61
     *
62
     * @var boolean|string
63
     */
64
    protected $outputBuffering = 'append';
65
66
    /**
67
     * Route parameters
68
     *
69
     * @var array
70
     */
71
    protected $arguments = [];
72
73
    /**
74
     * The callable payload
75
     *
76
     * @var callable
77
     */
78
    protected $callable;
79
80
    /**
81
     * Create new route
82
     *
83
     * @param string|string[]   $methods The route HTTP methods
84
     * @param string            $pattern The route pattern
85
     * @param callable          $callable The route callable
86
     * @param RouteGroup[]      $groups The parent route groups
87
     * @param int               $identifier The route identifier
88
     */
89
    public function __construct($methods, $pattern, $callable, $groups = [], $identifier = 0)
90
    {
91
        $this->methods  = is_string($methods) ? [$methods] : $methods;
92
        $this->pattern  = $pattern;
93
        $this->callable = $callable;
94
        $this->groups   = $groups;
95
        $this->identifier = 'route' . $identifier;
96
    }
97
98
    /**
99
     * Finalize the route in preparation for dispatching
100
     */
101
    public function finalize()
102
    {
103
        if ($this->finalized) {
104
            return;
105
        }
106
107
        $groupMiddleware = [];
108
        foreach ($this->getGroups() as $group) {
109
            $groupMiddleware = array_merge($group->getMiddleware(), $groupMiddleware);
110
        }
111
112
        $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...
113
114
        foreach ($this->getMiddleware() as $middleware) {
115
            $this->addMiddleware($middleware);
116
        }
117
118
        $this->finalized = true;
119
    }
120
121
    /**
122
     * Get route callable
123
     *
124
     * @return callable
125
     */
126
    public function getCallable()
127
    {
128
        return $this->callable;
129
    }
130
131
    /**
132
     * This method enables you to override the Route's callable
133
     *
134
     * @param string|\Closure $callable
135
     */
136
    public function setCallable($callable)
137
    {
138
        $this->callable = $callable;
139
    }
140
141
    /**
142
     * Get route methods
143
     *
144
     * @return string[]
145
     */
146
    public function getMethods()
147
    {
148
        return $this->methods;
149
    }
150
151
    /**
152
     * Get parent route groups
153
     *
154
     * @return RouteGroup[]
155
     */
156
    public function getGroups()
157
    {
158
        return $this->groups;
159
    }
160
161
    /**
162
     * Get route name
163
     *
164
     * @return null|string
165
     */
166
    public function getName()
167
    {
168
        return $this->name;
169
    }
170
171
    /**
172
     * Get route identifier
173
     *
174
     * @return string
175
     */
176
    public function getIdentifier()
177
    {
178
        return $this->identifier;
179
    }
180
181
    /**
182
     * Get output buffering mode
183
     *
184
     * @return boolean|string
185
     */
186
    public function getOutputBuffering()
187
    {
188
        return $this->outputBuffering;
189
    }
190
191
    /**
192
     * Set output buffering mode
193
     *
194
     * One of: false, 'prepend' or 'append'
195
     *
196
     * @param boolean|string $mode
197
     *
198
     * @throws InvalidArgumentException If an unknown buffering mode is specified
199
     */
200
    public function setOutputBuffering($mode)
201
    {
202
        if (!in_array($mode, [false, 'prepend', 'append'], true)) {
203
            throw new InvalidArgumentException('Unknown output buffering mode');
204
        }
205
        $this->outputBuffering = $mode;
206
    }
207
208
    /**
209
     * Set route name
210
     *
211
     * @param string $name
212
     *
213
     * @return self
214
     *
215
     * @throws InvalidArgumentException if the route name is not a string
216
     */
217
    public function setName($name)
218
    {
219
        if (!is_string($name)) {
220
            throw new InvalidArgumentException('Route name must be a string');
221
        }
222
        $this->name = $name;
223
        return $this;
224
    }
225
226
    /**
227
     * Set a route argument
228
     *
229
     * @param string $name
230
     * @param string $value
231
     *
232
     * @return self
233
     */
234
    public function setArgument($name, $value)
235
    {
236
        $this->arguments[$name] = $value;
237
        return $this;
238
    }
239
240
    /**
241
     * Replace route arguments
242
     *
243
     * @param array $arguments
244
     *
245
     * @return self
246
     */
247
    public function setArguments(array $arguments)
248
    {
249
        $this->arguments = $arguments;
250
        return $this;
251
    }
252
253
    /**
254
     * Retrieve route arguments
255
     *
256
     * @return array
257
     */
258
    public function getArguments()
259
    {
260
        return $this->arguments;
261
    }
262
263
    /**
264
     * Retrieve a specific route argument
265
     *
266
     * @param string $name
267
     * @param mixed $default
268
     *
269
     * @return mixed
270
     */
271
    public function getArgument($name, $default = null)
272
    {
273
        if (array_key_exists($name, $this->arguments)) {
274
            return $this->arguments[$name];
275
        }
276
        return $default;
277
    }
278
279
    /********************************************************************************
280
     * Route Runner
281
     *******************************************************************************/
282
283
    /**
284
     * Prepare the route for use
285
     *
286
     * @param ServerRequestInterface $request
287
     * @param array $arguments
288
     */
289
    public function prepare(ServerRequestInterface $request, array $arguments)
290
    {
291
        // Add the arguments
292
        foreach ($arguments as $k => $v) {
293
            $this->setArgument($k, $v);
294
        }
295
    }
296
297
    /**
298
     * Run route
299
     *
300
     * This method traverses the middleware stack, including the route's callable
301
     * and captures the resultant HTTP response object. It then sends the response
302
     * back to the Application.
303
     *
304
     * @param ServerRequestInterface $request
305
     * @param ResponseInterface      $response
306
     *
307
     * @return ResponseInterface
308
     */
309
    public function run(ServerRequestInterface $request, ResponseInterface $response)
310
    {
311
        // Finalise route now that we are about to run it
312
        $this->finalize();
313
314
        // Traverse middleware stack and fetch updated response
315
        return $this->callMiddlewareStack($request, $response);
316
    }
317
318
    /**
319
     * Dispatch route callable against current Request and Response objects
320
     *
321
     * This method invokes the route object's callable. If middleware is
322
     * registered for the route, each callable middleware is invoked in
323
     * the order specified.
324
     *
325
     * @param ServerRequestInterface $request  The current Request object
326
     * @param ResponseInterface      $response The current Response object
327
     * @return \Psr\Http\Message\ResponseInterface
328
     * @throws \Exception  if the route callable throws an exception
329
     */
330
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
331
    {
332
        $this->callable = $this->resolveCallable($this->callable);
333
334
        /** @var InvocationStrategyInterface $handler */
335
        $handler = isset($this->container) ? $this->container->get('foundHandler') : new RequestResponse();
336
337
        // invoke route callable
338
        if ($this->outputBuffering === false) {
339
            $newResponse = $handler($this->callable, $request, $response, $this->arguments);
340
        } else {
341
            try {
342
                ob_start();
343
                $newResponse = $handler($this->callable, $request, $response, $this->arguments);
344
                $output = ob_get_clean();
345
            } catch (Exception $e) {
346
                ob_end_clean();
347
                throw $e;
348
            }
349
        }
350
351
        if ($newResponse instanceof ResponseInterface) {
352
            // if route callback returns a ResponseInterface, then use it
353
            $response = $newResponse;
354
        } elseif (is_string($newResponse)) {
355
            // if route callback returns a string, then append it to the response
356
            if ($response->getBody()->isWritable()) {
357
                $response->getBody()->write($newResponse);
358
            }
359
        }
360
361
        if (!empty($output) && $response->getBody()->isWritable()) {
362
            if ($this->outputBuffering === 'prepend') {
363
                // prepend output buffer content
364
                $body = new Http\Body(fopen('php://temp', 'r+'));
365
                $body->write($output . $response->getBody());
366
                $response = $response->withBody($body);
367
            } elseif ($this->outputBuffering === 'append') {
368
                // append output buffer content
369
                $response->getBody()->write($output);
370
            }
371
        }
372
373
        return $response;
374
    }
375
}
376