| Conditions | 11 |
| Paths | 192 |
| Total Lines | 48 |
| Code Lines | 32 |
| 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 |
||
| 17 | public function editable($value, Field $f, HTMLElement $previous): HTMLElement |
||
| 18 | { |
||
| 19 | $input = new HTMLElement('input'); |
||
| 20 | |||
| 21 | $extensions = $f->getExtensions(); |
||
| 22 | $validators = $f->getValidators(); |
||
| 23 | $input->setAttributes([ |
||
| 24 | 'id' => $f->getName() . Framework::counter(), |
||
| 25 | 'type' => ($extensions[static::HIDDEN] ?? false ? 'hidden' : 'text'), |
||
| 26 | 'name' => $f->getName(), |
||
| 27 | 'class' => '', |
||
| 28 | 'data-attribute' => $f->getName(), |
||
| 29 | 'data-datatype' => $f->getDatatype()->getName(), |
||
| 30 | 'data-basetype' => $f->getDatatype()->getBasetype(), |
||
| 31 | 'value' => $value, |
||
| 32 | 'maxlength' => static::MAX_STRING_SIZE, |
||
| 33 | 'title' => $f->getExtension(static::LABEL, '') |
||
| 34 | ]); |
||
| 35 | |||
| 36 | if (isset($extensions[static::PLACEHOLDER])) { |
||
| 37 | $input->setAttribute('placeholder', $extensions[static::PLACEHOLDER]); |
||
| 38 | } |
||
| 39 | foreach ([static::DISABLED, static::READONLY, static::REQUIRED] as $v) { |
||
| 40 | if ($f->getExtension($v, false)) { |
||
| 41 | $input->setAttribute($v, $v); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | if (array_key_exists(static::MIN_LENGTH, $validators)) { |
||
| 46 | $input->setAttribute('minlength', $validators[static::MIN_LENGTH]); |
||
| 47 | } |
||
| 48 | if (array_key_exists(static::MAX_LENGTH, $validators) |
||
| 49 | && $validators[static::MAX_LENGTH] < static::MAX_STRING_SIZE |
||
| 50 | ) { |
||
| 51 | $input->setAttribute('maxlength', $validators[static::MAX_LENGTH]); |
||
| 52 | } |
||
| 53 | if (isset($extensions[static::NO_AUTOCOMPLETE])) { |
||
| 54 | $input->setAttribute('autocomplete', 'off'); |
||
| 55 | } |
||
| 56 | |||
| 57 | $container = new HTMLElement('div', [], $input); |
||
| 58 | if (array_key_exists('label', $extensions)) { |
||
| 59 | $container->prependContent(new HTMLElement('label', ['for' => $input->getAttribute('id')], $extensions['label'])); |
||
| 60 | } |
||
| 61 | if (array_key_exists('comment', $extensions)) { |
||
| 62 | $container->appendContent(new HTMLElement('div', ['class' => 'comment'], $extensions['comment'])); |
||
| 63 | } |
||
| 64 | return $container; |
||
| 65 | } |
||
| 67 |