Completed
Push — master ( 9d1b93...59e655 )
by Misbahul D
02:36
created

User   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 52
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 7 1
A search() 0 26 3
1
<?php
2
3
namespace mdm\admin\models\searchs;
4
5
use Yii;
6
use yii\base\Model;
7
use yii\data\ActiveDataProvider;
8
9
/**
10
 * User represents the model behind the search form about `mdm\admin\models\User`.
11
 */
12
class User extends Model
13
{
14
    public $id;
15
    public $username;
16
    public $email;
17
    public $status;
18
    
19
    /**
20
     * @inheritdoc
21
     */
22
    public function rules()
23
    {
24
        return [
25
            [['id', 'status',], 'integer'],
26
            [['username', 'email'], 'safe'],
27
        ];
28
    }
29
30
    /**
31
     * Creates data provider instance with search query applied
32
     *
33
     * @param array $params
34
     *
35
     * @return ActiveDataProvider
36
     */
37
    public function search($params)
38
    {
39
        /* @var $query \yii\db\ActiveQuery */
40
        $class = Yii::$app->getUser()->identityClass ? : 'mdm\admin\models\User';
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...
41
        $query = $class::find();
42
43
        $dataProvider = new ActiveDataProvider([
44
            'query' => $query,
45
        ]);
46
47
        $this->load($params);
48
        if (!$this->validate()) {
49
            $query->where('1=0');
50
            return $dataProvider;
51
        }
52
53
        $query->andFilterWhere([
54
            'id' => $this->id,
55
            'status' => $this->status,
56
        ]);
57
58
        $query->andFilterWhere(['like', 'username', $this->username])
59
            ->andFilterWhere(['like', 'email', $this->email]);
60
61
        return $dataProvider;
62
    }
63
}
64