Completed
Push — master ( c22208...64acfb )
by Igor
04:17
created

Tag::checkAccess()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 3
eloc 3
nc 4
nop 0
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use app\models\query\TagQuery;
7
8
/**
9
 * This is the model class for table "tag".
10
 *
11
 * @property integer $id
12
 * @property string $user_id
13
 * @property string $title
14
 * @property integer $count
15
 */
16
class Tag extends \yii\db\ActiveRecord
17
{
18
    /**
19
     * @inheritdoc
20
     */
21
    public static function tableName()
22
    {
23
        return 'tag';
24
    }
25
26
    /**
27
     * @inheritdoc
28
     */
29
    public function rules()
30
    {
31
        return [
32
            ['title', 'trim'],
33
            ['title', 'required'],
34
            ['title', 'unique'],
35
            ['title', 'string', 'max' => 255],
36
37
            ['count', 'integer'],
38
            ['count', 'default', 'value' => 0]
39
        ];
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function attributeLabels()
46
    {
47
        return [
48
            'id' => Yii::t('app', 'ID'),
49
            'title' => Yii::t('app', 'Title'),
50
            'count' => Yii::t('app', 'Amount'),
51
        ];
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function beforeSave($insert)
58
    {
59
        if (parent::beforeSave($insert)) {
60
            if ($insert) {
61
                if (!Yii::$app instanceof \yii\console\Application) {
62
                    $this->user_id = Yii::$app->user->id;
63
                }
64
            }
65
66
            return true;
67
        }
68
69
        return false; // @codeCoverageIgnore
70
    }
71
72
    /**
73
     * @inheritdoc
74
     * @return TagQuery
75
     */
76
    public static function find()
77
    {
78
        return new TagQuery(get_called_class());
79
    }
80
81
    /**
82
     * Check access
83
     *
84
     * @return bool
85
     */
86
    public function checkAccess()
87
    {
88
        $isSuperUser = !Yii::$app->getUser()->getIsGuest() && Yii::$app->getUser()->getIdentity()->isSuperUser();
0 ignored issues
show
Bug introduced by
The method getUser does only exist in yii\web\Application, but not in yii\console\Application.

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...
89
        return $isSuperUser || Yii::$app->getUser()->id === $this->user_id;
90
    }
91
}
92