Passed
Push — master ( 515328...b241d1 )
by Alexander
02:28
created

BaseWidget   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 14
c 6
b 0
f 2
lcom 2
cbo 7
dl 0
loc 152
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSelector() 0 5 1
A init() 0 14 4
B initJsEvents() 0 24 5
A initDefaults() 0 15 4
1
<?php
2
3
namespace hauntd\vote\widgets;
4
5
use hauntd\vote\assets\VoteAsset;
6
use hauntd\vote\models\VoteAggregate;
7
use hauntd\vote\traits\ModuleTrait;
8
use yii\base\InvalidParamException;
9
use yii\base\Widget;
10
use Yii;
11
12
/**
13
 * @author Alexander Kononenko <[email protected]>
14
 * @package hauntd\vote\widgets
15
 */
16
abstract class BaseWidget extends Widget
17
{
18
    use ModuleTrait;
19
20
    /**
21
     * @var string
22
     */
23
    public $entity;
24
25
    /**
26
     * @var \yii\base\Model|\yii\db\ActiveRecord
27
     */
28
    public $model;
29
30
    /**
31
     * @var null|integer;
32
     */
33
    public $targetId;
34
35
    /**
36
     * @var string
37
     */
38
    public $voteUrl;
39
40
    /**
41
     * @var null|\hauntd\vote\models\VoteAggregate
42
     */
43
    public $aggregateModel;
44
45
    /**
46
     * @var null|integer
47
     */
48
    public $userValue = null;
49
50
    /**
51
     * @var string
52
     */
53
    public $jsBeforeVote;
54
55
    /**
56
     * @var string
57
     */
58
    public $jsAfterVote;
59
60
    /**
61
     * @var string
62
     */
63
    public $jsCodeKey = 'vote';
64
65
    /**
66
     * @var string
67
     */
68
    public $jsErrorVote;
69
70
    /**
71
     * @var string
72
     */
73
    public $jsShowMessage;
74
75
    /**
76
     * @var string
77
     */
78
    public $jsChangeCounters;
79
80
    /**
81
     * @var array
82
     */
83
    public $options = [];
84
85
    /**
86
     * @var string
87
     */
88
    public $viewFile = 'vote';
89
90
    /**
91
     * @param $classes
92
     * @return string
93
     */
94
    public function getSelector($classes)
95
    {
96
        $classes = str_replace(' ', '.', $classes);
97
        return ".{$classes}[data-entity=\"' + entity + '\"][data-target-id=\"' + target  + '\"]";
98
    }
99
100
    /**
101
     * @inheritdoc
102
     * @throws \yii\base\InvalidConfigException
103
     */
104
    public function init()
105
    {
106
        parent::init();
107
108
        if (!isset($this->entity) || !isset($this->model)) {
109
            throw new InvalidParamException(Yii::t('vote', 'Entity and model must be set.'));
110
        }
111
112
        $this->initDefaults();
113
114
        if ($this->getModule()->registerAsset) {
115
            $this->view->registerAssetBundle(VoteAsset::class);
116
        }
117
    }
118
119
    /**
120
     * Initialize with default events.
121
     */
122
    public function initJsEvents()
123
    {
124
        $selector = $this->getSelector($this->options['class']);
125
        if (!isset($this->jsBeforeVote)) {
126
            $this->jsBeforeVote = "
127
                $('$selector button').prop('disabled', 'disabled').addClass('btn-loading');
128
            ";
129
        }
130
        if (!isset($this->jsAfterVote)) {
131
            $this->jsAfterVote = "
132
                $('$selector button').prop('disabled', false).removeClass('btn-loading');
133
            ";
134
        }
135
        if (!isset($this->jsShowMessage)) {
136
            $this->jsShowMessage = "
137
                /** todo **/
138
            ";
139
        }
140
        if (!isset($this->jsErrorVote)) {
141
            $this->jsErrorVote = "
142
                /** todo **/
143
            ";
144
        }
145
    }
146
147
    /**
148
     * Init with default options.
149
     *
150
     * @throws \yii\base\InvalidConfigException
151
     */
152
    public function initDefaults()
153
    {
154
        if (!isset($this->voteUrl)) {
155
            $this->voteUrl = Yii::$app->getUrlManager()->createUrl(['vote/default/vote']);
156
        }
157
        if (!isset($this->targetId)) {
158
            $this->targetId = $this->model->getPrimaryKey();
0 ignored issues
show
Bug introduced by
The method getPrimaryKey does only exist in yii\db\ActiveRecord, but not in yii\base\Model.

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...
159
        }
160
        if (!isset($this->aggregateModel)) {
161
            $this->aggregateModel = VoteAggregate::findOne([
1 ignored issue
show
Documentation Bug introduced by
It seems like \hauntd\vote\models\Vote...d' => $this->targetId)) of type array or boolean is incompatible with the declared type null|object<hauntd\vote\models\VoteAggregate> of property $aggregateModel.

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...
162
                'entity' => $this->getModule()->encodeEntity($this->entity),
163
                'target_id' => $this->targetId,
164
            ]);
165
        }
166
    }
167
}
168