Complex classes like RulesParser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RulesParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class RulesParser |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var FormField |
||
| 20 | */ |
||
| 21 | protected $field; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var FormHelper |
||
| 25 | */ |
||
| 26 | protected $formHelper; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param FormField $field |
||
| 30 | */ |
||
| 31 | 103 | public function __construct(FormField $field) |
|
| 36 | |||
| 37 | /** |
||
| 38 | * Parse a rule for an input into an array of attributes. |
||
| 39 | * |
||
| 40 | * @param string|array $rules |
||
| 41 | * @return array |
||
| 42 | */ |
||
| 43 | 18 | public function parse($rules) |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Check that a checkbox is accepted. Needs yes, on, 1, or true as value. |
||
| 61 | * |
||
| 62 | * accepted -> required="required" |
||
| 63 | * |
||
| 64 | * @return array |
||
| 65 | * |
||
| 66 | * @see http://laravel.com/docs/5.1/validation#rule-accepted |
||
| 67 | */ |
||
| 68 | 1 | protected function accepted() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Check that the field is required. |
||
| 78 | * |
||
| 79 | * required --> required="required" |
||
| 80 | * |
||
| 81 | * @return array |
||
| 82 | * |
||
| 83 | * @see http://laravel.com/docs/5.1/validation#rule-required |
||
| 84 | */ |
||
| 85 | 16 | protected function required() |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Check that the input only contains alpha. |
||
| 92 | * |
||
| 93 | * alpha --> pattern="[a-zA-Z]+" |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | 1 | protected function alpha() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Check if the input contains only alpha and num. |
||
| 107 | * |
||
| 108 | * alpha_num --> pattern="[a-zA-Z0-9]+" |
||
| 109 | * |
||
| 110 | * @return array |
||
| 111 | * |
||
| 112 | * @see http://laravel.com/docs/5.1/validation#rule-alpha-num |
||
| 113 | */ |
||
| 114 | 1 | protected function alphaNum() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Check if the input contains only alpha, num and dash. |
||
| 124 | * |
||
| 125 | * alpha_dash --> pattern="[a-zA-Z0-9_\-]+" |
||
| 126 | * |
||
| 127 | * @return array |
||
| 128 | * |
||
| 129 | * @see http://laravel.com/docs/5.1/validation#rule-alpha-dash |
||
| 130 | */ |
||
| 131 | protected function alphaDash() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Check if the field is an integer value. Cannot contain decimals. |
||
| 141 | * |
||
| 142 | * integer --> step="1" (number) |
||
| 143 | * integer --> pattern="\d+" (text) |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | * |
||
| 147 | * @see http://laravel.com/docs/5.1/validation#rule-integer |
||
| 148 | */ |
||
| 149 | protected function integer() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Check that a field is numeric. It may contain decimals. |
||
| 163 | * |
||
| 164 | * numeric --> step="any" (number) |
||
| 165 | * numeric --> pattern="[-+]?[0-9]*[.,]?[0-9]+" (text) |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | * |
||
| 169 | * @see http://laravel.com/docs/5.1/validation#rule-numeric |
||
| 170 | */ |
||
| 171 | protected function numeric() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Check that a value is either 0 or 1, so it can be parsed as bool. |
||
| 185 | * |
||
| 186 | * bool --> pattern="0|1" |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | * |
||
| 190 | * @see http://laravel.com/docs/5.1/validation#rule-boolean |
||
| 191 | */ |
||
| 192 | protected function boolean() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Check that the value is numeric and contains exactly the given digits. |
||
| 202 | * |
||
| 203 | * digits:3 --> min="100" max="999" |
||
| 204 | * digits:3 --> pattern="\d{3,5}" (text) |
||
| 205 | * |
||
| 206 | * @param $param |
||
| 207 | * @return array |
||
| 208 | * |
||
| 209 | * @see http://laravel.com/docs/5.1/validation#rule-digits |
||
| 210 | */ |
||
| 211 | protected function digits($param) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Check that the value is numeric and contains between min/max digits. |
||
| 230 | * |
||
| 231 | * digits_between:3,5 --> min="100" max="99999" |
||
| 232 | * digits_between:3,5 --> pattern="\d{3,5}" (text) |
||
| 233 | * |
||
| 234 | * @param $param |
||
| 235 | * @return array |
||
| 236 | * |
||
| 237 | * @see http://laravel.com/docs/5.1/validation#rule-digits-between |
||
| 238 | */ |
||
| 239 | protected function digitsBetween($param) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * For numbers, set the minimum value. |
||
| 258 | * For strings, set the minimum number of characters. |
||
| 259 | * |
||
| 260 | * min:5 --> min="5" (number) |
||
| 261 | * min:5 --> minlength="5" (text) |
||
| 262 | * |
||
| 263 | * @param $param |
||
| 264 | * @return array |
||
| 265 | * |
||
| 266 | * @see http://laravel.com/docs/5.1/validation#rule-min |
||
| 267 | */ |
||
| 268 | 14 | protected function min($param) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * For numbers, set the max value. |
||
| 283 | * For strings, set the max number of characters. |
||
| 284 | * |
||
| 285 | * max:5 --> max="5" (number) |
||
| 286 | * max:5 --> maxlength="5" (text) |
||
| 287 | * |
||
| 288 | * @param $param |
||
| 289 | * @return array |
||
| 290 | * |
||
| 291 | * @see http://laravel.com/docs/5.1/validation#rule-max |
||
| 292 | */ |
||
| 293 | 8 | protected function max($param) |
|
| 294 | { |
||
| 295 | 8 | $max = $param[0]; |
|
| 296 | |||
| 297 | 8 | if ($this->isNumeric()) { |
|
| 298 | return ['max' => $max]; |
||
| 299 | } |
||
| 300 | |||
| 301 | 8 | return ['maxlength' => $max]; |
|
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * For number/range inputs, check if the number is between the values. |
||
| 306 | * For strings, check the length of the string. |
||
| 307 | * |
||
| 308 | * between:3,5 --> min="3" max="5" (number) |
||
| 309 | * between:3,5 --> minlength="3" maxlength="5" (text) |
||
| 310 | * |
||
| 311 | * @param $param |
||
| 312 | * @return array |
||
| 313 | * |
||
| 314 | * @see http://laravel.com/docs/5.1/validation#rule-between |
||
| 315 | */ |
||
| 316 | protected function between($param) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * For numbers: Check an exact value. |
||
| 335 | * For strings: Check the length of the string. |
||
| 336 | * |
||
| 337 | * size:5 --> min="5" max="5" (number) |
||
| 338 | * size:5 --> pattern=".{5}" (text) |
||
| 339 | * |
||
| 340 | * @param mixed $param |
||
| 341 | * @return array |
||
| 342 | * |
||
| 343 | * @see http://laravel.com/docs/5.1/validation#rule-size |
||
| 344 | */ |
||
| 345 | protected function size($param) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Check if the value is one of the give 'in' rule values |
||
| 365 | * by creating a matching pattern. |
||
| 366 | * |
||
| 367 | * in:foo,bar --> pattern="foo|bar" |
||
| 368 | * |
||
| 369 | * @param array $params |
||
| 370 | * @return array |
||
| 371 | * |
||
| 372 | * @see http://laravel.com/docs/5.1/validation#rule-in |
||
| 373 | */ |
||
| 374 | protected function in($params) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Check if the value is not one of the 'not_in' rule values |
||
| 384 | * by creating a pattern value. |
||
| 385 | * |
||
| 386 | * not_in:foo,bar --> pattern="(?:(?!^foo$|^bar$).)*" |
||
| 387 | * |
||
| 388 | * @param array $params |
||
| 389 | * @return array |
||
| 390 | * |
||
| 391 | * @see http://laravel.com/docs/5.1/validation#rule-not-in |
||
| 392 | */ |
||
| 393 | protected function notIn($params) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Set the 'min' attribute on a date/datetime/datetime-local field, |
||
| 403 | * based on the 'before' validation. |
||
| 404 | * |
||
| 405 | * after:01-12-2015 -> min="2015-12-01" |
||
| 406 | * |
||
| 407 | * @param $params |
||
| 408 | * @return array |
||
| 409 | * |
||
| 410 | * @see http://laravel.com/docs/5.1/validation#rule-after |
||
| 411 | */ |
||
| 412 | protected function after($params) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Set the 'min' attribute on a date/datetime/datetime-local field, |
||
| 423 | * based on the 'before' validation. |
||
| 424 | * |
||
| 425 | * before:01-12-2015 -> max="2015-12-01" |
||
| 426 | * |
||
| 427 | * @param $params |
||
| 428 | * @return array |
||
| 429 | * |
||
| 430 | * @see http://laravel.com/docs/5.1/validation#rule-before |
||
| 431 | */ |
||
| 432 | protected function before($params) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Add the image mime-type to a file input. |
||
| 443 | * |
||
| 444 | * @return array |
||
| 445 | * |
||
| 446 | * @see http://laravel.com/docs/5.1/validation#rule-image |
||
| 447 | * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-accept |
||
| 448 | */ |
||
| 449 | protected function image() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Add the mime types to the accept attribute. |
||
| 456 | * |
||
| 457 | * mimes:xls,xlsx --> accept=".xls, .xlsx" |
||
| 458 | * |
||
| 459 | * @param array $params |
||
| 460 | * @return array |
||
| 461 | * |
||
| 462 | * @see http://laravel.com/docs/5.1/validation#rule-mimes |
||
| 463 | * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-accept |
||
| 464 | */ |
||
| 465 | protected function mimes($params) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Get the title, used for validating a rule. |
||
| 474 | * |
||
| 475 | * @param string $rule |
||
| 476 | * @param array $params |
||
| 477 | * @return string |
||
| 478 | */ |
||
| 479 | 3 | protected function getTitle($rule, $params = array()) |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Check if the field is one of certain types. |
||
| 488 | * |
||
| 489 | * @param string|array $types |
||
| 490 | * @return bool |
||
| 491 | */ |
||
| 492 | 15 | protected function isType($types) |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Check if the field is numeric. |
||
| 499 | * |
||
| 500 | * @return bool |
||
| 501 | */ |
||
| 502 | 15 | protected function isNumeric() |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Format a date to the correct format, based on the current field. |
||
| 509 | * |
||
| 510 | * @param $dateStr |
||
| 511 | * @return bool|string |
||
| 512 | */ |
||
| 513 | protected function getDateAttribute($dateStr) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Methods below are copied from \Illuminate\Validation\Validator |
||
| 525 | * @see https://github.com/laravel/framework/blob/5.1/src/Illuminate/Validation/Validator.php |
||
| 526 | * @copyright Taylor Otwell |
||
| 527 | */ |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Extract the rule name and parameters from a rule. |
||
| 531 | * |
||
| 532 | * @param array|string $rules |
||
| 533 | * @return array|null; |
||
|
|
|||
| 534 | */ |
||
| 535 | 18 | protected function parseRule($rules) |
|
| 536 | { |
||
| 537 | 18 | if (is_array($rules)) { |
|
| 538 | return $this->parseArrayRule($rules); |
||
| 539 | } |
||
| 540 | 18 | if (is_string($rules)) { |
|
| 541 | 18 | return $this->parseStringRule($rules); |
|
| 542 | } |
||
| 543 | 1 | } |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Parse an array based rule. |
||
| 547 | * |
||
| 548 | * @param array $rules |
||
| 549 | * @return array |
||
| 550 | */ |
||
| 551 | protected function parseArrayRule(array $rules) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Parse a string based rule. |
||
| 558 | * |
||
| 559 | * @param string $rules |
||
| 560 | * @return array |
||
| 561 | */ |
||
| 562 | 18 | protected function parseStringRule($rules) |
|
| 574 | |||
| 575 | /** |
||
| 576 | * Parse a parameter list. |
||
| 577 | * |
||
| 578 | * @param string $rule |
||
| 579 | * @param string $parameter |
||
| 580 | * @return array |
||
| 581 | */ |
||
| 582 | 15 | protected function parseParameters($rule, $parameter) |
|
| 589 | |||
| 590 | /** |
||
| 591 | * Parse field rules as array and initialize closure rules |
||
| 592 | * |
||
| 593 | * @param string|array $rules |
||
| 594 | * @return array |
||
| 595 | */ |
||
| 596 | 18 | protected function getRulesAsArray($rules) |
|
| 600 | } |
||
| 601 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.