Conditions | 8 |
Paths | 4 |
Total Lines | 54 |
Code Lines | 27 |
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 namespace Anomaly\Streams\Platform\Ui\Form\Component\Field; |
||
64 | public function make(array $parameters, StreamInterface $stream = null, $entry = null) |
||
65 | { |
||
66 | /* @var EntryInterface $entry */ |
||
67 | if ($stream && $entry instanceof EntryInterface && $entry->hasField(array_get($parameters, 'field'))) { |
||
68 | |||
69 | /** |
||
70 | * Allow overriding the type here |
||
71 | * should they want to do that. |
||
72 | */ |
||
73 | if (array_get($parameters, 'type')) { |
||
74 | $field = $this->builder->build($parameters); |
||
75 | } else { |
||
76 | $field = clone($entry->getFieldType(array_get($parameters, 'field'))); |
||
77 | } |
||
78 | |||
79 | $modifier = $field->getModifier(); |
||
80 | |||
81 | $value = array_pull($parameters, 'value'); |
||
82 | |||
83 | /* @var EntryInterface $entry */ |
||
84 | $field->setValue( |
||
85 | (!is_null($value)) ? $modifier->restore($value) : $entry->getAttribute($field->getField()) |
||
86 | ); |
||
87 | } elseif (is_object($entry)) { |
||
88 | |||
89 | $field = $this->builder->build($parameters); |
||
90 | $modifier = $field->getModifier(); |
||
91 | |||
92 | $value = array_pull($parameters, 'value'); |
||
93 | |||
94 | $field->setValue((!is_null($value)) ? $modifier->restore($value) : $entry->{$field->getField()}); |
||
95 | } else { |
||
96 | |||
97 | $field = $this->builder->build($parameters); |
||
98 | $modifier = $field->getModifier(); |
||
99 | |||
100 | $field->setValue($modifier->restore(array_pull($parameters, 'value'))); |
||
101 | } |
||
102 | |||
103 | // Set the entry. |
||
104 | $field->setEntry($entry); |
||
105 | |||
106 | // Merge in rules and validators. |
||
107 | $field |
||
108 | ->mergeRules(array_pull($parameters, 'rules', [])) |
||
109 | ->mergeConfig(array_pull($parameters, 'config', [])) |
||
110 | ->mergeMessages(array_pull($parameters, 'messages', [])) |
||
111 | ->mergeValidators(array_pull($parameters, 'validators', [])); |
||
112 | |||
113 | // Hydrate the field with parameters. |
||
114 | $this->hydrator->hydrate($field, $parameters); |
||
115 | |||
116 | return $field; |
||
117 | } |
||
118 | } |
||
119 |