Passed
Push — develop ( 1026fb...945d40 )
by Felipe
04:55
created

PopulateRequestResponse::__invoke()   C

Complexity

Conditions 11
Paths 288

Size

Total Lines 67
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 37
c 1
b 0
f 0
nc 288
nop 3
dl 0
loc 67
rs 5.3833

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
namespace PHPPgAdmin\Middleware;
4
5
/**
6
 * Set the requestobj and responseobj properties of the container
7
 * as the value of $request and $response, which already contain the route
8
 */
9
class PopulateRequestResponse extends Middleware
10
{
11
    use \PHPPgAdmin\Traits\HelperTrait;
12
13
    public function __invoke(
14
        \Psr\Http\Message\ServerRequestInterface $request,
15
        \Psr\Http\Message\ResponseInterface $response,
16
        $next
17
    ) {
18
19
        $container                = $this->container;
20
        $container['requestobj']  = $request;
21
        $container['responseobj'] = $response;
22
23
        $container['server']   = $request->getParam('server');
0 ignored issues
show
Bug introduced by
The method getParam() does not exist on Psr\Http\Message\ServerRequestInterface. It seems like you code against a sub-type of Psr\Http\Message\ServerRequestInterface such as Slim\Http\Request. ( Ignorable by Annotation )

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

23
        /** @scrutinizer ignore-call */ 
24
        $container['server']   = $request->getParam('server');
Loading history...
24
        $container['database'] = $request->getParam('database');
25
        $container['schema']   = $request->getParam('schema');
26
        $misc                  = $container->get('misc');
27
28
        $misc->setHREF();
29
        $misc->setForm();
30
31
        $container->view->offsetSet('METHOD', $request->getMethod());
32
        if ($request->getAttribute('route')) {
33
            $container->view->offsetSet('subject', $request->getAttribute('route')->getArgument('subject'));
34
        }
35
36
        $query_string = $request->getUri()->getQuery();
37
        $container->view->offsetSet('query_string', $query_string);
38
        $path = (SUBFOLDER ? (SUBFOLDER . '/') : '') . $request->getUri()->getPath() . ($query_string ? '?' . $query_string : '');
39
        $container->view->offsetSet('path', $path);
40
41
        $params = $request->getParams();
0 ignored issues
show
Bug introduced by
The method getParams() does not exist on Psr\Http\Message\ServerRequestInterface. It seems like you code against a sub-type of Psr\Http\Message\ServerRequestInterface such as Slim\Http\Request. ( Ignorable by Annotation )

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

41
        /** @scrutinizer ignore-call */ 
42
        $params = $request->getParams();
Loading history...
42
43
        $viewparams = [];
44
45
        foreach ($params as $key => $value) {
46
            if (is_scalar($value)) {
47
                $viewparams[$key] = $value;
48
            }
49
        }
50
51
        if (isset($_COOKIE['IN_TEST'])) {
52
            $in_test = (string) $_COOKIE['IN_TEST'];
53
        } else {
54
            $in_test = '0';
55
        }
56
57
        // remove tabs and linebreaks from query
58
        if (isset($params['query'])) {
59
            $viewparams['query'] = str_replace(["\r", "\n", "\t"], ' ', $params['query']);
60
        }
61
        $container->view->offsetSet('params', $viewparams);
62
        $container->view->offsetSet('in_test', $in_test);
63
64
        if (count($container['errors']) > 0) {
65
            return ($container->haltHandler)($container->requestobj, $container->responseobj, $container['errors'], 412);
66
        }
67
68
        $messages = $container->flash->getMessages();
69
        if (!empty($messages)) {
70
            foreach ($messages as $key => $message) {
71
                \PC::debug($message, 'Flash: ' . $key);
72
            }
73
        }
74
75
        // First execute anything else
76
        $response = $next($request, $response);
77
78
        // Any other request, pass on current response
79
        return $response;
80
    }
81
}
82