| Conditions | 2 |
| Paths | 2 |
| Total Lines | 58 |
| Code Lines | 26 |
| 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 declare(strict_types=1); |
||
| 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 | } |
||
| 94 |