| Total Complexity | 41 |
| Total Lines | 295 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | 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 |
||
| 28 | class PropertyHelper extends Helper |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * List of helpers used by this helper |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | public $helpers = ['Form', 'Schema']; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Special paths to retrieve properties from related resources |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | public const RELATED_PATHS = [ |
||
| 43 | 'file_name' => 'relationships.streams.data.0.attributes.file_name', |
||
| 44 | 'mime_type' => 'relationships.streams.data.0.attributes.mime_type', |
||
| 45 | 'file_size' => 'relationships.streams.data.0.meta.file_size', |
||
| 46 | ]; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Special properties having their own custom schema type |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | public const SPECIAL_PROPS_TYPE = [ |
||
| 54 | 'categories' => 'categories', |
||
| 55 | 'relations' => 'relations', |
||
| 56 | 'file_size' => 'byte', |
||
| 57 | ]; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Generates a form control element for an object property by name, value and options. |
||
| 61 | * Use SchemaHelper (@see \App\View\Helper\SchemaHelper) to get control options by schema model. |
||
| 62 | * Use FormHelper (@see \Cake\View\Helper\FormHelper::control) to render control. |
||
| 63 | * |
||
| 64 | * @param string $name The property name |
||
| 65 | * @param mixed|null $value The property value |
||
| 66 | * @param array $options The form element options, if any |
||
| 67 | * @param string|null $type The object or resource type, for others schemas |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public function control(string $name, $value, array $options = [], ?string $type = null): string |
||
| 71 | { |
||
| 72 | $forceReadonly = !empty(Hash::get($options, 'readonly')); |
||
| 73 | $controlOptions = $this->Schema->controlOptions($name, $value, $this->schema($name, $type)); |
||
| 74 | $controlOptions['label'] = $this->fieldLabel($name, $type); |
||
| 75 | $readonly = Hash::get($controlOptions, 'readonly') || $forceReadonly; |
||
| 76 | if ($readonly === true && array_key_exists('html', $controlOptions)) { |
||
| 77 | $controlOptions['html'] = str_replace('readonly="false"', 'readonly="true"', $controlOptions['html']); |
||
| 78 | $controlOptions['html'] = str_replace(':readonly=false', ':readonly=true', $controlOptions['html']); |
||
| 79 | } |
||
| 80 | if ($readonly === true && array_key_exists('v-datepicker', $controlOptions)) { |
||
| 81 | unset($controlOptions['v-datepicker']); |
||
| 82 | } |
||
| 83 | if (Hash::get($controlOptions, 'class') === 'json' || Hash::get($controlOptions, 'type') === 'json') { |
||
| 84 | $jsonKeys = (array)Configure::read('_jsonKeys'); |
||
| 85 | Configure::write('_jsonKeys', array_merge($jsonKeys, [$name])); |
||
| 86 | } |
||
| 87 | if (Hash::check($controlOptions, 'html')) { |
||
| 88 | return (string)Hash::get($controlOptions, 'html', ''); |
||
| 89 | } |
||
| 90 | |||
| 91 | return $this->Form->control($name, array_merge($controlOptions, $options)); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Generates a form control for translation property |
||
| 96 | * |
||
| 97 | * @param string $name The property name |
||
| 98 | * @param mixed|null $value The property value |
||
| 99 | * @param array $options The form element options, if any |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function translationControl(string $name, $value, array $options = []): string |
||
| 103 | { |
||
| 104 | $formControlName = sprintf('translated_fields[%s]', $name); |
||
| 105 | $controlOptions = $this->Schema->controlOptions($name, $value, $this->schema($name, null)); |
||
| 106 | if (array_key_exists('html', $controlOptions)) { |
||
| 107 | $controlOptions['html'] = str_replace(sprintf('name="%s"', $name), sprintf('name="%s"', $formControlName), $controlOptions['html']); |
||
| 108 | } |
||
| 109 | $controlOptions['label'] = $this->fieldLabel($name, null); |
||
| 110 | $readonly = Hash::get($controlOptions, 'readonly'); |
||
| 111 | if ($readonly === true && array_key_exists('v-datepicker', $controlOptions)) { |
||
| 112 | unset($controlOptions['v-datepicker']); |
||
| 113 | } |
||
| 114 | if (Hash::get($controlOptions, 'class') === 'json' || Hash::get($controlOptions, 'type') === 'json') { |
||
| 115 | $jsonKeys = (array)Configure::read('_jsonKeys'); |
||
| 116 | Configure::write('_jsonKeys', array_merge($jsonKeys, [$formControlName])); |
||
| 117 | } |
||
| 118 | if (Hash::check($controlOptions, 'html')) { |
||
| 119 | return (string)Hash::get($controlOptions, 'html', ''); |
||
| 120 | } |
||
| 121 | |||
| 122 | return $this->Form->control($formControlName, array_merge($controlOptions, $options)); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Return label for field by name and type. |
||
| 127 | * If there's a config for the field and type, return it. |
||
| 128 | * Return translation of name, otherwise. |
||
| 129 | * |
||
| 130 | * @param string $name The field name |
||
| 131 | * @param string|null $type The object type |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | public function fieldLabel(string $name, ?string $type = null): string |
||
| 135 | { |
||
| 136 | $defaultLabel = (string)Translate::get($name); |
||
| 137 | $t = empty($type) ? $this->getView()->get('objectType') : $type; |
||
| 138 | if (empty($t)) { |
||
| 139 | return $defaultLabel; |
||
| 140 | } |
||
| 141 | $key = sprintf('Properties.%s.options.%s.label', $t, $name); |
||
| 142 | |||
| 143 | return (string)Configure::read($key, $defaultLabel); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * JSON Schema array of property name |
||
| 148 | * |
||
| 149 | * @param string $name The property name |
||
| 150 | * @param string|null $objectType The object or resource type to use as schema |
||
| 151 | * @return array|null |
||
| 152 | */ |
||
| 153 | public function schema(string $name, ?string $objectType = null): ?array |
||
| 154 | { |
||
| 155 | $schema = (array)$this->_View->get('schema'); |
||
| 156 | if (!empty($objectType)) { |
||
| 157 | $schemas = (array)$this->_View->get('schemasByType'); |
||
| 158 | $schema = (array)Hash::get($schemas, $objectType); |
||
| 159 | } |
||
| 160 | if (Hash::check(self::SPECIAL_PROPS_TYPE, $name)) { |
||
| 161 | return array_filter([ |
||
| 162 | 'type' => Hash::get(self::SPECIAL_PROPS_TYPE, $name), |
||
| 163 | $name => Hash::get($schema, sprintf('%s', $name)), |
||
| 164 | ]); |
||
| 165 | } |
||
| 166 | $res = Hash::get($schema, sprintf('properties.%s', $name)); |
||
| 167 | |||
| 168 | return $res === null ? null : (array)$res; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get formatted property value of a resource or object. |
||
| 173 | * |
||
| 174 | * @param array $resource Resource or object data |
||
| 175 | * @param string $property Property name |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | public function value(array $resource, string $property): string |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Return fast create fields per module map. |
||
| 199 | * |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | public function fastCreateFieldsMap(): array |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Return html for fast create form fields. |
||
| 219 | * |
||
| 220 | * @param string $type The object type |
||
| 221 | * @param string $prefix The prefix |
||
| 222 | * @return string The html for form fields |
||
| 223 | */ |
||
| 224 | public function fastCreateFields(string $type, string $prefix): string |
||
| 225 | { |
||
| 226 | $cfg = (array)Configure::read(sprintf('Properties.%s.fastCreate', $type)); |
||
| 227 | $fields = (array)Hash::get($cfg, 'all', ['status', 'title', 'description']); |
||
| 228 | $required = (array)Hash::get($cfg, 'required', ['status', 'title']); |
||
| 229 | $html = ''; |
||
| 230 | $jsonKeys = []; |
||
| 231 | $ff = []; |
||
| 232 | foreach ($fields as $field => $fieldType) { |
||
| 233 | $field = is_numeric($field) ? $fieldType : $field; |
||
| 234 | $fieldClass = !in_array($field, $required) ? 'fastCreateField' : 'fastCreateField required'; |
||
| 235 | $fieldOptions = [ |
||
| 236 | 'id' => sprintf('%s%s', $prefix, $field), |
||
| 237 | 'class' => $fieldClass, |
||
| 238 | 'data-name' => $field, |
||
| 239 | 'key' => sprintf('%s-%s', $type, $field), |
||
| 240 | ]; |
||
| 241 | if ($field === 'date_ranges') { |
||
| 242 | $html .= $this->dateRange($type, $fieldOptions); |
||
| 243 | continue; |
||
| 244 | } |
||
| 245 | if ($fieldType === 'json') { |
||
| 246 | $jsonKeys[] = $field; |
||
| 247 | } |
||
| 248 | $this->prepareFieldOptions($field, $fieldType, $fieldOptions); |
||
| 249 | |||
| 250 | $html .= $this->control($field, '', $fieldOptions, $type); |
||
| 251 | $ff[] = $field; |
||
| 252 | } |
||
| 253 | $jsonKeys = array_unique(array_merge($jsonKeys, (array)Configure::read('_jsonKeys'))); |
||
| 254 | $jsonKeys = array_intersect($jsonKeys, $ff); |
||
| 255 | |||
| 256 | if (!empty($jsonKeys)) { |
||
| 257 | $html .= $this->Form->control('_jsonKeys', ['type' => 'hidden', 'value' => implode(',', $jsonKeys)]); |
||
| 258 | } |
||
| 259 | |||
| 260 | return $html; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Prepare field options for field. |
||
| 265 | * |
||
| 266 | * @param string $field The field name |
||
| 267 | * @param string|null $fieldType The field type, if any |
||
| 268 | * @param array $fieldOptions The field options |
||
| 269 | * @return void |
||
| 270 | */ |
||
| 271 | public function prepareFieldOptions(string $field, ?string $fieldType, array &$fieldOptions): void |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Return html for date range fields. |
||
| 290 | * |
||
| 291 | * @param string $type The object type |
||
| 292 | * @param array $options The options |
||
| 293 | * @return string The html for date range fields |
||
| 294 | */ |
||
| 295 | public function dateRange(string $type, array $options): string |
||
| 323 | } |
||
| 324 | } |
||
| 325 |