Conditions | 9 |
Paths | 1 |
Total Lines | 186 |
Code Lines | 106 |
Lines | 12 |
Ratio | 6.45 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
148 | public function rules() |
||
149 | { |
||
150 | return [ |
||
151 | [ |
||
152 | ['id', 'url', 'price', 'currencyId', 'categoryId'], |
||
153 | 'required', |
||
154 | ], |
||
155 | [ |
||
156 | ['id', 'cbid', 'bid', 'fee', 'minQuantity'], |
||
157 | 'integer', |
||
158 | 'min' => 0, |
||
159 | 'max' => self::NUMBER_LIMIT, |
||
160 | ], |
||
161 | [ |
||
162 | ['url'], |
||
163 | 'url', |
||
164 | ], |
||
165 | [ |
||
166 | ['url'], |
||
167 | 'string', |
||
168 | 'max' => 512, |
||
169 | ], |
||
170 | [ |
||
171 | ['price', 'oldprice'], |
||
172 | 'double', |
||
173 | ], |
||
174 | [ |
||
175 | 'oldprice', |
||
176 | function ($attribute, $value) { |
||
177 | if ($this->$attribute <= $this->price) { |
||
178 | $this->addError($attribute, 'Old price must be greater than price'); |
||
179 | } |
||
180 | } |
||
181 | ], |
||
182 | [ |
||
183 | [ |
||
184 | 'currencyId', |
||
185 | ], |
||
186 | 'in', |
||
187 | 'range' => [ |
||
188 | 'RUR', |
||
189 | 'RUB', |
||
190 | 'UAH', |
||
191 | 'BYR', |
||
192 | 'KZT', |
||
193 | 'USD', |
||
194 | 'EUR' |
||
195 | ], |
||
196 | ], |
||
197 | [ |
||
198 | ['categoryId'], |
||
199 | 'integer', |
||
200 | 'min' => 0, |
||
201 | 'max' => 999999999999999999, |
||
202 | ], |
||
203 | [ |
||
204 | ['pictures'], |
||
205 | function ($attribute, $params) { |
||
206 | if (count($this->pictures) > 10) { |
||
207 | $this->addError('pictures', 'maximum 10 pictures'); |
||
208 | } |
||
209 | } |
||
210 | ], |
||
211 | [ |
||
212 | ['pictures'], |
||
213 | 'each', |
||
214 | 'rule' => ['string', 'max' => 512], |
||
215 | ], |
||
216 | [ |
||
217 | ['pictures'], |
||
218 | 'each', |
||
219 | 'rule' => ['url'] |
||
220 | ], |
||
221 | [ |
||
222 | ['delivery', 'pickup', 'available', 'store', 'manufacturer_warranty', 'downloadable', 'adult',], |
||
223 | 'in', |
||
224 | 'range' => [ |
||
225 | 'true', |
||
226 | 'false' |
||
227 | ] |
||
228 | ], |
||
229 | [ |
||
230 | ['deliveryOptions'], |
||
231 | function ($attribute, $params) { |
||
232 | $value = $this->$attribute; |
||
233 | if (empty($value)) { |
||
234 | if ($this->delivery === 'true') { |
||
235 | $this->addError($attribute, 'Delivery options is required while delivery equal \'true\''); |
||
236 | } |
||
237 | } else { |
||
238 | if (count($value) > 5) { |
||
239 | $this->addError($attribute, 'Delivery options count greater whan 5'); |
||
240 | } |
||
241 | } |
||
242 | }, |
||
243 | 'skipOnEmpty' => false, |
||
244 | ], |
||
245 | [ |
||
246 | ['outlets'], |
||
247 | 'safe', |
||
248 | ], |
||
249 | [ |
||
250 | ['description'], |
||
251 | 'string', |
||
252 | 'max' => 3000, |
||
253 | ], |
||
254 | [ |
||
255 | ['sales_notes'], |
||
256 | 'string', |
||
257 | 'max' => 50, |
||
258 | ], |
||
259 | [ |
||
260 | ['stepQuantity'], |
||
261 | 'integer', |
||
262 | 'min' => 1, |
||
263 | 'max' => self::NUMBER_LIMIT, |
||
264 | ], |
||
265 | [ |
||
266 | ['barcode'], |
||
267 | 'string', |
||
268 | 'max' => 250, |
||
269 | ], |
||
270 | // [ |
||
271 | // ['age'], |
||
272 | // 'in', |
||
273 | // 'range' => [ |
||
274 | // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 |
||
275 | // ], |
||
276 | // ], |
||
277 | [ |
||
278 | ['cpa'], |
||
279 | 'integer', |
||
280 | 'min' => 0, |
||
281 | 'max' => 1, |
||
282 | ], |
||
283 | [ |
||
284 | ['params'], |
||
285 | 'each', |
||
286 | 'rule' => ['string'] |
||
287 | ], |
||
288 | [ |
||
289 | ['expiry'], |
||
290 | 'date', |
||
291 | 'format' => 'yyyy-MM-ddTHH:mm', |
||
292 | ], |
||
293 | [ |
||
294 | ['weight'], |
||
295 | 'number', |
||
296 | 'numberPattern' => '/^\[0-9]*\.?[0-9]+\s*$/' |
||
297 | ], |
||
298 | [ |
||
299 | 'dimensionsValues', |
||
300 | 'each', |
||
301 | 'rule' => ['number', 'numberPattern' => '/^[0-9]*\.?[0-9]+\s*$/'], |
||
302 | ], |
||
303 | [ |
||
304 | 'dimensionsValues', |
||
305 | View Code Duplication | function ($attribute, $params) { |
|
306 | $count = count($this->$attribute); |
||
307 | if ($count && $count != 3) { |
||
308 | $this->addError($attribute, 'dimensions must have 3 values'); |
||
309 | } |
||
310 | } |
||
311 | ], |
||
312 | [ |
||
313 | 'recValues', |
||
314 | 'each', |
||
315 | 'rule' => ['number', 'min' => 0, 'max' => self::NUMBER_LIMIT], |
||
316 | ], |
||
317 | [ |
||
318 | 'recValues', |
||
319 | View Code Duplication | function ($attribute, $params) { |
|
320 | $count = count($this->$attribute); |
||
321 | if ($count > 30) { |
||
322 | $this->addError($attribute, 'maximum 30 recommended offers'); |
||
323 | } |
||
324 | } |
||
325 | ], |
||
326 | [ |
||
327 | ['group_id'], |
||
328 | 'integer', |
||
329 | 'min' => 0, |
||
330 | 'max' => 999999999, |
||
331 | ], |
||
332 | ]; |
||
333 | } |
||
334 | |||
449 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.