Total Complexity | 49 |
Total Lines | 365 |
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 (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 |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Return fast create fields per module map. |
||
259 | * |
||
260 | * @return array |
||
261 | */ |
||
262 | public function fastCreateFieldsMap(): array |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Return html for fast create form fields. |
||
291 | * |
||
292 | * @param string $type The object type |
||
293 | * @param string $prefix The prefix |
||
294 | * @return string The html for form fields |
||
295 | */ |
||
296 | public function fastCreateFields(string $type, string $prefix): string |
||
297 | { |
||
298 | $cfg = (array)Configure::read(sprintf('Properties.%s.fastCreate', $type)); |
||
299 | $fields = (array)Hash::get($cfg, 'all', ['status', 'title', 'description']); |
||
300 | $required = (array)Hash::get($cfg, 'required', ['status', 'title']); |
||
301 | $html = ''; |
||
302 | $jsonKeys = []; |
||
303 | $ff = []; |
||
304 | foreach ($fields as $field => $fieldType) { |
||
305 | $field = is_numeric($field) ? $fieldType : $field; |
||
306 | $fieldClass = !in_array($field, $required) ? 'fastCreateField' : 'fastCreateField required'; |
||
307 | $fieldOptions = [ |
||
308 | 'id' => sprintf('%s%s', $prefix, $field), |
||
309 | 'class' => $fieldClass, |
||
310 | 'data-name' => $field, |
||
311 | 'key' => sprintf('%s-%s', $type, $field), |
||
312 | ]; |
||
313 | if ($field === 'date_ranges') { |
||
314 | $html .= $this->dateRange($type, $fieldOptions); |
||
315 | continue; |
||
316 | } |
||
317 | if ($fieldType === 'json') { |
||
318 | $jsonKeys[] = $field; |
||
319 | } |
||
320 | $this->prepareFieldOptions($field, $fieldType, $fieldOptions); |
||
321 | |||
322 | $html .= $this->control($field, '', $fieldOptions, $type); |
||
323 | $ff[] = $field; |
||
324 | } |
||
325 | $jsonKeys = array_unique(array_merge($jsonKeys, (array)Configure::read('_jsonKeys'))); |
||
326 | $jsonKeys = array_intersect($jsonKeys, $ff); |
||
327 | |||
328 | if (!empty($jsonKeys)) { |
||
329 | $html .= $this->Form->control('_jsonKeys', ['type' => 'hidden', 'value' => implode(',', $jsonKeys)]); |
||
330 | } |
||
331 | |||
332 | return $html; |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * Prepare field options for field. |
||
337 | * |
||
338 | * @param string $field The field name |
||
339 | * @param string|null $fieldType The field type, if any |
||
340 | * @param array $fieldOptions The field options |
||
341 | * @return void |
||
342 | */ |
||
343 | public function prepareFieldOptions(string $field, ?string $fieldType, array &$fieldOptions): void |
||
357 | } |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Return html for date range fields. |
||
362 | * |
||
363 | * @param string $type The object type |
||
364 | * @param array $options The options |
||
365 | * @return string The html for date range fields |
||
366 | */ |
||
367 | public function dateRange(string $type, array $options): string |
||
395 | } |
||
396 | } |
||
397 |