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 (#2015)
by Glenn
02:37
created

Route::setEnforceReturn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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