| Conditions | 20 |
| Paths | 270 |
| Total Lines | 94 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 declare(strict_types=1); |
||
| 30 | public function editable($value, Field $field, HTMLNode $previous): HTMLNode |
||
| 31 | { |
||
| 32 | $validators = $field->getValidators(); |
||
| 33 | |||
| 34 | // get the tag |
||
| 35 | /** |
||
| 36 | * @var HTMLNode $validationNode |
||
| 37 | */ |
||
| 38 | $validationNode = null; |
||
| 39 | $input = $previous->get('input'); |
||
| 40 | if (count($input)) { |
||
| 41 | $validationNode = $input[0]; |
||
| 42 | } |
||
| 43 | if (!$validationNode) { |
||
|
|
|||
| 44 | $input = $previous->get('textarea'); |
||
| 45 | if (count($input)) { |
||
| 46 | $validationNode = $input[0]; |
||
| 47 | } |
||
| 48 | } |
||
| 49 | if (!$validationNode) { |
||
| 50 | $input = $previous->get('select'); |
||
| 51 | if (count($input)) { |
||
| 52 | $validationNode = $input[0]; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | if (!$validationNode) { |
||
| 56 | return $previous; |
||
| 57 | } |
||
| 58 | |||
| 59 | // add rules |
||
| 60 | $newInput = clone $validationNode; |
||
| 61 | $validationNode->setTag('validation-provider'); |
||
| 62 | $validationNode->clearAttributes()->clearContent(); |
||
| 63 | $validationNode->addAttribute('v-slot', '{ errors }') |
||
| 64 | ->addContent([ |
||
| 65 | $newInput, |
||
| 66 | new HTMLNode('span', [], '{{ v.errors[0] }}') |
||
| 67 | ]); |
||
| 68 | |||
| 69 | $rules = []; |
||
| 70 | foreach ($validators as $validator => $data) { |
||
| 71 | switch ($validator) { |
||
| 72 | case Datatype::REQUIRED: |
||
| 73 | case Filled::class: |
||
| 74 | $rules['required'] = true; |
||
| 75 | break; |
||
| 76 | |||
| 77 | case Equals::class: |
||
| 78 | $rules['oneOf'] = implode(',', $field->getValidatorOption($validator, 'value', '')); |
||
| 79 | break; |
||
| 80 | |||
| 81 | case In::class: |
||
| 82 | $rules['oneOf'] = implode(',', $field->getValidatorOption($validator, 'value', '')); |
||
| 83 | break; |
||
| 84 | |||
| 85 | case Max::class: |
||
| 86 | $rules['max_value'] = $field->getValidatorOption($validator, 'value', ''); |
||
| 87 | break; |
||
| 88 | case Min::class: |
||
| 89 | $rules['min_value'] = $field->getValidatorOption($validator, 'value', ''); |
||
| 90 | break; |
||
| 91 | |||
| 92 | case MaxLength::class: |
||
| 93 | $rules['max'] = $field->getValidatorOption($validator, 'value', ''); |
||
| 94 | break; |
||
| 95 | case MinLength::class: |
||
| 96 | $rules['min'] = $field->getValidatorOption($validator, 'value', ''); |
||
| 97 | break; |
||
| 98 | |||
| 99 | case NotIn::class: |
||
| 100 | // TODO |
||
| 101 | break; |
||
| 102 | |||
| 103 | case Password::class: |
||
| 104 | // TODO |
||
| 105 | break; |
||
| 106 | |||
| 107 | case Regex::class: |
||
| 108 | $rules['regex'] = $field->getValidatorOption($validator, 'value', ''); |
||
| 109 | break; |
||
| 110 | |||
| 111 | case SameAs::class: |
||
| 112 | // TODO |
||
| 113 | break; |
||
| 114 | default: |
||
| 115 | break; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | $validationNode->addAttribute( |
||
| 119 | 'rules', |
||
| 120 | json_encode(array_merge($rules, $this->rules($value, $field, $newInput))) |
||
| 121 | ); |
||
| 122 | |||
| 123 | return $previous; |
||
| 124 | } |
||
| 131 |