Completed
Push — master ( a146dd...34b580 )
by Andrii
15:54 queued 07:39
created

src/actions/ErrorAction.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Health monitoring for Yii2 applications
4
 *
5
 * @link      https://github.com/hiqdev/yii2-monitoring
6
 * @package   yii2-monitoring
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\yii2\monitoring\actions;
12
13
use hiqdev\yii2\monitoring\models\FeedbackForm;
14
use Yii;
15
16
class ErrorAction extends \yii\web\ErrorAction
17
{
18
    protected function getViewRenderParams()
19
    {
20
        return array_merge(parent::getViewRenderParams(), [
0 ignored issues
show
The expression return array_merge(paren...>buildFeedbackForm())); seems to be an array, but some of its elements' types (hiqdev\yii2\monitoring\models\FeedbackForm) are incompatible with the return type of the parent method yii\web\ErrorAction::getViewRenderParams of type array<string,string|Exception>.

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 new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return '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...
21
            'model' => $this->buildFeedbackForm(),
22
        ]);
23
    }
24
25
    private function buildFeedbackForm()
26
    {
27
        $model = new FeedbackForm();
28
29
        if (Yii::$app->hasModule('debug')) {
30
            /** @var \yii\debug\Module $debug */
31
            $debug = Yii::$app->getModule('debug');
32
            $model->session_tag = $debug->logTarget->tag;
33
        }
34
35
        return $model;
36
    }
37
}
38