Completed
Push — master ( 8d732d...a8c54b )
by Oscar
02:44
created

Middleware::getServices()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 53
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 8.9849
c 0
b 0
f 0
cc 4
eloc 33
nc 1
nop 0

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;
6
use Interop\Container\ServiceProvider;
7
use Middlewares\Utils\Dispatcher;
8
use Middlewares;
9
use Gettext\{Translator, Translations};
10
11
class Middleware implements ServiceProvider
12
{
13
    public function getServices()
14
    {
15
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('middleware...tcher($middleware); }); (array<string,Closure>) is incompatible with the return type declared by the interface Interop\Container\ServiceProvider::getServices of type callable[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
16
            'middleware' => function (App $app): Dispatcher {
17
                $middleware = [];
18
19
                if ($app->has('users')) {
20
                    $middleware[] = new Middlewares\DigestAuthentication($app->get('users'));
21
                }
22
23
                $middleware[] = new Middlewares\Expires();
24
                $middleware[] = (new Middlewares\ErrorHandler())
25
                    ->catchExceptions(false)
26
                    ->statusCode(function ($code) {
27
                        return $code > 400 && $code < 600;
28
                    })
29
                    ->arguments($app);
30
31
                $middleware[] = new Middlewares\BasePath($app->getUri()->getPath());
32
                $middleware[] = new Middlewares\TrailingSlash();
33
                $middleware[] = new Middlewares\ContentType();
34
                $middleware[] = new Middlewares\ContentLanguage(['en', 'gl', 'es']);
35
36
                $middleware[] = function ($request, $next) use ($app) {
37
                    $language = $request->getHeaderLine('Accept-Language');
38
                    $translator = new Translator();
39
                    $translator->loadTranslations(Translations::fromPoFile(dirname(dirname(__DIR__)).'/locales/'.$language.'.po'));
40
                    $prev = $translator->register();
41
42
                    $app->get('templates')->addData(['language' => $language]);
43
44
                    $response = $next->process($request);
45
46
                    if ($prev) {
47
                        $prev->register();
48
                    }
49
50
                    return $response;
51
                };
52
53
                $middleware[] = (new Middlewares\MethodOverride())
54
                    ->parsedBodyParameter('method-override');
55
56
                $middleware[] = (new Middlewares\Reader(dirname(dirname(__DIR__)).'/assets'))
57
                    ->continueOnError();
58
59
                $middleware[] = (new Middlewares\AuraRouter($app->get('router')))
60
                    ->arguments($app);
61
62
                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...
63
            }
64
        ];
65
    }
66
}
67