Conditions | 51 |
Paths | 188 |
Total Lines | 185 |
Code Lines | 173 |
Lines | 61 |
Ratio | 32.97 % |
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 |
||
191 | protected function parseRule($rule, $ruleName, &$attributeData, $seed) |
||
192 | { |
||
193 | $faker = Factory::create(); |
||
194 | $faker->seed(crc32($seed)); |
||
195 | |||
196 | $parsedRule = $this->parseStringRule($rule); |
||
197 | $parsedRule[0] = $this->normalizeRule($parsedRule[0]); |
||
198 | list($rule, $parameters) = $parsedRule; |
||
199 | |||
200 | switch ($rule) { |
||
201 | case 'required': |
||
202 | $attributeData['required'] = true; |
||
203 | break; |
||
204 | case 'accepted': |
||
205 | $attributeData['required'] = true; |
||
206 | $attributeData['type'] = 'boolean'; |
||
207 | $attributeData['value'] = true; |
||
208 | break; |
||
209 | View Code Duplication | case 'after': |
|
210 | $attributeData['type'] = 'date'; |
||
211 | $attributeData['description'][] = Description::parse($rule)->with(date(DATE_RFC850, strtotime($parameters[0])))->getDescription(); |
||
212 | $attributeData['value'] = date(DATE_RFC850, strtotime('+1 day', strtotime($parameters[0]))); |
||
213 | break; |
||
214 | View Code Duplication | case 'alpha': |
|
215 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
216 | $attributeData['value'] = $faker->word; |
||
217 | break; |
||
218 | case 'alpha_dash': |
||
219 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
220 | break; |
||
221 | case 'alpha_num': |
||
222 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
223 | break; |
||
224 | View Code Duplication | case 'in': |
|
225 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
226 | $attributeData['value'] = $faker->randomElement($parameters); |
||
227 | break; |
||
228 | View Code Duplication | case 'not_in': |
|
229 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
230 | $attributeData['value'] = $faker->word; |
||
231 | break; |
||
232 | View Code Duplication | case 'min': |
|
233 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
234 | if (Arr::get($attributeData, 'type') === 'numeric' || Arr::get($attributeData, 'type') === 'integer') { |
||
235 | $attributeData['value'] = $faker->numberBetween($parameters[0]); |
||
236 | } |
||
237 | break; |
||
238 | View Code Duplication | case 'max': |
|
239 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
240 | if (Arr::get($attributeData, 'type') === 'numeric' || Arr::get($attributeData, 'type') === 'integer') { |
||
241 | $attributeData['value'] = $faker->numberBetween(0, $parameters[0]); |
||
242 | } |
||
243 | break; |
||
244 | case 'between': |
||
245 | if (! isset($attributeData['type'])) { |
||
246 | $attributeData['type'] = 'numeric'; |
||
247 | } |
||
248 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
249 | $attributeData['value'] = $faker->numberBetween($parameters[0], $parameters[1]); |
||
250 | break; |
||
251 | View Code Duplication | case 'before': |
|
252 | $attributeData['type'] = 'date'; |
||
253 | $attributeData['description'][] = Description::parse($rule)->with(date(DATE_RFC850, strtotime($parameters[0])))->getDescription(); |
||
254 | $attributeData['value'] = date(DATE_RFC850, strtotime('-1 day', strtotime($parameters[0]))); |
||
255 | break; |
||
256 | case 'date_format': |
||
257 | $attributeData['type'] = 'date'; |
||
258 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
259 | $attributeData['value'] = date($parameters[0]); |
||
260 | break; |
||
261 | case 'different': |
||
262 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
263 | break; |
||
264 | case 'digits': |
||
265 | $attributeData['type'] = 'numeric'; |
||
266 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
267 | $attributeData['value'] = $faker->randomNumber($parameters[0], true); |
||
268 | break; |
||
269 | case 'digits_between': |
||
270 | $attributeData['type'] = 'numeric'; |
||
271 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
272 | break; |
||
273 | View Code Duplication | case 'file': |
|
274 | $attributeData['type'] = 'file'; |
||
275 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
276 | break; |
||
277 | View Code Duplication | case 'image': |
|
278 | $attributeData['type'] = 'image'; |
||
279 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
280 | break; |
||
281 | case 'json': |
||
282 | $attributeData['type'] = 'string'; |
||
283 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
284 | $attributeData['value'] = json_encode(['foo', 'bar', 'baz']); |
||
285 | break; |
||
286 | case 'mimetypes': |
||
287 | View Code Duplication | case 'mimes': |
|
288 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
289 | break; |
||
290 | case 'required_if': |
||
291 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
292 | break; |
||
293 | case 'required_unless': |
||
294 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
295 | break; |
||
296 | View Code Duplication | case 'required_with': |
|
297 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
298 | break; |
||
299 | View Code Duplication | case 'required_with_all': |
|
300 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' and '))->getDescription(); |
||
301 | break; |
||
302 | View Code Duplication | case 'required_without': |
|
303 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
304 | break; |
||
305 | View Code Duplication | case 'required_without_all': |
|
306 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' and '))->getDescription(); |
||
307 | break; |
||
308 | case 'same': |
||
309 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
310 | break; |
||
311 | case 'size': |
||
312 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
313 | break; |
||
314 | View Code Duplication | case 'timezone': |
|
315 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
316 | $attributeData['value'] = $faker->timezone; |
||
317 | break; |
||
318 | case 'exists': |
||
319 | $fieldName = isset($parameters[1]) ? $parameters[1] : $ruleName; |
||
320 | $attributeData['description'][] = Description::parse($rule)->with([Str::singular($parameters[0]), $fieldName])->getDescription(); |
||
321 | break; |
||
322 | case 'active_url': |
||
323 | $attributeData['type'] = 'url'; |
||
324 | $attributeData['value'] = $faker->url; |
||
325 | break; |
||
326 | case 'regex': |
||
327 | $attributeData['type'] = 'string'; |
||
328 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
329 | break; |
||
330 | case 'boolean': |
||
331 | $attributeData['value'] = true; |
||
332 | $attributeData['type'] = $rule; |
||
333 | break; |
||
334 | case 'array': |
||
335 | $attributeData['value'] = $faker->word; |
||
336 | $attributeData['type'] = $rule; |
||
337 | break; |
||
338 | case 'date': |
||
339 | $attributeData['value'] = $faker->date(); |
||
340 | $attributeData['type'] = $rule; |
||
341 | break; |
||
342 | case 'email': |
||
343 | $attributeData['value'] = $faker->safeEmail; |
||
344 | $attributeData['type'] = $rule; |
||
345 | break; |
||
346 | case 'string': |
||
347 | $attributeData['value'] = $faker->word; |
||
348 | $attributeData['type'] = $rule; |
||
349 | break; |
||
350 | case 'integer': |
||
351 | $attributeData['value'] = $faker->randomNumber(); |
||
352 | $attributeData['type'] = $rule; |
||
353 | break; |
||
354 | case 'numeric': |
||
355 | $attributeData['value'] = $faker->randomNumber(); |
||
356 | $attributeData['type'] = $rule; |
||
357 | break; |
||
358 | case 'url': |
||
359 | $attributeData['value'] = $faker->url; |
||
360 | $attributeData['type'] = $rule; |
||
361 | break; |
||
362 | case 'ip': |
||
363 | $attributeData['value'] = $faker->ipv4; |
||
364 | $attributeData['type'] = $rule; |
||
365 | break; |
||
366 | } |
||
367 | |||
368 | if ($attributeData['value'] === '') { |
||
369 | $attributeData['value'] = $faker->word; |
||
370 | } |
||
371 | |||
372 | if (is_null($attributeData['type'])) { |
||
373 | $attributeData['type'] = 'string'; |
||
374 | } |
||
375 | } |
||
376 | |||
476 |
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.