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 — master ( b7eb7a...1705c6 )
by Alexander
07:45
created

Counter::renderCountersAtEndOfBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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\web\View;
12
13
/**
14
 * This is the model class for table "seo_counter".
15
 *
16
 * @property integer $id
17
 * @property string $name
18
 * @property string $description
19
 * @property string $code
20
 */
21
class Counter extends \yii\db\ActiveRecord
22
{
23
    const POSITION_AT_END_OF_BODY = 0;
24
    const POSITION_AT_BEGIN_OF_BODY = 1;
25
    const POSITION_AT_HEAD = 2;
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public static function tableName()
31
    {
32
        return '{{%seo_counter}}';
33
    }
34
35
    /**
36
     * @param $counter
37
     * @return string
38
     */
39
    private static function renderCounter($counter)
40
    {
41
        return
42
            "\n<!-- {$counter->name} counter -->\n"
43
            . "{$counter->code}"
44
            . "\n<!-- /{$counter->name} counter -->\n";
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 View Code Duplication
    public function rules()
51
    {
52
        return [
53
            [['description', 'code'], 'string'],
54
            [['code'], 'required'],
55
            [['name'], 'string', 'max' => 255],
56
            [['position'], 'integer']
57
        ];
58
    }
59
60
    public function behaviors()
61
    {
62
        return [
63
            ActiveRecordHelper::className(),
64
        ];
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function attributeLabels()
71
    {
72
        return [
73
            'id' => \Yii::t('app', 'ID'),
74
            'name' => \Yii::t('app', 'Name'),
75
            'description' => \Yii::t('app', 'Description'),
76
            'code' => \Yii::t('app', 'Code'),
77
            'position' => \Yii::t('app', 'Position')
78
        ];
79
    }
80
81
    public static function renderCountersAtHead(Event $event)
82
    {
83
        $counters = self::find()->where(["position" => self::POSITION_AT_HEAD])->all();
84
        foreach ($counters as $counter) {
85
            $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...
86
        }
87
    }
88
89
    public static function renderCountersAtBeginningOfBody(Event $event)
90
    {
91
        static::renderCounters($event, static::POSITION_AT_BEGIN_OF_BODY);
92
    }
93
94
    public static function renderCountersAtEndOfBody(Event $event)
95
    {
96
        static::renderCounters($event, static::POSITION_AT_END_OF_BODY);
97
    }
98
99 View Code Duplication
    public function getPositionVariants()
100
    {
101
        return [
102
            self::POSITION_AT_END_OF_BODY => \Yii::t("app", "End of body tag"),
103
            self::POSITION_AT_BEGIN_OF_BODY => \Yii::t("app", "Beginning of body tag"),
104
            self::POSITION_AT_HEAD => \Yii::t("app", "Inside of head tag")
105
        ];
106
    }
107
108
    public static function renderCounters(Event $event, $mode = self::POSITION_AT_END_OF_BODY)
109
    {
110
        if (
111
            \Yii::$app->request->isAjax === false &&
112
            \Yii::$app->controller->module instanceof BackendModule === false &&
113
            \Yii::$app->controller instanceof BackendController === false
114
        ) {
115
            $cacheKey = \Yii::$app->getModule('seo')->cacheConfig['counterCache']['name'] . $mode;
116
            $counter_str = '';
117
            /* @var $counters Counter[] */
118
            if (false === $counters = \Yii::$app->getCache()->get($cacheKey)) {
119
                $counters = self::find()->where(["position" => $mode])->all();
120
                \Yii::$app->getCache()->set(
121
                    $cacheKey,
122
                    $counters,
123
                    \Yii::$app->getModule('seo')->cacheConfig['counterCache']['expire'],
124
                    new TagDependency(
125
                        [
126
                            'tags' => [
127
                                ActiveRecordHelper::getCommonTag(self::className()),
128
                            ],
129
                        ]
130
                    )
131
                );
132
            }
133
            foreach ($counters as $counter) {
134
                $counter_str .= self::renderCounter($counter);
135
            }
136
            echo $counter_str;
137
        }
138
    }
139
140
    public
141
    function scenarios()
142
    {
143
        return [
144
            'default' => ['id', 'name', 'description', 'code', 'position'],
145
        ];
146
    }
147
148
149
    /**
150
     * Search counters
151
     * @param $params
152
     * @return ActiveDataProvider
153
     */
154
    public
155
    function search(
156
        $params
157
    ) {
158
        $this->load($params);
159
        $query = self::find();
160
        foreach ($this->attributes as $name => $value) {
161
            if (!empty($value)) {
162
                if ($name == 'id') {
163
                    $query->andWhere("`$name` = :$name", [":$name" => $value]);
164
                } else {
165
                    $query->andWhere("`$name` LIKE :$name", [":$name" => "%$value%"]);
166
                }
167
            }
168
        }
169
        $dataProvider = new ActiveDataProvider(
170
            [
171
                'query' => $query,
172
                'pagination' => [
173
                    'pageSize' => 10,
174
                ],
175
            ]
176
        );
177
        return $dataProvider;
178
    }
179
}
180