Completed
Push — master ( 5fc968...929a6e )
by Basil
03:12
created

Application::handleRequest()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace luya\web;
4
5
use luya\traits\ApplicationTrait;
6
use yii\web\ForbiddenHttpException;
7
8
/**
9
 * Luya Web Application.
10
 *
11
 *
12
 * @property \luya\cms\Menu $menu Menu component in order to build navigation from CMS module.
13
 * @property \luya\admin\storage\BaseFileSystemStorage $storage Storage component for reading, saving and holding files from the Admin module.
14
 * @property \luya\web\Composition $composition Composition component.
15
 * @property \luya\web\Element $element The element component.
16
 * @property \luya\web\View $view The view component.
17
 * @property \luya\web\Request $request The request component.
18
 * @property \luya\web\ErrorHandler $errorHandler The error handler component.
19
 *
20
 * @author Basil Suter <[email protected]>
21
 * @since 1.0.0
22
 */
23
class Application extends \yii\web\Application
24
{
25
    use ApplicationTrait;
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function handleRequest($request)
31
    {
32
        if ($this->ensureSecureConnection && !$request->isSecureConnection) {
33
            throw new ForbiddenHttpException("Insecure connection is not allowed.");
34
        }
35
        
36
        return parent::handleRequest($request);
0 ignored issues
show
Compatibility introduced by
$request of type object<yii\base\Request> is not a sub-type of object<yii\web\Request>. It seems like you assume a child class of the class yii\base\Request to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
37
    }
38
    
39
    /**
40
     * @inheritdoc
41
     */
42
    public function coreComponents()
43
    {
44
        return array_merge($this->luyaCoreComponents(), [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_merge($this...tamp' => !YII_DEBUG))); (array<string,array<string,string|boolean>>) is incompatible with the return type of the parent method yii\web\Application::coreComponents of type array<string,array<string,string>>.

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...
45
            'request' => ['class' => 'luya\web\Request'],
46
            'errorHandler' => ['class' => 'luya\web\ErrorHandler'],
47
            'urlManager' => ['class' => 'luya\web\UrlManager'],
48
            'view' => ['class' => 'luya\web\View'],
49
            'element' => ['class' => 'luya\web\Element'],
50
            'composition' => ['class' => 'luya\web\Composition'],
51
            'assetManager' => [
52
                'class' => 'luya\web\AssetManager',
53
                'forceCopy' => YII_DEBUG,
54
                'appendTimestamp' => !YII_DEBUG,
55
            ],
56
        ]);
57
    }
58
}
59