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

Framework::editableCompose()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 58
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 26
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 58
rs 9.504

How to fix   Long Method   

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 declare(strict_types=1);
2
3
namespace Formularium\Frontend\React;
4
5
use Formularium\HTMLElement;
6
7
class Framework extends \Formularium\Framework
8
{
9
    public function __construct(string $name = 'React')
10
    {
11
        parent::__construct($name);
12
    }
13
14
    /**
15
     * Static counter to generate unique ids.
16
     *
17
     * @return integer
18
     */
19
    public static function counter(): int
20
    {
21
        static $counter = 0;
22
        return $counter++;
23
    }
24
25
    public function htmlHead(HTMLElement &$head)
26
    {
27
        $head->prependContent([
28
            HTMLElement::factory('script', ['src' => "https://unpkg.com/react@16/umd/react.development.js", 'crossorigin' => '']),
29
            HTMLElement::factory('script', ['src' => "https://unpkg.com/react-dom@16/umd/react-dom.development.js", 'crossorigin' => '']),
30
            HTMLElement::factory('script', ['src' => "https://unpkg.com/babel-standalone@6/babel.min.js", 'crossorigin' => ''])
31
        ]);
32
    }
33
34
    public function editableCompose(\Formularium\Model $m, array $elements, string $previousCompose): string
35
    {
36
        $data = [];
37
        foreach ($m->getFields() as $name => $field) {
38
            $data[$name] = $field->getDatatype()->getDefault();
39
        }
40
41
        $editableForm = join('', $elements);
42
        $editableForm = str_replace('"{this.handleInputChange}"', '{this.handleInputChange}', $editableForm);
43
        $editableForm = preg_replace(
44
            '/value="\{this.state.([a-zA-Z0-9_]+)\}"/',
45
            'value={this.state.$1}',
46
            $editableForm
47
        );
48
        
49
        $jsonData = json_encode($data);
50
51
        $id = 'reactapp' . self::counter();
52
        $component = $m->getName() . 'Component';
53
        $reactCode = <<<EOF
54
'use strict';
55
56
const e = React.createElement;
57
58
class {$component} extends React.Component {
59
    constructor(props) {
60
        super(props);
61
        this.state = $jsonData;
62
   
63
        this.handleInputChange = this.handleInputChange.bind(this);
64
    }
65
    
66
    handleInputChange(event) {
67
        const target = event.target;
68
        const value = target.type === 'checkbox' ? target.checked : target.value;
69
        const name = target.name;
70
    
71
        this.setState({
72
            [name]: value
73
        });
74
    }
75
    
76
    render() {
77
        return (
78
        <form>
79
            $editableForm
80
        </form>
81
        );
82
    }
83
}
84
85
const domContainer = document.querySelector('#{$id}');
86
ReactDOM.render(e($component), domContainer);
87
EOF;
88
89
        $t = new HTMLElement('div', ['id' => $id]);
90
        $s = new HTMLElement('script', ['type' => "text/babel"], $reactCode, true);
91
        return HTMLElement::factory('', [], [$t, $s])->getRenderHTML();
92
    }
93
}
94