Completed
Push — master ( 42b0e2...396baf )
by vistart
03:52
created

UserProfileSearchTrait::search()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 65
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 65
rs 9.3571
cc 3
eloc 51
nc 2
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user;
14
15
use Yii;
16
use yii\base\Model;
17
use yii\data\ActiveDataProvider;
18
19
/**
20
 * @version 1.0
21
 * @author vistart <[email protected]>
22
 */
23
trait UserProfileSearchTrait
24
{
25
    /**
26
     * @var string 
27
     */
28
    public $createdFrom;
29
    protected $createdFromInUtc;
30
31
    /**
32
     * @var string 
33
     */
34
    public $createdTo;
35
    protected $createdToInUtc;
36
37
    /**
38
     * @var string Gender filter.
39
     */
40
    public $gf;
41
42
    /**
43
     * 
44
     * @return array
45
     */
46
    public function rules()
47
    {
48
        return [
49
            ['id', 'integer'],
50
            [['nickname', 'first_name', 'last_name'], 'string'],
51
            [['createdFrom', 'createdTo'], 'datetime', 'format' => 'yyyy-MM-dd HH:mm'],
52
            [['createdFrom', 'createdTo'], 'gmdate'],
53
            ['gf', 'in', 'range' => array_keys(Profile::getGenderDescsWithEmpty())],
54
            ['gf', 'default', 'value' => ''],
55
        ];
56
    }
57
58
    /**
59
     * Convert time attribute to UTC time.
60
     * @param string $attribute
61
     * @param array $params
62
     * @param mixed $validator
63
     */
64
    public function gmdate($attribute, $params, $validator)
0 ignored issues
show
Unused Code introduced by
The parameter $params 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...
Unused Code introduced by
The parameter $validator 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...
65
    {
66
        if (isset($this->$attribute)) {
67
            $timestamp = strtotime($this->$attribute);
68
            $this->{$attribute . 'InUtc'} = gmdate('Y-m-d H:i:s', $timestamp);
69
        }
70
    }
71
72
    /**
73
     * 
74
     * @return array
75
     */
76
    public function scenarios()
77
    {
78
        // bypass scenarios() implementation in the parent class
79
        return Model::scenarios();
80
    }
81
82
    /**
83
     * Search
84
     * @param array $params
85
     * @return ActiveDataProvider
86
     */
87
    public function search($params)
88
    {
89
        $query = static::find();
90
        $dataProvider = new ActiveDataProvider([
91
            'query' => $query,
92
            'pagination' => [
93
                'pageParam' => 'user-page',
94
                'defaultPageSize' => 20,
95
                'pageSizeParam' => 'user-per-page',
96
            ],
97
            'sort' => [
98
                'sortParam' => 'user-sort',
99
                'attributes' => [
100
                    'id',
101
                    'nickname',
102
                    'name' => [
103
                        'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
104
                        'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
105
                        'default' => SORT_DESC,
106
                        'label' => Yii::t('user', 'Name'),
107
                    ],
108
                    'gender' => [
109
                        'asc' => ['gender' => SORT_ASC],
110
                        'desc' => ['gender' => SORT_DESC],
111
                        'default' => SORT_ASC,
112
                        'label' => Yii::t('user', 'Gender'),
113
                    ],
114
                    'createdAt' => [
115
                        'asc' => ['created_at' => SORT_ASC],
116
                        'desc' => ['created_at' => SORT_DESC],
117
                        'default' => SORT_ASC,
118
                        'label' => Yii::t('user', 'Creation Time'),
119
                    ],
120
                    'updatedAt' => [
121
                        'asc' => ['updated_at' => SORT_ASC],
122
                        'desc' => ['updated_at' => SORT_DESC],
123
                        'default' => SORT_ASC,
124
                        'label' => Yii::t('user', 'Last Updated Time'),
125
                    ],
126
                ],
127
            ],
128
        ]);
129
130
        if (!($this->load($params) && $this->validate())) {
0 ignored issues
show
Bug introduced by
It seems like load() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like validate() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
131
            return $dataProvider;
132
        }
133
134
        $query = $query->andFilterWhere([
135
            'LIKE', 'id', $this->id,
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
136
        ])->andFilterWhere([
137
            'LIKE', 'nickname', $this->nickname,
0 ignored issues
show
Bug introduced by
The property nickname does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
138
        ])->andFilterWhere([
139
            '>=', 'created_at', $this->createdFromInUtc,
140
        ])->andFilterWhere([
141
            '<=', 'created_at', $this->createdToInUtc,
142
        ])->andFilterWhere([
143
            'LIKE', 'first_name', $this->first_name,
0 ignored issues
show
Bug introduced by
The property first_name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
144
        ])->andFilterWhere([
145
            'LIKE', 'last_name', $this->last_name,
0 ignored issues
show
Bug introduced by
The property last_name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
146
        ])->andFilterWhere([
147
            'gender' => $this->gf,
148
        ]);
149
        $dataProvider->query = $query;
150
        return $dataProvider;
151
    }
152
153
    /**
154
     * Add `createdFrom` & `createdTo` attributes.
155
     * @return array
156
     */
157
    public function attributeLabels()
158
    {
159
        $attributeLabels = parent::attributeLabels();
160
        $attributeLabels['createdFrom'] = Yii::t('user', 'From');
161
        $attributeLabels['createdTo'] = Yii::t('user', 'To');
162
        return $attributeLabels;
163
    }
164
}
165