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 namespace Kris\LaravelFormBuilder; |
||
| 14 | class RulesParser |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var FormField |
||
| 18 | */ |
||
| 19 | protected $field; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var FormHelper |
||
| 23 | */ |
||
| 24 | protected $formHelper; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param FormField $field |
||
| 28 | */ |
||
| 29 | public function __construct(FormField $field) |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Parse a rule for an input into an array of attributes. |
||
| 37 | * |
||
| 38 | * @param string|array $rules |
||
| 39 | * @return array |
||
| 40 | */ |
||
| 41 | public function parse($rules) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Check that a checkbox is accepted. Needs yes, on, 1, or true as value. |
||
| 59 | * |
||
| 60 | * accepted -> required="required" |
||
| 61 | * |
||
| 62 | * @return array |
||
| 63 | * |
||
| 64 | * @see http://laravel.com/docs/5.1/validation#rule-accepted |
||
| 65 | */ |
||
| 66 | protected function accepted() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Check that the field is required. |
||
| 76 | * |
||
| 77 | * required --> required="required" |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | * |
||
| 81 | * @see http://laravel.com/docs/5.1/validation#rule-required |
||
| 82 | */ |
||
| 83 | protected function required() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Check that the input only contains alpha. |
||
| 90 | * |
||
| 91 | * alpha --> pattern="[a-zA-Z]+" |
||
| 92 | * |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | protected function alpha() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Check if the input contains only alpha and num. |
||
| 105 | * |
||
| 106 | * alpha_num --> pattern="[a-zA-Z0-9]+" |
||
| 107 | * |
||
| 108 | * @return array |
||
| 109 | * |
||
| 110 | * @see http://laravel.com/docs/5.1/validation#rule-alpha-num |
||
| 111 | */ |
||
| 112 | protected function alphaNum() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Check if the input contains only alpha, num and dash. |
||
| 122 | * |
||
| 123 | * alpha_dash --> pattern="[a-zA-Z0-9_\-]+" |
||
| 124 | * |
||
| 125 | * @return array |
||
| 126 | * |
||
| 127 | * @see http://laravel.com/docs/5.1/validation#rule-alpha-dash |
||
| 128 | */ |
||
| 129 | protected function alphaDash() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Check if the field is an integer value. Cannot contain decimals. |
||
| 139 | * |
||
| 140 | * integer --> step="1" (number) |
||
| 141 | * integer --> pattern="\d+" (text) |
||
| 142 | * |
||
| 143 | * @return array |
||
| 144 | * |
||
| 145 | * @see http://laravel.com/docs/5.1/validation#rule-integer |
||
| 146 | */ |
||
| 147 | protected function integer() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Check that a field is numeric. It may contain decimals. |
||
| 161 | * |
||
| 162 | * numeric --> step="any" (number) |
||
| 163 | * numeric --> pattern="[-+]?[0-9]*[.,]?[0-9]+" (text) |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | * |
||
| 167 | * @see http://laravel.com/docs/5.1/validation#rule-numeric |
||
| 168 | */ |
||
| 169 | protected function numeric() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Check that a value is either 0 or 1, so it can be parsed as bool. |
||
| 183 | * |
||
| 184 | * boolean --> pattern="0|1" |
||
| 185 | * |
||
| 186 | * @return array |
||
| 187 | * |
||
| 188 | * @see http://laravel.com/docs/5.1/validation#rule-boolean |
||
| 189 | */ |
||
| 190 | protected function boolean() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Check that the value is numeric and contains exactly the given digits. |
||
| 200 | * |
||
| 201 | * digits:3 --> min="100" max="999" |
||
| 202 | * digits:3 --> pattern="\d{3,5}" (text) |
||
| 203 | * |
||
| 204 | * @param $param |
||
| 205 | * @return array |
||
| 206 | * |
||
| 207 | * @see http://laravel.com/docs/5.1/validation#rule-digits |
||
| 208 | */ |
||
| 209 | protected function digits($param) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Check that the value is numeric and contains between min/max digits. |
||
| 228 | * |
||
| 229 | * digits_between:3,5 --> min="100" max="99999" |
||
| 230 | * digits_between:3,5 --> pattern="\d{3,5}" (text) |
||
| 231 | * |
||
| 232 | * @param $param |
||
| 233 | * @return array |
||
| 234 | * |
||
| 235 | * @see http://laravel.com/docs/5.1/validation#rule-digits-between |
||
| 236 | */ |
||
| 237 | protected function digitsBetween($param) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * For numbers, set the minimum value. |
||
| 256 | * For strings, set the minimum number of characters. |
||
| 257 | * |
||
| 258 | * min:5 --> min="5" (number) |
||
| 259 | * min:5 --> minlength="5" (text) |
||
| 260 | * |
||
| 261 | * @param $param |
||
| 262 | * @return array |
||
| 263 | * |
||
| 264 | * @see http://laravel.com/docs/5.1/validation#rule-min |
||
| 265 | */ |
||
| 266 | protected function min($param) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * For numbers, set the max value. |
||
| 281 | * For strings, set the max number of characters. |
||
| 282 | * |
||
| 283 | * max:5 --> max="5" (number) |
||
| 284 | * max:5 --> maxlength="5" (text) |
||
| 285 | * |
||
| 286 | * @param $param |
||
| 287 | * @return array |
||
| 288 | * |
||
| 289 | * @see http://laravel.com/docs/5.1/validation#rule-max |
||
| 290 | */ |
||
| 291 | protected function max($param) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * For number/range inputs, check if the number is between the values. |
||
| 304 | * For strings, check the length of the string. |
||
| 305 | * |
||
| 306 | * between:3,5 --> min="3" max="5" (number) |
||
| 307 | * between:3,5 --> minlength="3" maxlength="5" (text) |
||
| 308 | * |
||
| 309 | * @param $param |
||
| 310 | * @return array |
||
| 311 | * |
||
| 312 | * @see http://laravel.com/docs/5.1/validation#rule-between |
||
| 313 | */ |
||
| 314 | protected function between($param) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * For numbers: Check an exact value |
||
| 333 | * For strings: Check the length of the string |
||
| 334 | * |
||
| 335 | * size:5 --> min="5" max="5" (number) |
||
| 336 | * size:5 --> pattern=".{5}" (text) |
||
| 337 | * |
||
| 338 | * @param $param |
||
| 339 | * @return array |
||
| 340 | * |
||
| 341 | * @see http://laravel.com/docs/5.1/validation#rule-size |
||
| 342 | */ |
||
| 343 | protected function size($param) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Check if the value is one of the give 'in' rule values |
||
| 363 | * by creating a matching pattern. |
||
| 364 | * |
||
| 365 | * in:foo,bar --> pattern="foo|bar" |
||
| 366 | * |
||
| 367 | * @param $params |
||
| 368 | * @return array |
||
| 369 | * |
||
| 370 | * @see http://laravel.com/docs/5.1/validation#rule-in |
||
| 371 | */ |
||
| 372 | protected function in($params) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Check if the value is not one of the 'not_in' rule values |
||
| 382 | * by creating a pattern value. |
||
| 383 | * |
||
| 384 | * not_in:foo,bar --> pattern="(?:(?!^foo$|^bar$).)*" |
||
| 385 | * |
||
| 386 | * @param $params |
||
| 387 | * @return array |
||
| 388 | * |
||
| 389 | * @see http://laravel.com/docs/5.1/validation#rule-not-in |
||
| 390 | */ |
||
| 391 | protected function notIn($params) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Set the 'min' attribute on a date/datetime/datetime-local field, |
||
| 401 | * based on the 'before' validation. |
||
| 402 | * |
||
| 403 | * after:01-12-2015 -> min="2015-12-01" |
||
| 404 | * |
||
| 405 | * @param $params |
||
| 406 | * @return array |
||
| 407 | * |
||
| 408 | * @see http://laravel.com/docs/5.1/validation#rule-after |
||
| 409 | */ |
||
| 410 | protected function after($params) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Set the 'min' attribute on a date/datetime/datetime-local field, |
||
| 421 | * based on the 'before' validation. |
||
| 422 | * |
||
| 423 | * before:01-12-2015 -> max="2015-12-01" |
||
| 424 | * |
||
| 425 | * @param $params |
||
| 426 | * @return array |
||
| 427 | * |
||
| 428 | * @see http://laravel.com/docs/5.1/validation#rule-before |
||
| 429 | */ |
||
| 430 | protected function before($params) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Add the image mime-type to a file input. |
||
| 441 | * |
||
| 442 | * @return array |
||
| 443 | * |
||
| 444 | * @see http://laravel.com/docs/5.1/validation#rule-image |
||
| 445 | * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-accept |
||
| 446 | */ |
||
| 447 | protected function image() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Add the mime types to the accept attribute. |
||
| 454 | * |
||
| 455 | * mimes:xls,xlsx --> accept=".xls, .xlsx" |
||
| 456 | * |
||
| 457 | * @param array |
||
| 458 | * @return array |
||
| 459 | * |
||
| 460 | * @see http://laravel.com/docs/5.1/validation#rule-mimes |
||
| 461 | * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-accept |
||
| 462 | */ |
||
| 463 | protected function mimes($param) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Get the title, used for validating a rule |
||
| 472 | * |
||
| 473 | * @param string $rule |
||
| 474 | * @param array $params |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | protected function getTitle($rule, $params = array()) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Check if the field is one of certain types. |
||
| 486 | * |
||
| 487 | * @param string|array $types |
||
| 488 | * @return bool |
||
| 489 | */ |
||
| 490 | protected function isType($types) |
||
| 494 | |||
| 495 | protected function isNumeric() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Format a date to the correct format, based on the current field. |
||
| 502 | * |
||
| 503 | * @param $dateStr |
||
| 504 | * @return bool|string |
||
| 505 | */ |
||
| 506 | protected function getDateAttribute($dateStr) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Methods below are copied from \Illuminate\Validation\Validator |
||
| 518 | * @see https://github.com/laravel/framework/blob/5.1/src/Illuminate/Validation/Validator.php |
||
| 519 | * @copyright Taylor Otwell |
||
| 520 | */ |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Extract the rule name and parameters from a rule. |
||
| 524 | * |
||
| 525 | * @param array|string $rules |
||
| 526 | * @return array |
||
| 527 | */ |
||
| 528 | protected function parseRule($rules) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Parse an array based rule. |
||
| 538 | * |
||
| 539 | * @param array $rules |
||
| 540 | * @return array |
||
| 541 | */ |
||
| 542 | protected function parseArrayRule(array $rules) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Parse a string based rule. |
||
| 549 | * |
||
| 550 | * @param string $rules |
||
| 551 | * @return array |
||
| 552 | */ |
||
| 553 | protected function parseStringRule($rules) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Parse a parameter list. |
||
| 568 | * |
||
| 569 | * @param string $rule |
||
| 570 | * @param string $parameter |
||
| 571 | * @return array |
||
| 572 | */ |
||
| 573 | protected function parseParameters($rule, $parameter) |
||
| 580 | } |
||
| 581 |