GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Controller::computeViewFile()   B
last analyzed

Complexity

Conditions 11
Paths 18

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 7.3166
c 0
b 0
f 0
cc 11
nc 18
nop 2

How to fix   Complexity   

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 app\components;
4
5
use Yii;
6
use app\models\ViewObject;
7
use app\modules\core\events\ViewEvent;
8
use yii\web\ServerErrorHttpException;
9
10
/**
11
 * Class Controller extends default \yii\web\Controller adding some additional functions
12
 * @package app\components
13
 */
14
class Controller extends \yii\web\Controller
15
{
16
    const EVENT_PRE_DECORATOR = 'pre-decorator';
17
    const EVENT_POST_DECORATOR = 'post-decorator';
18
19
    protected function renderDecorator($methodName, $view, $params = [])
20
    {
21
        /** @var \app\components\Response $response */
22
        $response = Yii::$app->response;
23
        if (!empty($response->title)) {
24
            $this->view->title = $response->title;
25
        }
26
        foreach ($response->blocks as $block_name => $value) {
27
            $this->view->blocks[$block_name] = $value;
28
        }
29
        if (!empty($response->meta_description)) {
30
            $this->view->registerMetaTag(
0 ignored issues
show
Bug introduced by
The method registerMetaTag does only exist in yii\web\View, but not in yii\base\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
31
                [
32
                    'name' => 'description',
33
                    'content' => $response->meta_description,
34
                ],
35
                'meta_description'
36
            );
37
        }
38
39
        $preDecoratorEvent = new ViewEvent();
40
        $preDecoratorEvent->viewFile = $view;
41
        $preDecoratorEvent->params = &$params;
42
        $preDecoratorEvent->blocks = &$this->view->blocks;
43
        $this->trigger(self::EVENT_PRE_DECORATOR, $preDecoratorEvent);
44
        if ($preDecoratorEvent->isValid === true) {
45
            $content = $this->getView()->{$methodName}($view, $preDecoratorEvent->params, $this);
46
            $postDecoratorEvent = new ViewEvent();
47
            $postDecoratorEvent->viewFile = $view;
48
            $postDecoratorEvent->params = &$preDecoratorEvent->params;
49
            $postDecoratorEvent->output = &$content;
50
            $postDecoratorEvent->blocks = &$this->view->blocks;
51
            $this->trigger(self::EVENT_POST_DECORATOR, $postDecoratorEvent);
52
            if ($postDecoratorEvent->isValid === true) {
53
                return $methodName === 'render'
54
                    ? $this->renderContent($postDecoratorEvent->output)
55
                    : $postDecoratorEvent->output;
56
            }
57
        }
58
        throw new ServerErrorHttpException("Error rendering output");
59
    }
60
61
    /**
62
     * @param \yii\db\ActiveRecord $model
63
     * @param string $defaultView
64
     * @return string
65
     */
66
    public function computeViewFile($model, $defaultView = '')
67
    {
68
        if (Yii::$app->response->view_id !== null) {
69
            $view = \app\models\View::getViewById(Yii::$app->response->view_id);
70
            if (!is_null($view)) {
71
                if ($view === 'default') {
72
                    $view =  ViewObject::getViewByModel($model->parent);
73
                }
74
                return $view === null ? $defaultView : $view;
75
            }
76
        }
77
        if (is_null($model)) {
78
            return $defaultView;
79
        }
80
81
        do {
82
            $view = ViewObject::getViewByModel($model);
83
            if (is_null($view) || $view == 'default') {
84
                $view = ViewObject::getViewByModel($model->parent, true);
85
            }
86
            if (!is_null($view)) {
87
                return $view === 'default' ? $defaultView : $view;
88
            }
89
            $model = $model->parent;
90
        } while (!is_null($model));
91
92
        return $defaultView;
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98
    public function render($view, $params = [])
99
    {
100
        return $this->renderDecorator('render', $view, $params);
101
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106
    public function renderAjax($view, $params = [])
107
    {
108
        return $this->renderDecorator('renderAjax', $view, $params);
109
    }
110
}
111