Test Failed
Push — master ( f1767b...06a3a6 )
by Divine Niiquaye
03:30
created

RouteHandler::__construct()   D

Complexity

Conditions 20
Paths 1

Size

Total Lines 54
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 20.0431

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 20
eloc 32
c 3
b 0
f 0
nc 1
nop 1
dl 0
loc 54
ccs 20
cts 21
cp 0.9524
crap 20.0431
rs 4.1666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\Handler;
19
20
use Biurad\Http\Request;
21
use Flight\Routing\Handlers\RouteHandler as BaseRouteHandler;
22
use Flight\Routing\Route;
23
use Nette\Utils\Callback;
24
use Psr\Http\Message\ServerRequestInterface;
25
use Psr\Http\Server\RequestHandlerInterface;
26
use Rade\DI\Container;
27
use Rade\Event\ControllerEvent;
28
use Rade\Event\RequestEvent;
29
30
/**
31
 * Default route's handler for rade framework.
32
 *
33
 * @author Divine Niiquaye Ibok <[email protected]>
34
 */
35 4
class RouteHandler extends BaseRouteHandler
36
{
37 4
    public function __construct(Container $container)
38 4
    {
39
        $handlerResolver = static function ($handler, array $parameters) use ($container) {
40 4
            $request = $parameters[ServerRequestInterface::class] ?? null;
41 4
42
            if (\is_string($handler)) {
43 4
                if ($container->has($handler)) {
44
                    $handler = $container->get($handler);
45
                } elseif ($container->typed($handler)) {
46
                    $handler = $container->autowired($handler);
47 4
                } elseif (\class_exists($handler)) {
48 4
                    $handler = $container->getResolver()->resolveClass($handler);
49 4
                }
50
            } elseif (\is_array($handler) && \count($handler) === 2) {
51
                $handler[0] = \is_string($handler[0]) ? $container->get($handler[0]) : $handler[0];
52 4
            }
53 4
54
            $ref = !$handler instanceof RequestHandlerInterface ? Callback::toReflection($handler) : new \ReflectionMethod($handler, 'handle');
55
56 4
            if ($ref->getNumberOfParameters() > 0) {
57
                foreach (\class_implements($request) + (\class_parents($request) ?: []) as $psr7Interface) {
58
                    if (\Stringable::class === $psr7Interface) {
59 4
                        continue;
60
                    }
61
                    $parameters[$psr7Interface] = $request;
62
                }
63
                $parameters[\get_class($request)] = $request;
0 ignored issues
show
Bug introduced by
It seems like $request can also be of type null; however, parameter $object of get_class() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
                $parameters[\get_class(/** @scrutinizer ignore-type */ $request)] = $request;
Loading history...
64
                $args = $container->getResolver()->autowireArguments($ref, $parameters);
65 4
            }
66
67 4
            if ($container->has('events.dispatcher')) {
68 4
                $container->get('events.dispatcher')->dispatch($event = new RequestEvent($container, $request));
0 ignored issues
show
Bug introduced by
It seems like $request can also be of type null; however, parameter $request of Rade\Event\RequestEvent::__construct() does only seem to accept Psr\Http\Message\ServerRequestInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
                $container->get('events.dispatcher')->dispatch($event = new RequestEvent($container, /** @scrutinizer ignore-type */ $request));
Loading history...
69
70 4
                if ($event->hasResponse()) {
71 4
                    return $event->getResponse();
72 4
                }
73
74
                $request = $event->getRequest();
75 4
                $container->get('events.dispatcher')->dispatch($event = new ControllerEvent($container, $handler, $ref, $args ?? [], $request));
76
                [$handler, $ref, $args] = [$event->getController(), $event->getReflection(), $event->getArguments()];
77
            }
78 4
79
            if ($request instanceof Request && $container->has('request_stack')) {
80
                $container->get('request_stack')->push($request->getRequest());
81
            }
82
83
            if ($ref instanceof \ReflectionMethod) {
84
                return $ref->isStatic() ? $ref->invokeArgs(null, $args ?? []) : $ref->invokeArgs(!\is_object($handler) ? $handler[0] : $handler, $args ?? []);
85
            }
86
87
            return $ref->invokeArgs($args ?? []);
88
        };
89
90
        parent::__construct($container->get('psr17.factory'), $handlerResolver);
0 ignored issues
show
Bug introduced by
It seems like $container->get('psr17.factory') can also be of type array and mixed; however, parameter $responseFactory of Flight\Routing\Handlers\...eHandler::__construct() does only seem to accept Psr\Http\Message\ResponseFactoryInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
        parent::__construct(/** @scrutinizer ignore-type */ $container->get('psr17.factory'), $handlerResolver);
Loading history...
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    protected function resolveArguments(ServerRequestInterface $request, Route $route): array
97
    {
98
        $route->getArguments();
99
        $parameters[ServerRequestInterface::class] = $request;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$parameters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $parameters = array(); before regardless.
Loading history...
100
101
        return $parameters;
102
    }
103
}
104