Passed
Push — master ( bc0996...c35245 )
by Bruno
05:47
created

RenderableReactTrait::editable()   C

Complexity

Conditions 12
Paths 1

Size

Total Lines 42
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 29
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 42
rs 6.9666

How to fix   Complexity   

Long Method

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:

1
<?php
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
0 ignored issues
show
Unused Code introduced by
The parameter $field 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

10
    public function viewable($value, /** @scrutinizer ignore-unused */ 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...
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

10
    public function viewable(/** @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...
11
    {
12
        return $previous;
13
    }
14
15
    public function editable($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

15
    public function editable(/** @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...
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
    }
58
}
59