Passed
Push — master ( 0a0bcf...92e6c5 )
by Bruno
06:17
created

RenderableReactTrait::editable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\React;
4
5
use Formularium\Field;
6
use Formularium\HTMLElement;
7
8
trait RenderableReactTrait
9
{
10
    public function viewable($value, Field $field, HTMLElement $previous): HTMLElement
11
    {
12
        return $this->fix($value, $field, $previous);
13
    }
14
15
    public function editable($value, Field $field, HTMLElement $previous): HTMLElement
16
    {
17
        return $this->fix($value, $field, $previous);
18
    }
19
20
    public function fix($value, Field $field, HTMLElement $previous): HTMLElement
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

20
    public function fix(/** @scrutinizer ignore-unused */ $value, Field $field, HTMLElement $previous): HTMLElement

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
21
    {
22
        $previous->walk(function (HTMLElement $element) use ($field) {
23
            if ($element->getTag() === 'input') {
24
                if ($element->getAttribute('type') === ['checkbox']) {
25
                    $element->setAttribute('checked', "{this.state.{$field->getName()}}");
26
                } else {
27
                    $element->setAttribute('value', "{this.state.{$field->getName()}}");
28
                }
29
                $element->setAttribute('onChange', '{this.handleInputChange}');
30
            }
31
            if ($element->getTag() === 'textarea' || $element->getTag() === 'select') {
32
                $element->setAttribute('value', "{this.state.{$field->getName()}}");
33
                $element->setAttribute('onChange', '{this.handleInputChange}');
34
            }
35
            if ($element->getTag() === 'select') {
36
                $options = $element->get('[selected=selected]');
37
                if (!empty($options)) {
38
                    $element->setAttribute('value', $options[0]->getAttribute('value')[0]);
39
                }
40
            }
41
            if ($element->getTag() === 'option') {
42
                $element->removeAttribute('selected');
43
            }
44
            if ($element->hasAttribute('for')) {
45
                $element->setAttribute('htmlFor', $element->getAttribute('for'));
46
                $element->removeAttribute('for');
47
            }
48
            if ($element->hasAttribute('class')) {
49
                $element->setAttribute('className', $element->getAttribute('class'));
50
                $element->removeAttribute('class');
51
            }
52
            if (!empty($element->getAttribute('minlength'))) {
53
                $element->setAttribute('minLength', $element->getAttribute('minlength'));
54
                $element->removeAttribute('minlength');
55
            }
56
            if (!empty($element->getAttribute('maxlength'))) {
57
                $element->setAttribute('maxLength', $element->getAttribute('maxlength'));
58
                $element->removeAttribute('maxlength');
59
            }
60
        });
61
        return $previous;
62
    }
63
}
64