Banner::setCountViews()   A
last analyzed

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
 * @link https://github.com/yiimaker/yii2-banner
4
 * @copyright Copyright (c) 2017 Yii Maker
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace ymaker\banner\frontend\widgets;
9
10
use yii\base\InvalidConfigException;
11
use yii\base\Widget;
12
use ymaker\banner\common\models\entities\Banner as BannerEntity;
13
14
/**
15
 * Renders banner by slug.
16
 *
17
 * @author Vladimir Kuprienko <[email protected]>
18
 * @since 1.0
19
 */
20
class Banner extends Widget
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $slug;
26
    /**
27
     * @var bool If set `true` - views counter will be updated.
28
     */
29
    protected $countViews = true;
30
    /**
31
     * @var BannerEntity|null
32
     */
33
    protected $model;
34
35
36
    /**
37
     * @param string $slug
38
     */
39
    public function setSlug($slug)
40
    {
41
        $this->slug = $slug;
42
    }
43
44
    /**
45
     * @param bool $countViews
46
     */
47
    public function setCountViews($countViews)
48
    {
49
        $this->countViews = $countViews;
50
    }
51
52
    /**
53
     * @inheritdoc
54
     * @throws InvalidConfigException
55
     */
56
    public function init()
57
    {
58
        if (empty($this->slug)) {
59
            throw new InvalidConfigException('You should set a slug');
60
        }
61
62
        $this->model = BannerEntity::find()
0 ignored issues
show
Documentation Bug introduced by
It seems like \ymaker\banner\common\mo...'translations'))->one() can also be of type object<yii\db\ActiveRecord> or array. However, the property $model is declared as type object<ymaker\banner\com...s\entities\Banner>|null. 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...
63
            ->where([
64
                'slug' => $this->slug,
65
                'published' => true,
66
            ])
67
            ->with(['translations'])
68
            ->one();
69
70
        parent::init();
71
    }
72
73
    /**
74
     * @inheritdoc
75
     * @return string|null
76
     */
77
    public function run()
78
    {
79
        if (empty($this->model)) {
80
            return null;
81
        }
82
        if ($this->countViews) {
83
            $this->model->incrementViewsCounter();
84
        }
85
86
        return $this->render('banner', [
87
            'model' => $this->model,
88
        ]);
89
    }
90
}
91