Completed
Push — master ( b4d6a5...352f6d )
by Basil
02:42
created

Application   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 46
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handleRequest() 0 18 4
A coreComponents() 0 16 1
1
<?php
2
3
namespace luya\web;
4
5
use Yii;
6
use luya\traits\ApplicationTrait;
7
use yii\web\ForbiddenHttpException;
8
9
/**
10
 * LUYA Web Application.
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
 * @property \luya\admin\components\Jwt $jwt The admin JWT handler component, if enabled in the Admin module.
20
 * @property \yii\queue\db\Queue $adminqueue The yii queue component configured for the Admin module.
21
 *
22
 * @author Basil Suter <[email protected]>
23
 * @since 1.0.0
24
 */
25
class Application extends \yii\web\Application
26
{
27
    use ApplicationTrait;
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function handleRequest($request)
33
    {
34
        if ($this->ensureSecureConnection && !$request->isSecureConnection) {
35
            throw new ForbiddenHttpException("Insecure connection is not allowed.");
36
        }
37
        
38
        if ($this->ensureSecureConnection) {
39
            // add secure flag to cookie
40
            Yii::$app->request->csrfCookie = ['httpOnly' => true, 'secure' => true];
41
            Yii::$app->session->cookieParams = ['httpOnly' => true, 'secure' => true];
42
            // apply strict, hsts and x-* headers
43
            Yii::$app->response->headers->set('Strict-Transport-Security', 'max-age=31536000');
44
            Yii::$app->response->headers->set('X-XSS-Protection', "1; mode=block");
45
            Yii::$app->response->headers->set('X-Frame-Options', "SAMEORIGIN");
46
        }
47
        
48
        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...
49
    }
50
    
51
    /**
52
     * @inheritdoc
53
     */
54
    public function coreComponents()
55
    {
56
        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...
57
            'request' => ['class' => 'luya\web\Request'],
58
            'errorHandler' => ['class' => 'luya\web\ErrorHandler'],
59
            'urlManager' => ['class' => 'luya\web\UrlManager'],
60
            'view' => ['class' => 'luya\web\View'],
61
            'element' => ['class' => 'luya\web\Element'],
62
            'composition' => ['class' => 'luya\web\Composition'],
63
            'assetManager' => [
64
                'class' => 'luya\web\AssetManager',
65
                'forceCopy' => YII_DEBUG,
66
                'appendTimestamp' => !YII_DEBUG,
67
            ],
68
        ]);
69
    }
70
}
71