Conditions | 38 |
Paths | 3744 |
Total Lines | 131 |
Code Lines | 77 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
33 | protected function prepareParametersFromRequest(Form $form, array &$params, $entity = null, $masks = [], $fields = []) |
||
34 | { |
||
35 | // ungroup fields if need it |
||
36 | foreach ($fields as $key=>$field) { |
||
37 | if (is_array($field)) { |
||
38 | foreach ($field as $k=>$f) { |
||
39 | $fields[$k]=$f; |
||
40 | } |
||
41 | unset($fields[$key]); |
||
42 | continue; |
||
43 | } |
||
44 | } |
||
45 | |||
46 | // Special handling of some fields |
||
47 | foreach ($form as $name => $child) { |
||
48 | if (!isset($params[$name])) { |
||
49 | continue; |
||
50 | } |
||
51 | |||
52 | $type = $child->getConfig()->getType(); |
||
53 | if ($type instanceof ResolvedFormTypeInterface) { |
||
54 | $type = $type->getInnerType(); |
||
55 | } |
||
56 | switch (get_class($type)) { |
||
57 | case YesNoButtonGroupType::class: |
||
58 | case BooleanType::class: |
||
59 | if (!is_object($entity)) { |
||
60 | break; |
||
61 | } |
||
62 | |||
63 | $setter = 'set'.ucfirst($name); |
||
64 | // Symfony fails to recognize true values on PATCH and add support for all boolean types (on, off, true, false, 1, 0) |
||
65 | // If value is array and count 1, return value of array as string |
||
66 | if (is_array($params[$name]) && 1 == count($params[$name])) { |
||
67 | $params[$name] = end($params[$name]); |
||
68 | } |
||
69 | |||
70 | if ('' === $params[$name]) { |
||
71 | break; |
||
72 | } |
||
73 | |||
74 | // find property by value |
||
75 | if (!empty($fields)) { |
||
76 | $properties = ArrayHelper::getValue('properties', $fields[$name]); |
||
77 | if (is_array($properties)) { |
||
78 | $valuesAsKeys = array_flip(array_values($properties)); |
||
79 | if (isset($valuesAsKeys[$params[$name]])) { |
||
80 | $params[$name] = $valuesAsKeys[$params[$name]]; |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | |||
85 | $data = filter_var($params[$name], FILTER_VALIDATE_BOOLEAN); |
||
86 | $data = (bool) $data; |
||
87 | try { |
||
88 | $entity->$setter($data); |
||
89 | // Manually handled so remove from form processing |
||
90 | unset($form[$name], $params[$name]); |
||
91 | break; |
||
92 | } catch (\InvalidArgumentException $exception) { |
||
|
|||
93 | } |
||
94 | |||
95 | // If not manually handled cast to int because Symfony form processing take false as empty |
||
96 | $params[$name] = (int) $data; |
||
97 | |||
98 | break; |
||
99 | case ChoiceType::class: |
||
100 | case CountryType::class: |
||
101 | case LocaleType::class: |
||
102 | case MultiselectType::class: |
||
103 | case RegionType::class: |
||
104 | case SelectType::class: |
||
105 | case TimezoneType::class: |
||
106 | if (!$child->getConfig()->getOption('multiple')) { |
||
107 | break; |
||
108 | } |
||
109 | |||
110 | // Ensure the value is an array |
||
111 | if (!is_array($params[$name])) { |
||
112 | $params[$name] = (false !== strpos($params[$name], '|')) ? explode('|', $params[$name]) : [$params[$name]]; |
||
113 | } |
||
114 | |||
115 | break; |
||
116 | case DateTimeType::class: |
||
117 | case DateType::class: |
||
118 | case TimeType::class: |
||
119 | // Prevent zero based date placeholders |
||
120 | $dateTest = (int) str_replace(['/', '-', ' '], '', $params[$name]); |
||
121 | |||
122 | if (!$dateTest) { |
||
123 | // Date placeholder was used so just ignore it to allow import of the field |
||
124 | unset($params[$name]); |
||
125 | break; |
||
126 | } |
||
127 | |||
128 | if (false === ($timestamp = strtotime($params[$name]))) { |
||
129 | $timestamp = null; |
||
130 | } |
||
131 | |||
132 | if (!$timestamp) { |
||
133 | unset($params[$name]); |
||
134 | break; |
||
135 | } |
||
136 | |||
137 | switch ($type) { |
||
138 | case DateTimeType::class: |
||
139 | $params[$name] = (new \DateTime(date('Y-m-d H:i:s', $timestamp)))->format('Y-m-d H:i'); |
||
140 | break; |
||
141 | case DateType::class: |
||
142 | $params[$name] = (new \DateTime(date('Y-m-d', $timestamp)))->format('Y-m-d'); |
||
143 | break; |
||
144 | case TimeType::class: |
||
145 | $params[$name] = (new \DateTime(date('H:i:s', $timestamp)))->format('H:i:s'); |
||
146 | break; |
||
147 | } |
||
148 | |||
149 | break; |
||
150 | } |
||
151 | } |
||
152 | |||
153 | if (!isset($masks['description'])) { |
||
154 | // Add description to support strict HTML |
||
155 | $masks['description'] = 'strict_html'; |
||
156 | } |
||
157 | |||
158 | if (!isset($masks['content'])) { |
||
159 | // Assume HTML |
||
160 | $masks['description'] = 'html'; |
||
161 | } |
||
162 | |||
163 | $params = InputHelper::_($params, $masks); |
||
164 | } |
||
228 |