Completed
Push — master ( a8f028...4e52eb )
by Oscar
05:21
created

Middleware::register()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 51
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 51
rs 8.8981
cc 4
eloc 32
nc 1
nop 1

How to fix   Long Method   

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 Folk\Providers;
4
5
use Fol\{App, ServiceProviderInterface};
6
use Middlewares\Utils\Dispatcher;
7
use Middlewares;
8
use Gettext\{Translator, Translations};
9
10
class Middleware implements ServiceProviderInterface
11
{
12
    public function register(App $app)
13
    {
14
        $app['middleware'] = function (App $app): Dispatcher {
15
            $middleware = [];
16
17
            if ($app->has('users')) {
18
                $middleware[] = new Middlewares\DigestAuthentication($app['users']);
19
            }
20
21
            $middleware[] = new Middlewares\Expires();
22
            $middleware[] = (new Middlewares\ErrorHandler())
23
                ->catchExceptions(false)
24
                ->statusCode(function ($code) {
25
                    return $code > 400 && $code < 600;
26
                })
27
                ->arguments($app);
28
29
            $middleware[] = new Middlewares\BasePath($app->getUri()->getPath());
30
            $middleware[] = new Middlewares\TrailingSlash();
31
            $middleware[] = new Middlewares\ContentType();
32
            $middleware[] = new Middlewares\ContentLanguage(['en', 'gl', 'es']);
33
34
            $middleware[] = function ($request, $next) use ($app) {
35
                $language = $request->getHeaderLine('Accept-Language');
36
                $translator = new Translator();
37
                $translator->loadTranslations(Translations::fromPoFile(dirname(dirname(__DIR__)).'/locales/'.$language.'.po'));
38
                $prev = $translator->register();
39
40
                $app['templates']->addData(['language' => $language]);
41
42
                $response = $next->process($request);
43
44
                if ($prev) {
45
                    $prev->register();
46
                }
47
48
                return $response;
49
            };
50
51
            $middleware[] = (new Middlewares\MethodOverride())
52
                ->parsedBodyParameter('method-override');
53
54
            $middleware[] = (new Middlewares\Reader(dirname(dirname(__DIR__)).'/assets'))
55
                ->continueOnError();
56
57
            $middleware[] = (new Middlewares\AuraRouter($app['router']))
58
                ->arguments($app);
59
60
            return new Dispatcher($middleware);
0 ignored issues
show
Documentation introduced by
$middleware is of type array<integer,object<Mid...iddlewares\AuraRouter>>, but the function expects a array<integer,object<Int...e\MiddlewareInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61
        };
62
    }
63
}
64