| Conditions | 12 |
| Paths | 1 |
| Total Lines | 42 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 15 | public function editable($value, Field $field, HTMLElement $previous): HTMLElement |
||
| 16 | { |
||
| 17 | $previous->walk(function (HTMLElement $element) use ($field) { |
||
| 18 | if ($element->getTag() === 'input') { |
||
| 19 | if ($element->getAttribute('type') === ['checkbox']) { |
||
| 20 | $element->setAttribute('checked', "{this.state.{$field->getName()}}"); |
||
| 21 | } else { |
||
| 22 | $element->setAttribute('value', "{this.state.{$field->getName()}}"); |
||
| 23 | } |
||
| 24 | $element->setAttribute('onChange', '{this.handleInputChange}'); |
||
| 25 | } |
||
| 26 | if ($element->getTag() === 'textarea' || $element->getTag() === 'select') { |
||
| 27 | $element->setAttribute('value', "{this.state.{$field->getName()}}"); |
||
| 28 | $element->setAttribute('onChange', '{this.handleInputChange}'); |
||
| 29 | } |
||
| 30 | if ($element->getTag() === 'select') { |
||
| 31 | $options = $element->get('[selected=selected]'); |
||
| 32 | if (!empty($options)) { |
||
| 33 | $element->setAttribute('value', $options[0]->getAttribute('value')[0]); |
||
| 34 | } |
||
| 35 | } |
||
| 36 | if ($element->getTag() === 'option') { |
||
| 37 | $element->removeAttribute('selected'); |
||
| 38 | } |
||
| 39 | if (!empty($element->getAttribute('for'))) { |
||
| 40 | $element->setAttribute('htmlFor', $element->getAttribute('for')); |
||
| 41 | $element->removeAttribute('for'); |
||
| 42 | } |
||
| 43 | if (!empty($element->getAttribute('class'))) { |
||
| 44 | $element->setAttribute('className', $element->getAttribute('class')); |
||
| 45 | $element->removeAttribute('class'); |
||
| 46 | } |
||
| 47 | if (!empty($element->getAttribute('minlength'))) { |
||
| 48 | $element->setAttribute('minLength', $element->getAttribute('minlength')); |
||
| 49 | $element->removeAttribute('minlength'); |
||
| 50 | } |
||
| 51 | if (!empty($element->getAttribute('maxlength'))) { |
||
| 52 | $element->setAttribute('maxLength', $element->getAttribute('maxlength')); |
||
| 53 | $element->removeAttribute('maxlength'); |
||
| 54 | } |
||
| 55 | }); |
||
| 56 | return $previous; |
||
| 57 | } |
||
| 59 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.