Completed
Push — master ( 621f22...aff4d6 )
by Oscar
04:35
created

Middleware   B

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 16

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 16
dl 0
loc 56
rs 8.4614

1 Method

Rating   Name   Duplication   Size   Complexity  
B getServices() 0 53 4
1
<?php
2
3
namespace Folk\Providers;
4
5
use Fol\App;
6
use Interop\Container\ServiceProvider;
7
use Middleland\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