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.
Passed
Push — enh/manageCounterRework ( 6461d6 )
by Alexander
07:20
created

Counter::updateInfoForEditable()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 5
nop 1
1
<?php
2
3
namespace app\modules\seo\models;
4
5
use app\backend\BackendModule;
6
use app\backend\components\BackendController;
7
use devgroup\TagDependencyHelper\ActiveRecordHelper;
8
use yii\base\Event;
9
use yii\caching\TagDependency;
10
use yii\data\ActiveDataProvider;
11
use yii\db\ActiveRecord;
12
use yii\web\View;
13
14
/**
15
 * This is the model class for table "seo_counter".
16
 *
17
 * @property integer $id
18
 * @property string $name
19
 * @property string $description
20
 * @property string $code
21
 */
22
class Counter extends ActiveRecord
23
{
24
    const POSITION_AT_END_OF_BODY = 0;
25
    const POSITION_AT_BEGIN_OF_BODY = 1;
26
    const POSITION_AT_HEAD = 2;
27
    const MESSAGE_END_OF_BODY_TAG = "End of body tag";
28
    const MESSAGE_BEGINNING_OF_BODY_TAG = "Beginning of body tag";
29
    const MESSAGE_INSIDE_OF_HEAD_TAG = "Inside of head tag";
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public static function tableName()
35
    {
36
        return '{{%seo_counter}}';
37
    }
38
39
    /**
40
     * @param $counter
41
     * @return string
42
     */
43
    private static function renderCounter($counter)
44
    {
45
        return
46
            "\n<!-- {$counter->name} counter -->\n"
47
            . "{$counter->code}"
48
            . "\n<!-- /{$counter->name} counter -->\n";
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54 View Code Duplication
    public function rules()
55
    {
56
        return [
57
            [['description', 'code'], 'string'],
58
            [['code'], 'required'],
59
            [['name'], 'string', 'max' => 255],
60
            [['position'], 'integer']
61
        ];
62
    }
63
64
    public function behaviors()
65
    {
66
        return [
67
            ActiveRecordHelper::className(),
68
        ];
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 View Code Duplication
    public function attributeLabels()
75
    {
76
        return [
77
            'id' => \Yii::t('app', 'ID'),
78
            'name' => \Yii::t('app', 'Name'),
79
            'description' => \Yii::t('app', 'Description'),
80
            'code' => \Yii::t('app', 'Code'),
81
            'position' => \Yii::t('app', 'Position')
82
        ];
83
    }
84
85
    public static function renderCountersAtHead(Event $event)
86
    {
87
        $counters = self::find()->where(["position" => self::POSITION_AT_HEAD])->all();
88
        foreach ($counters as $counter) {
89
            $event->sender->jsFiles[View::POS_HEAD][] = static::renderCounter($counter);
0 ignored issues
show
Bug introduced by
Since renderCounter() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of renderCounter() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
90
        }
91
    }
92
93
    public static function renderCountersAtBeginningOfBody(Event $event)
94
    {
95
        static::renderCounters($event, static::POSITION_AT_BEGIN_OF_BODY);
96
    }
97
98
    public static function renderCountersAtEndOfBody(Event $event)
99
    {
100
        static::renderCounters($event, static::POSITION_AT_END_OF_BODY);
101
    }
102
103
    /**
104
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
105
     */
106
    public static function getPositionVariants()
107
    {
108
        return [
109
            self::POSITION_AT_END_OF_BODY => \Yii::t("app", self::MESSAGE_END_OF_BODY_TAG),
110
            self::POSITION_AT_BEGIN_OF_BODY => \Yii::t("app", self::MESSAGE_BEGINNING_OF_BODY_TAG),
111
            self::POSITION_AT_HEAD => \Yii::t("app", self::MESSAGE_INSIDE_OF_HEAD_TAG)
112
        ];
113
    }
114
115
    /**
116
     * @param Counter $model
117
     * @return null|string
118
     */
119
    public static function updateInfoForEditable(Counter $model)
120
    {
121
        if ($model === null) {
122
            return null;
123
        }
124
125
        switch($model->position) {
0 ignored issues
show
Documentation introduced by
The property position does not exist on object<app\modules\seo\models\Counter>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
126
            case Counter::POSITION_AT_BEGIN_OF_BODY:
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
127
                $value = self::MESSAGE_BEGINNING_OF_BODY_TAG;
128
                break;
129
            case Counter::POSITION_AT_END_OF_BODY:
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
130
                $value = self::MESSAGE_END_OF_BODY_TAG;
131
                break;
132
            case Counter::POSITION_AT_HEAD:
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
133
                $value = self::MESSAGE_INSIDE_OF_HEAD_TAG;
134
                break;
135
            default:
136
                $value = 'Unexpected value';
137
                break;
138
        }
139
        return \yii\helpers\Html::tag(
140
            'span',
141
            \Yii::t('app', $value)
142
        );
143
    }
144
145
    public static function renderCounters(Event $event, $mode = self::POSITION_AT_END_OF_BODY)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
146
    {
147
        if (
148
            \Yii::$app->request->isAjax === false &&
149
            \Yii::$app->controller->module instanceof BackendModule === false &&
150
            \Yii::$app->controller instanceof BackendController === false
151
        ) {
152
            $cacheKey = \Yii::$app->getModule('seo')->cacheConfig['counterCache']['name'] . $mode;
153
            $counter_str = '';
154
            /* @var $counters Counter[] */
155
            if (false === $counters = \Yii::$app->getCache()->get($cacheKey)) {
156
                $counters = self::find()->where(["position" => $mode])->all();
157
                \Yii::$app->getCache()->set(
158
                    $cacheKey,
159
                    $counters,
160
                    \Yii::$app->getModule('seo')->cacheConfig['counterCache']['expire'],
161
                    new TagDependency(
162
                        [
163
                            'tags' => [
164
                                ActiveRecordHelper::getCommonTag(self::className()),
165
                            ],
166
                        ]
167
                    )
168
                );
169
            }
170
            foreach ($counters as $counter) {
171
                $counter_str .= self::renderCounter($counter);
172
            }
173
            echo $counter_str;
174
        }
175
    }
176
177
    public function scenarios()
178
    {
179
        return [
180
            'default' => ['id', 'name', 'description', 'code', 'position'],
181
        ];
182
    }
183
184
185
    /**
186
     * Search counters
187
     * @param $params
188
     * @return ActiveDataProvider
189
     */
190
    public
191
    function search(
192
        $params
193
    ) {
194
        $this->load($params);
195
        $query = self::find();
196
        foreach ($this->attributes as $name => $value) {
197
            if (!empty($value)) {
198
                if ($name == 'id') {
199
                    $query->andWhere("`$name` = :$name", [":$name" => $value]);
200
                } else {
201
                    $query->andWhere("`$name` LIKE :$name", [":$name" => "%$value%"]);
202
                }
203
            }
204
        }
205
        $dataProvider = new ActiveDataProvider(
206
            [
207
                'query' => $query,
208
                'pagination' => [
209
                    'pageSize' => 10,
210
                ],
211
            ]
212
        );
213
        return $dataProvider;
214
    }
215
}
216