Completed
Push — master ( d129c8...00172d )
by Gabriel
03:56
created

Kernel::renderException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 12
cp 0
rs 9.568
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Nip\Http\Kernel;
4
5
use Exception;
6
use Nip\Application\ApplicationInterface;
0 ignored issues
show
Bug introduced by
The type Nip\Application\ApplicationInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Nip\Dispatcher\ActionDispatcherMiddleware;
0 ignored issues
show
Bug introduced by
The type Nip\Dispatcher\ActionDispatcherMiddleware was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Nip\Http\Kernel\Traits\HandleExceptionsTrait;
0 ignored issues
show
Bug introduced by
The type Nip\Http\Kernel\Traits\HandleExceptionsTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Nip\Http\Response\Response;
10
use Nip\Http\ServerMiddleware\Dispatcher;
11
use Nip\Http\ServerMiddleware\Traits\HasServerMiddleware;
12
use Nip\Request;
13
use Nip\Router\Middleware\RouteResolverMiddleware;
14
use Nip\Router\Router;
15
use Nip\Session\Middleware\StartSession;
0 ignored issues
show
Bug introduced by
The type Nip\Session\Middleware\StartSession was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Psr\Http\Message\RequestInterface;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Symfony\Component\Debug\Exception\FatalThrowableError;
20
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
21
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
22
use Throwable;
23
24
/**
25
 * Class Kernel
26
 * @package Nip\Http\Kernel
27
 */
28
class Kernel implements KernelInterface
29
{
30
    use Traits\HandleExceptions;
31
    use Traits\HasApplication;
32
33
    use HasServerMiddleware;
34
35
    /**
36
     * The router instance.
37
     *
38
     * @var Router
39
     */
40
    protected $router;
41
42
    /**
43
     * The application's route middleware groups.
44
     *
45
     * @var array
46
     */
47
    protected $middlewareGroups = [];
48
49
    /**
50
     * The application's route middleware.
51
     *
52
     * @var array
53
     */
54
    protected $routeMiddleware = [];
55
56
    /**
57
     * Create a new HTTP kernel instance.
58
     *
59
     * @param ApplicationInterface $app
60
     * @param Router $router
61
     */
62
    public function __construct(ApplicationInterface $app, Router $router)
63
    {
64
        $this->app = $app;
65
        $this->router = $router;
66
67
        $this->pushMiddleware(StartSession::class);
68
        $this->pushMiddleware(RouteResolverMiddleware::class);
69
        $this->pushMiddleware(ActionDispatcherMiddleware::class);
70
    }
71
72
    /**
73
     * Handle an incoming HTTP request.
74
     *
75
     * @param SymfonyRequest $request
76
     * @param int $type
77
     * @param bool $catch
78
     * @return ResponseInterface
79
     */
80
    public function handle(SymfonyRequest $request, $type = self::MASTER_REQUEST, $catch = true)
81
    {
82
        try {
83
            $this->getApplication()->share('request', $request);
84
            return $this->handleRaw($request, $type);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->handleRaw($request, $type) returns the type Psr\Http\Message\ResponseInterface which is incompatible with the return type mandated by Symfony\Component\HttpKe...rnelInterface::handle() of Symfony\Component\HttpFoundation\Response.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
Bug introduced by
$request of type Symfony\Component\HttpFoundation\Request is incompatible with the type Psr\Http\Message\ServerRequestInterface expected by parameter $request of Nip\Http\Kernel\Kernel::handleRaw(). ( Ignorable by Annotation )

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

84
            return $this->handleRaw(/** @scrutinizer ignore-type */ $request, $type);
Loading history...
85
        } catch (Exception $e) {
86
            $this->reportException($e);
87
            $response = $this->renderException($request, $e);
88
        } catch (Throwable $e) {
89
            $this->reportException($e = new FatalThrowableError($e));
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Debug\...ion\FatalThrowableError has been deprecated: since Symfony 4.4 ( Ignorable by Annotation )

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

89
            $this->reportException($e = /** @scrutinizer ignore-deprecated */ new FatalThrowableError($e));
Loading history...
90
            $response = $this->renderException($request, $e);
91
        }
92
//        event(new Events\RequestHandled($request, $response));
93
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response also could return the type Psr\Http\Message\ResponseInterface which is incompatible with the return type mandated by Symfony\Component\HttpKe...rnelInterface::handle() of Symfony\Component\HttpFoundation\Response.
Loading history...
94
    }
95
96
    /**
97
     * Handles a request to convert it to a response.
98
     *
99
     * @param ServerRequestInterface $request A Request instance
100
     * @param int $type The type of the request
101
     *
102
     * @return ResponseInterface A Response instance
103
     *
104
     * @throws \LogicException       If one of the listener does not behave as expected
105
     * @throws NotFoundHttpException When controller cannot be found
106
     */
107
    protected function handleRaw(ServerRequestInterface $request, $type = self::MASTER_REQUEST)
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

107
    protected function handleRaw(ServerRequestInterface $request, /** @scrutinizer ignore-unused */ $type = self::MASTER_REQUEST)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
    {
109
        return (
110
        new Dispatcher($this->middleware, $this->getApplication()->getContainer())
111
        )->dispatch($request);
112
    }
113
    /**
114
     * @param Request $request
115
     * @param Response $response
116
     */
117
    public function terminate(RequestInterface $request, ResponseInterface $response)
118
    {
119
        $this->terminateMiddleware($request, $response);
120
        $this->getApplication()->terminate();
0 ignored issues
show
Bug introduced by
The call to Nip\Application\Application::terminate() has too few arguments starting with request. ( Ignorable by Annotation )

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

120
        $this->getApplication()->/** @scrutinizer ignore-call */ terminate();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
121
    }
122
123
    public function postRouting()
124
    {
125
    }
126
}
127