These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 rhosocial\base\models\queries\BaseUserQuery; |
||
16 | use Yii; |
||
17 | use yii\base\Model; |
||
18 | use yii\data\ActiveDataProvider; |
||
19 | |||
20 | /** |
||
21 | * Class UserSearchTrait |
||
22 | * This trait should be used in the User model inherited from [[User]]. |
||
23 | * @package rhosocial\user |
||
24 | * @version 1.0 |
||
25 | * @author vistart <[email protected]> |
||
26 | */ |
||
27 | class UserSearch extends Model |
||
28 | { |
||
29 | public $userClass = User::class; |
||
30 | public static function find() |
||
31 | { |
||
32 | $noInit = new static; |
||
33 | /* @var $noInit static */ |
||
34 | $class = $noInit->userClass; |
||
35 | if (empty($class)) { |
||
36 | return null; |
||
37 | } |
||
38 | return $class::find(); |
||
39 | } |
||
40 | public $userAlias = 'u_alias'; |
||
41 | public $profileAlias = 'p_alias'; |
||
42 | public $id; |
||
43 | public $nickname; |
||
44 | public $first_name; |
||
45 | public $last_name; |
||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | public $createdFrom; |
||
50 | protected $createdFromInUtc; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | public $createdTo; |
||
56 | protected $createdToInUtc; |
||
57 | |||
58 | /** |
||
59 | * @var string Gender filter. |
||
60 | */ |
||
61 | public $gf; |
||
62 | |||
63 | /** |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | public function rules() |
||
68 | { |
||
69 | return [ |
||
70 | ['id', 'integer'], |
||
71 | [['nickname', 'first_name', 'last_name'], 'string'], |
||
72 | [['createdFrom', 'createdTo'], 'datetime', 'format' => 'yyyy-MM-dd HH:mm'], |
||
73 | [['createdFrom', 'createdTo'], 'gmdate'], |
||
74 | ['gf', 'in', 'range' => array_keys(Profile::getGenderDescsWithEmpty())], |
||
75 | ['gf', 'default', 'value' => ''], |
||
76 | ]; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Convert time attribute to UTC time. |
||
81 | * @param string $attribute |
||
82 | * @param array $params |
||
83 | * @param mixed $validator |
||
84 | */ |
||
85 | public function gmdate($attribute, $params, $validator) |
||
86 | { |
||
87 | if (isset($this->$attribute)) { |
||
88 | $timestamp = strtotime($this->$attribute); |
||
89 | $this->{$attribute . 'InUtc'} = gmdate('Y-m-d H:i:s', $timestamp); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * |
||
95 | * @return array |
||
96 | */ |
||
97 | public function scenarios() |
||
98 | { |
||
99 | // bypass scenarios() implementation in the parent class |
||
100 | return Model::scenarios(); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Search |
||
105 | * @param array $params |
||
106 | * @return ActiveDataProvider |
||
107 | */ |
||
108 | public function search($params) |
||
109 | { |
||
110 | $query = static::find(); |
||
111 | /* @var $query BaseUserQuery */ |
||
112 | $userClass = $this->userClass; |
||
113 | $query = $query->from("{$userClass::tableName()} {$this->userAlias}"); |
||
114 | $noInitUser = new $userClass; |
||
115 | /* @var $noInitUser User */ |
||
116 | $profileClass = $noInitUser->profileClass; |
||
117 | $noInitProfile = null; |
||
0 ignored issues
–
show
|
|||
118 | if (!empty($profileClass)) { |
||
119 | $query = $query->joinWith(["profile {$this->profileAlias}"]); |
||
120 | } |
||
121 | $dataProvider = new ActiveDataProvider([ |
||
122 | 'query' => $query, |
||
123 | 'pagination' => [ |
||
124 | 'pageParam' => 'user-page', |
||
125 | 'defaultPageSize' => 20, |
||
126 | 'pageSizeParam' => 'user-per-page', |
||
127 | ], |
||
128 | 'sort' => [ |
||
129 | 'sortParam' => 'user-sort', |
||
130 | 'attributes' => [ |
||
131 | 'id', |
||
132 | 'nickname', |
||
133 | 'name' => [ |
||
134 | 'asc' => [$this->profileAlias . '.first_name' => SORT_ASC, $this->profileAlias . '.last_name' => SORT_ASC], |
||
135 | 'desc' => [$this->profileAlias . '.first_name' => SORT_DESC, $this->profileAlias . '.last_name' => SORT_DESC], |
||
136 | 'default' => SORT_DESC, |
||
137 | 'label' => Yii::t('user', 'Name'), |
||
138 | ], |
||
139 | 'gender' => [ |
||
140 | 'asc' => [$this->profileAlias . '.gender' => SORT_ASC], |
||
141 | 'desc' => [$this->profileAlias . '.gender' => SORT_DESC], |
||
142 | 'default' => SORT_ASC, |
||
143 | 'label' => Yii::t('user', 'Gender'), |
||
144 | ], |
||
145 | 'createdAt' => [ |
||
146 | 'asc' => [$this->userAlias . '.created_at' => SORT_ASC], |
||
147 | 'desc' => [$this->userAlias . '.created_at' => SORT_DESC], |
||
148 | 'default' => SORT_ASC, |
||
149 | 'label' => Yii::t('user', 'Creation Time'), |
||
150 | ], |
||
151 | 'updatedAt' => [ |
||
152 | 'asc' => [$this->userAlias . '.updated_at' => SORT_ASC], |
||
153 | 'desc' => [$this->userAlias . '.updated_at' => SORT_DESC], |
||
154 | 'default' => SORT_ASC, |
||
155 | 'label' => Yii::t('user', 'Last Updated Time'), |
||
156 | ], |
||
157 | ], |
||
158 | ], |
||
159 | ]); |
||
160 | |||
161 | if (!($this->load($params) && $this->validate())) { |
||
162 | return $dataProvider; |
||
163 | } |
||
164 | |||
165 | $query = $query->andFilterWhere([ |
||
166 | 'LIKE', $this->userAlias . '.id', $this->id, |
||
167 | ])->andFilterWhere([ |
||
168 | 'LIKE', $this->profileAlias . '.nickname', $this->nickname, |
||
169 | ])->andFilterWhere([ |
||
170 | '>=', $this->userAlias . '.created_at', $this->createdFromInUtc, |
||
171 | ])->andFilterWhere([ |
||
172 | '<=', $this->userAlias . '.created_at', $this->createdToInUtc, |
||
173 | ])->andFilterWhere([ |
||
174 | 'LIKE', $this->profileAlias . '.first_name', $this->first_name, |
||
175 | ])->andFilterWhere([ |
||
176 | 'LIKE', $this->profileAlias . '.last_name', $this->last_name, |
||
177 | ])->andFilterWhere([ |
||
178 | $this->profileAlias . '.gender' => $this->gf, |
||
179 | ]); |
||
180 | $dataProvider->query = $query; |
||
181 | return $dataProvider; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Add `createdFrom` & `createdTo` attributes. |
||
186 | * @return array |
||
187 | */ |
||
188 | public function attributeLabels() |
||
189 | { |
||
190 | $attributeLabels = parent::attributeLabels(); |
||
191 | $attributeLabels['createdFrom'] = Yii::t('user', 'From'); |
||
192 | $attributeLabels['createdTo'] = Yii::t('user', 'To'); |
||
193 | return $attributeLabels; |
||
194 | } |
||
195 | } |
||
196 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.