Total Complexity | 54 |
Total Lines | 398 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 3 | Features | 0 |
Complex classes like PropertyHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PropertyHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class PropertyHelper extends Helper |
||
31 | { |
||
32 | /** |
||
33 | * List of helpers used by this helper |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | public $helpers = ['Form', 'Schema']; |
||
38 | |||
39 | /** |
||
40 | * Special paths to retrieve properties from related resources |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | public const RELATED_PATHS = [ |
||
45 | 'file_name' => 'relationships.streams.data.0.attributes.file_name', |
||
46 | 'mime_type' => 'relationships.streams.data.0.attributes.mime_type', |
||
47 | 'file_size' => 'relationships.streams.data.0.meta.file_size', |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * Special properties having their own custom schema type |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | public const SPECIAL_PROPS_TYPE = [ |
||
56 | 'categories' => 'categories', |
||
57 | 'relations' => 'relations', |
||
58 | 'file_size' => 'byte', |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * Generates a form control element for an object property by name, value and options. |
||
63 | * Use SchemaHelper (@see \App\View\Helper\SchemaHelper) to get control options by schema model. |
||
64 | * Use FormHelper (@see \Cake\View\Helper\FormHelper::control) to render control. |
||
65 | * |
||
66 | * @param string $name The property name |
||
67 | * @param mixed|null $value The property value |
||
68 | * @param array $options The form element options, if any |
||
69 | * @param string|null $type The object or resource type, for others schemas |
||
70 | * @return string |
||
71 | */ |
||
72 | public function control(string $name, $value, array $options = [], ?string $type = null): string |
||
73 | { |
||
74 | $forceReadonly = !empty(Hash::get($options, 'readonly')); |
||
75 | $controlOptions = $this->Schema->controlOptions($name, $value, $this->schema($name, $type)); |
||
76 | $controlOptions['label'] = $this->fieldLabel($name, $type); |
||
77 | $readonly = Hash::get($controlOptions, 'readonly') || $forceReadonly; |
||
78 | if ($readonly === true && array_key_exists('html', $controlOptions)) { |
||
79 | $controlOptions['html'] = str_replace('readonly="false"', 'readonly="true"', $controlOptions['html']); |
||
80 | $controlOptions['html'] = str_replace(':readonly=false', ':readonly=true', $controlOptions['html']); |
||
81 | } |
||
82 | if ($readonly === true && array_key_exists('v-datepicker', $controlOptions)) { |
||
83 | unset($controlOptions['v-datepicker']); |
||
84 | } |
||
85 | if (!$readonly && Hash::get($controlOptions, 'class') === 'json' || Hash::get($controlOptions, 'type') === 'json') { |
||
86 | $jsonKeys = (array)Configure::read('_jsonKeys'); |
||
87 | Configure::write('_jsonKeys', array_merge($jsonKeys, [$name])); |
||
88 | } |
||
89 | if (Hash::check($controlOptions, 'html')) { |
||
90 | return (string)Hash::get($controlOptions, 'html', ''); |
||
91 | } |
||
92 | |||
93 | return $this->Form->control($name, array_merge($controlOptions, $options)); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Generates a form control for translation property |
||
98 | * |
||
99 | * @param string $name The property name |
||
100 | * @param mixed|null $value The property value |
||
101 | * @param array $options The form element options, if any |
||
102 | * @return string |
||
103 | */ |
||
104 | public function translationControl(string $name, $value, array $options = []): string |
||
105 | { |
||
106 | $formControlName = sprintf('translated_fields[%s]', $name); |
||
107 | $controlOptions = $this->Schema->controlOptions($name, $value, $this->schema($name, null)); |
||
108 | if (array_key_exists('html', $controlOptions)) { |
||
109 | $controlOptions['html'] = str_replace(sprintf('name="%s"', $name), sprintf('name="%s"', $formControlName), $controlOptions['html']); |
||
110 | } |
||
111 | $controlOptions['label'] = $this->fieldLabel($name, null); |
||
112 | $readonly = Hash::get($controlOptions, 'readonly'); |
||
113 | if ($readonly === true && array_key_exists('v-datepicker', $controlOptions)) { |
||
114 | unset($controlOptions['v-datepicker']); |
||
115 | } |
||
116 | if (Hash::get($controlOptions, 'class') === 'json' || Hash::get($controlOptions, 'type') === 'json') { |
||
117 | $jsonKeys = (array)Configure::read('_jsonKeys'); |
||
118 | Configure::write('_jsonKeys', array_merge($jsonKeys, [$formControlName])); |
||
119 | } |
||
120 | if (Hash::check($controlOptions, 'html')) { |
||
121 | return (string)Hash::get($controlOptions, 'html', ''); |
||
122 | } |
||
123 | |||
124 | return $this->Form->control($formControlName, array_merge($controlOptions, $options)); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Return label for field by name and type. |
||
129 | * If there's a config for the field and type, return it. |
||
130 | * Return translation of name, otherwise. |
||
131 | * |
||
132 | * @param string $name The field name |
||
133 | * @param string|null $type The object type |
||
134 | * @return string |
||
135 | */ |
||
136 | public function fieldLabel(string $name, ?string $type = null): string |
||
137 | { |
||
138 | $defaultLabel = (string)Translate::get($name); |
||
139 | $t = empty($type) ? $this->getView()->get('objectType') : $type; |
||
140 | if (empty($t)) { |
||
141 | return $defaultLabel; |
||
142 | } |
||
143 | $key = sprintf('Properties.%s.options.%s.label', $t, $name); |
||
144 | |||
145 | return (string)Configure::read($key, $defaultLabel); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * JSON Schema array of property name |
||
150 | * |
||
151 | * @param string $name The property name |
||
152 | * @param string|null $objectType The object or resource type to use as schema |
||
153 | * @return array|null |
||
154 | */ |
||
155 | public function schema(string $name, ?string $objectType = null): ?array |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Get formatted property value of a resource or object. |
||
175 | * |
||
176 | * @param array $resource Resource or object data |
||
177 | * @param string $property Property name |
||
178 | * @return string |
||
179 | */ |
||
180 | public function value(array $resource, string $property): string |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Return translations for object fields and more. |
||
201 | * |
||
202 | * @return array |
||
203 | */ |
||
204 | public function translationsMap(): array |
||
205 | { |
||
206 | try { |
||
207 | $key = CacheTools::cacheKey('translationsMap'); |
||
208 | $map = Cache::remember( |
||
209 | $key, |
||
210 | function () { |
||
211 | $map = []; |
||
212 | $keys = []; |
||
213 | Configure::load('properties'); |
||
214 | $properties = (array)Configure::read('DefaultProperties'); |
||
215 | $removeKeys = ['_element', '_hide', '_keep']; |
||
216 | foreach ($properties as $name => $prop) { |
||
217 | $keys[] = trim($name); |
||
218 | $keys = array_merge($keys, (array)Hash::get($prop, 'fastCreate.all', [])); |
||
219 | $keys = array_merge($keys, (array)Hash::get($prop, 'index', [])); |
||
220 | $keys = array_merge($keys, (array)Hash::get($prop, 'filter', [])); |
||
221 | $groups = array_keys((array)Hash::get($prop, 'view', [])); |
||
222 | $addKeys = array_reduce($groups, function ($carry, $group) use ($prop, $removeKeys) { |
||
223 | $carry[] = $group; |
||
224 | $groupKeys = (array)Hash::get($prop, sprintf('view.%s', $group), []); |
||
225 | $groupKeys = array_filter( |
||
226 | $groupKeys, |
||
227 | function ($val, $key) use ($removeKeys) { |
||
228 | return is_string($val) && !in_array($key, $removeKeys); |
||
229 | }, |
||
230 | ARRAY_FILTER_USE_BOTH |
||
231 | ); |
||
232 | |||
233 | return array_merge($carry, $groupKeys); |
||
234 | }, []); |
||
235 | $keys = array_merge($keys, $addKeys); |
||
236 | } |
||
237 | $keys = array_map(function ($key) { |
||
238 | return is_array($key) ? array_key_first($key) : $key; |
||
239 | }, $keys); |
||
240 | $keys = array_diff($keys, $removeKeys); |
||
241 | $keys = array_unique($keys); |
||
242 | $keys = array_map(function ($key) { |
||
243 | return strpos($key, '/') !== false ? substr($key, strrpos($key, '/') + 1) : $key; |
||
244 | }, $keys); |
||
245 | $properties = (array)Configure::read(sprintf('Properties')); |
||
246 | foreach ($properties as $name => $prop) { |
||
247 | $keys[] = trim($name); |
||
248 | $keys = array_merge($keys, (array)Hash::get($prop, 'fastCreate.all', [])); |
||
249 | $keys = array_merge($keys, (array)Hash::get($prop, 'index', [])); |
||
250 | $keys = array_merge($keys, (array)Hash::get($prop, 'filter', [])); |
||
251 | $groups = array_keys((array)Hash::get($prop, 'view', [])); |
||
252 | $addKeys = array_reduce($groups, function ($carry, $group) use ($prop, $removeKeys) { |
||
253 | $carry[] = $group; |
||
254 | $groupKeys = (array)Hash::get($prop, sprintf('view.%s', $group), []); |
||
255 | $groupKeys = array_filter( |
||
256 | $groupKeys, |
||
257 | function ($val, $key) use ($removeKeys) { |
||
258 | return is_string($val) && !in_array($key, $removeKeys); |
||
259 | }, |
||
260 | ARRAY_FILTER_USE_BOTH |
||
261 | ); |
||
262 | |||
263 | return array_merge($carry, $groupKeys); |
||
264 | }, []); |
||
265 | $keys = array_merge($keys, $addKeys); |
||
266 | } |
||
267 | $keys = array_map(function ($key) { |
||
268 | return is_array($key) ? array_key_first($key) : $key; |
||
269 | }, $keys); |
||
270 | $keys = array_diff($keys, $removeKeys); |
||
271 | $keys = array_unique($keys); |
||
272 | $keys = array_map(function ($key) { |
||
273 | return strpos($key, '/') !== false ? substr($key, strrpos($key, '/') + 1) : $key; |
||
274 | }, $keys); |
||
275 | sort($keys); |
||
276 | foreach ($keys as $key) { |
||
277 | $map[$key] = (string)Translate::get($key); |
||
278 | } |
||
279 | |||
280 | return $map; |
||
281 | } |
||
282 | ); |
||
283 | } catch (\Throwable $e) { |
||
284 | $map = []; |
||
285 | } |
||
286 | |||
287 | return $map; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Return fast create fields per module map. |
||
292 | * |
||
293 | * @return array |
||
294 | */ |
||
295 | public function fastCreateFieldsMap(): array |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Return html for fast create form fields. |
||
324 | * |
||
325 | * @param string $type The object type |
||
326 | * @param string $prefix The prefix |
||
327 | * @return string The html for form fields |
||
328 | */ |
||
329 | public function fastCreateFields(string $type, string $prefix): string |
||
330 | { |
||
331 | $cfg = (array)Configure::read(sprintf('Properties.%s.fastCreate', $type)); |
||
332 | $fields = (array)Hash::get($cfg, 'all', ['status', 'title', 'description']); |
||
333 | $required = (array)Hash::get($cfg, 'required', ['status', 'title']); |
||
334 | $html = ''; |
||
335 | $jsonKeys = []; |
||
336 | $ff = []; |
||
337 | foreach ($fields as $field => $fieldType) { |
||
338 | $field = is_numeric($field) ? $fieldType : $field; |
||
339 | $fieldClass = !in_array($field, $required) ? 'fastCreateField' : 'fastCreateField required'; |
||
340 | $fieldOptions = [ |
||
341 | 'id' => sprintf('%s%s', $prefix, $field), |
||
342 | 'class' => $fieldClass, |
||
343 | 'data-name' => $field, |
||
344 | 'key' => sprintf('%s-%s', $type, $field), |
||
345 | ]; |
||
346 | if ($field === 'date_ranges') { |
||
347 | $html .= $this->dateRange($type, $fieldOptions); |
||
348 | continue; |
||
349 | } |
||
350 | if ($fieldType === 'json') { |
||
351 | $jsonKeys[] = $field; |
||
352 | } |
||
353 | $this->prepareFieldOptions($field, $fieldType, $fieldOptions); |
||
354 | |||
355 | $html .= $this->control($field, '', $fieldOptions, $type); |
||
356 | $ff[] = $field; |
||
357 | } |
||
358 | $jsonKeys = array_unique(array_merge($jsonKeys, (array)Configure::read('_jsonKeys'))); |
||
359 | $jsonKeys = array_intersect($jsonKeys, $ff); |
||
360 | |||
361 | if (!empty($jsonKeys)) { |
||
362 | $html .= $this->Form->control('_jsonKeys', ['type' => 'hidden', 'value' => implode(',', $jsonKeys)]); |
||
363 | } |
||
364 | |||
365 | return $html; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Prepare field options for field. |
||
370 | * |
||
371 | * @param string $field The field name |
||
372 | * @param string|null $fieldType The field type, if any |
||
373 | * @param array $fieldOptions The field options |
||
374 | * @return void |
||
375 | */ |
||
376 | public function prepareFieldOptions(string $field, ?string $fieldType, array &$fieldOptions): void |
||
390 | } |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Return html for date range fields. |
||
395 | * |
||
396 | * @param string $type The object type |
||
397 | * @param array $options The options |
||
398 | * @return string The html for date range fields |
||
399 | */ |
||
400 | public function dateRange(string $type, array $options): string |
||
428 | } |
||
429 | } |
||
430 |