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.

SliderWidget   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 9
dl 0
loc 110
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 31 8
A run() 0 34 5
1
<?php
2
3
namespace app\slider;
4
5
use app;
6
use app\models\Slider;
7
use devgroup\TagDependencyHelper\ActiveRecordHelper;
8
use Yii;
9
use yii\base\InvalidConfigException;
10
use yii\base\Widget;
11
use yii\helpers\ArrayHelper;
12
use yii\helpers\Json;
13
14
/**
15
 * Slider widget
16
 * @package app\slider\sliders
17
 */
18
class SliderWidget extends Widget
19
{
20
    /**
21
     * @var string Slider name
22
     */
23
    public $slider_name = null;
24
25
    /**
26
     * @var integer Slider ID
27
     */
28
    public $slider_id = null;
29
30
    /**
31
     * @var Slider|ActiveRecordHelper slider model instance
32
     */
33
    public $slider = null;
34
35
    /**
36
     * @var string View file to render slider widget with - can be redefined by Slider.custom_slider_view_file
37
     */
38
    public $view_file = "slider-widget";
39
40
    /**
41
     * @var string View file for each slide - can be redefined by Slider.custom_slide_view_file
42
     */
43
    public $slide_view_file = "slide";
44
45
    /**
46
     * @var array Params for the JS slider - will be merged with Slider.params
47
     */
48
    public $params = [];
49
50
    /**
51
     * @var string Advanced css classes to apply to this slider - will be appended by Slider.css_class
52
     */
53
    public $css_class = "";
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function init()
59
    {
60
        parent::init();
61
        if ($this->slider_id === null && $this->slider_name === null && $this->slider === null) {
62
            throw new InvalidConfigException("Either slider, slider_id or slider_name should be set.");
63
        }
64
        if ($this->slider === null) {
65
            if ($this->slider_name !== null) {
66
                $this->slider = Yii::$app->cache->get("Slider:name:".$this->slider_name);
67
                if ($this->slider === false) {
68
                    $this->slider = Slider::find()
0 ignored issues
show
Documentation Bug introduced by
It seems like \app\models\Slider::find...s->slider_name))->one() can also be of type object<yii\db\ActiveRecord> or array. However, the property $slider is declared as type object<app\models\Slider...per\ActiveRecordHelper>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
69
                        ->where(['name' => $this->slider_name])
70
                        ->one();
71
                    if ($this->slider !== null) {
72
                        Yii::$app->cache->set(
73
                            "Slider:name:" . $this->slider_name,
74
                            $this->slider,
75
                            86400,
76
                            new \yii\caching\TagDependency([
77
                                'tags' => [
78
                                    $this->slider->objectTag(),
79
                                ]
80
                            ])
81
                        );
82
                    }
83
                }
84
            } else {
85
                $this->slider = Slider::findById($this->slider_id);
0 ignored issues
show
Documentation Bug introduced by
It seems like \app\models\Slider::findById($this->slider_id) of type object<yii\db\ActiveRecord> is incompatible with the declared type object<app\models\Slider...per\ActiveRecordHelper> of property $slider.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
86
            }
87
        }
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93
    public function run()
94
    {
95
        if ($this->slider === null) {
96
            return '<!-- slider model not found -->';
97
        }
98
99
        $viewFile = !empty($this->slider->custom_slider_view_file) ?
100
            $this->slider->custom_slider_view_file :
101
            $this->view_file;
102
103
        $slide_viewFile = !empty($this->slider->custom_slide_view_file) ?
104
            $this->slider->custom_slide_view_file :
105
            $this->slide_view_file;
106
107
        $slider_params = Json::decode($this->slider->params);
108
        if (!is_array($slider_params)) {
109
            $slider_params = [];
110
        }
111
112
        $slider_params = ArrayHelper::merge($this->params, $slider_params);
113
114
        $css_class = $this->css_class . ' ' . $this->slider->css_class;
115
116
        /** @var \app\slider\AbstractSliderWidget $class_name */
117
        $class_name = $this->slider->handler()->slider_widget;
0 ignored issues
show
Bug introduced by
The method handler does only exist in app\models\Slider, but not in devgroup\TagDependencyHelper\ActiveRecordHelper.

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...
118
119
        return $class_name::widget([
120
            'viewFile' => $viewFile,
121
            'slide_viewFile' => $slide_viewFile,
122
            'slider_params' => $slider_params,
123
            'css_class' => $css_class,
124
            'slider' => $this->slider,
125
        ]);
126
    }
127
}
128