| Conditions | 52 |
| Paths | 192 |
| Total Lines | 185 |
| Code Lines | 173 |
| Lines | 67 |
| Ratio | 36.22 % |
| 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 |
||
| 261 | protected function parseRule($rule, $ruleName, &$attributeData, $seed) |
||
| 262 | { |
||
| 263 | $faker = Factory::create(); |
||
| 264 | $faker->seed(crc32($seed)); |
||
| 265 | |||
| 266 | $parsedRule = $this->parseStringRule($rule); |
||
| 267 | $parsedRule[0] = $this->normalizeRule($parsedRule[0]); |
||
| 268 | list($rule, $parameters) = $parsedRule; |
||
| 269 | |||
| 270 | switch ($rule) { |
||
| 271 | case 'required': |
||
| 272 | $attributeData['required'] = true; |
||
| 273 | break; |
||
| 274 | case 'accepted': |
||
| 275 | $attributeData['required'] = true; |
||
| 276 | $attributeData['type'] = 'boolean'; |
||
| 277 | $attributeData['value'] = true; |
||
| 278 | break; |
||
| 279 | View Code Duplication | case 'after': |
|
| 280 | $attributeData['type'] = 'date'; |
||
| 281 | $attributeData['description'][] = Description::parse($rule)->with(date(DATE_RFC850, strtotime($parameters[0])))->getDescription(); |
||
| 282 | $attributeData['value'] = date(DATE_RFC850, strtotime('+1 day', strtotime($parameters[0]))); |
||
| 283 | break; |
||
| 284 | View Code Duplication | case 'alpha': |
|
| 285 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 286 | $attributeData['value'] = $faker->word; |
||
| 287 | break; |
||
| 288 | case 'alpha_dash': |
||
| 289 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 290 | break; |
||
| 291 | case 'alpha_num': |
||
| 292 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 293 | break; |
||
| 294 | View Code Duplication | case 'in': |
|
| 295 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 296 | $attributeData['value'] = $faker->randomElement($parameters); |
||
| 297 | break; |
||
| 298 | View Code Duplication | case 'not_in': |
|
| 299 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 300 | $attributeData['value'] = $faker->word; |
||
| 301 | break; |
||
| 302 | View Code Duplication | case 'min': |
|
| 303 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 304 | if (Arr::get($attributeData, 'type') === 'numeric' || Arr::get($attributeData, 'type') === 'integer') { |
||
| 305 | $attributeData['value'] = $faker->numberBetween($parameters[0]); |
||
| 306 | } |
||
| 307 | break; |
||
| 308 | View Code Duplication | case 'max': |
|
| 309 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 310 | if (Arr::get($attributeData, 'type') === 'numeric' || Arr::get($attributeData, 'type') === 'integer') { |
||
| 311 | $attributeData['value'] = $faker->numberBetween(0, $parameters[0]); |
||
| 312 | } |
||
| 313 | break; |
||
| 314 | case 'between': |
||
| 315 | if (! isset($attributeData['type'])) { |
||
| 316 | $attributeData['type'] = 'numeric'; |
||
| 317 | } |
||
| 318 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 319 | $attributeData['value'] = $faker->numberBetween($parameters[0], $parameters[1]); |
||
| 320 | break; |
||
| 321 | View Code Duplication | case 'before': |
|
| 322 | $attributeData['type'] = 'date'; |
||
| 323 | $attributeData['description'][] = Description::parse($rule)->with(date(DATE_RFC850, strtotime($parameters[0])))->getDescription(); |
||
| 324 | $attributeData['value'] = date(DATE_RFC850, strtotime('-1 day', strtotime($parameters[0]))); |
||
| 325 | break; |
||
| 326 | case 'date_format': |
||
| 327 | $attributeData['type'] = 'date'; |
||
| 328 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 329 | $attributeData['value'] = date($parameters[0]); |
||
| 330 | break; |
||
| 331 | case 'different': |
||
| 332 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 333 | break; |
||
| 334 | case 'digits': |
||
| 335 | $attributeData['type'] = 'numeric'; |
||
| 336 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 337 | $attributeData['value'] = ($parameters[0] < 9) ? $faker->randomNumber($parameters[0], true) : substr(mt_rand(100000000, mt_getrandmax()), 0, $parameters[0]); |
||
| 338 | break; |
||
| 339 | case 'digits_between': |
||
| 340 | $attributeData['type'] = 'numeric'; |
||
| 341 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 342 | break; |
||
| 343 | View Code Duplication | case 'file': |
|
| 344 | $attributeData['type'] = 'file'; |
||
| 345 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 346 | break; |
||
| 347 | View Code Duplication | case 'image': |
|
| 348 | $attributeData['type'] = 'image'; |
||
| 349 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 350 | break; |
||
| 351 | case 'json': |
||
| 352 | $attributeData['type'] = 'string'; |
||
| 353 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 354 | $attributeData['value'] = json_encode(['foo', 'bar', 'baz']); |
||
| 355 | break; |
||
| 356 | case 'mimetypes': |
||
| 357 | View Code Duplication | case 'mimes': |
|
| 358 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 359 | break; |
||
| 360 | View Code Duplication | case 'required_if': |
|
| 361 | $attributeData['description'][] = Description::parse($rule)->with($this->splitValuePairs($parameters))->getDescription(); |
||
| 362 | break; |
||
| 363 | View Code Duplication | case 'required_unless': |
|
| 364 | $attributeData['description'][] = Description::parse($rule)->with($this->splitValuePairs($parameters))->getDescription(); |
||
| 365 | break; |
||
| 366 | View Code Duplication | case 'required_with': |
|
| 367 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 368 | break; |
||
| 369 | View Code Duplication | case 'required_with_all': |
|
| 370 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' and '))->getDescription(); |
||
| 371 | break; |
||
| 372 | View Code Duplication | case 'required_without': |
|
| 373 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 374 | break; |
||
| 375 | View Code Duplication | case 'required_without_all': |
|
| 376 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' and '))->getDescription(); |
||
| 377 | break; |
||
| 378 | case 'same': |
||
| 379 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 380 | break; |
||
| 381 | case 'size': |
||
| 382 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 383 | break; |
||
| 384 | View Code Duplication | case 'timezone': |
|
| 385 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 386 | $attributeData['value'] = $faker->timezone; |
||
| 387 | break; |
||
| 388 | case 'exists': |
||
| 389 | $fieldName = isset($parameters[1]) ? $parameters[1] : $ruleName; |
||
| 390 | $attributeData['description'][] = Description::parse($rule)->with([Str::singular($parameters[0]), $fieldName])->getDescription(); |
||
| 391 | break; |
||
| 392 | case 'active_url': |
||
| 393 | $attributeData['type'] = 'url'; |
||
| 394 | $attributeData['value'] = $faker->url; |
||
| 395 | break; |
||
| 396 | case 'regex': |
||
| 397 | $attributeData['type'] = 'string'; |
||
| 398 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 399 | break; |
||
| 400 | case 'boolean': |
||
| 401 | $attributeData['value'] = true; |
||
| 402 | $attributeData['type'] = $rule; |
||
| 403 | break; |
||
| 404 | case 'array': |
||
| 405 | $attributeData['value'] = $faker->word; |
||
| 406 | $attributeData['type'] = $rule; |
||
| 407 | break; |
||
| 408 | case 'date': |
||
| 409 | $attributeData['value'] = $faker->date(); |
||
| 410 | $attributeData['type'] = $rule; |
||
| 411 | break; |
||
| 412 | case 'email': |
||
| 413 | $attributeData['value'] = $faker->safeEmail; |
||
| 414 | $attributeData['type'] = $rule; |
||
| 415 | break; |
||
| 416 | case 'string': |
||
| 417 | $attributeData['value'] = $faker->word; |
||
| 418 | $attributeData['type'] = $rule; |
||
| 419 | break; |
||
| 420 | case 'integer': |
||
| 421 | $attributeData['value'] = $faker->randomNumber(); |
||
| 422 | $attributeData['type'] = $rule; |
||
| 423 | break; |
||
| 424 | case 'numeric': |
||
| 425 | $attributeData['value'] = $faker->randomNumber(); |
||
| 426 | $attributeData['type'] = $rule; |
||
| 427 | break; |
||
| 428 | case 'url': |
||
| 429 | $attributeData['value'] = $faker->url; |
||
| 430 | $attributeData['type'] = $rule; |
||
| 431 | break; |
||
| 432 | case 'ip': |
||
| 433 | $attributeData['value'] = $faker->ipv4; |
||
| 434 | $attributeData['type'] = $rule; |
||
| 435 | break; |
||
| 436 | } |
||
| 437 | |||
| 438 | if ($attributeData['value'] === '') { |
||
| 439 | $attributeData['value'] = $faker->word; |
||
| 440 | } |
||
| 441 | |||
| 442 | if (is_null($attributeData['type'])) { |
||
| 443 | $attributeData['type'] = 'string'; |
||
| 444 | } |
||
| 445 | } |
||
| 446 | |||
| 546 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.