| Total Complexity | 70 |
| Total Lines | 463 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like StructureFormGroup 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 StructureFormGroup, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 76 | class StructureFormGroup extends FormGroupWidget implements |
||
| 77 | FormInputInterface, |
||
| 78 | StructureContainerInterface |
||
| 79 | { |
||
| 80 | use StructureContainerTrait; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The structure entry identifier. |
||
| 84 | * |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | private $structId; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The form group's storage model. |
||
| 91 | * |
||
| 92 | * @var \Charcoal\Model\ModelInterface|null |
||
| 93 | */ |
||
| 94 | protected $obj; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The form group's storage medium. |
||
| 98 | * |
||
| 99 | * @var array|PropertyInterface|\Charcoal\Source\SourceInterface|null |
||
| 100 | */ |
||
| 101 | protected $storage; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * The form group's storage target. {@deprecated In favor of $storage.} |
||
| 105 | * |
||
| 106 | * @var ModelStructureProperty|null |
||
| 107 | */ |
||
| 108 | protected $storageProperty; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The form group the input belongs to. |
||
| 112 | * |
||
| 113 | * @var FormGroupInterface|null |
||
| 114 | */ |
||
| 115 | private $formGroup; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Whether the form is ready. |
||
| 119 | * |
||
| 120 | * @var boolean |
||
| 121 | */ |
||
| 122 | protected $isStructureFinalized = false; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * The form group's raw data. |
||
| 126 | * |
||
| 127 | * @var array|null |
||
| 128 | */ |
||
| 129 | protected $rawData; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | public function type() |
||
| 135 | { |
||
| 136 | return 'charcoal/admin/widget/form-group/structure'; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $structId The structure entry identifier. |
||
| 141 | * @return self |
||
| 142 | */ |
||
| 143 | public function setStructId($structId) |
||
| 144 | { |
||
| 145 | $this->structId = $structId; |
||
| 146 | return $this; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | public function structId() |
||
| 153 | { |
||
| 154 | if (!$this->structId) { |
||
| 155 | $this->structId = uniqid(); |
||
| 156 | } |
||
| 157 | |||
| 158 | return $this->structId; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param array $data Widget data. |
||
| 163 | * @return self |
||
| 164 | */ |
||
| 165 | public function setData(array $data) |
||
| 166 | { |
||
| 167 | if ($this->rawData === null) { |
||
| 168 | $this->rawData = $data; |
||
| 169 | } |
||
| 170 | |||
| 171 | parent::setData($data); |
||
| 172 | |||
| 173 | return $this; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Determine if the header is to be displayed. |
||
| 178 | * |
||
| 179 | * @return boolean If TRUE or unset, check if there is a title. |
||
| 180 | */ |
||
| 181 | public function showHeader() |
||
| 182 | { |
||
| 183 | if ($this->display() === self::SEAMLESS_STRUCT_DISPLAY) { |
||
| 184 | return false; |
||
| 185 | } else { |
||
| 186 | return parent::showHeader(); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Determine if the footer is to be displayed. |
||
| 192 | * |
||
| 193 | * @return boolean If TRUE or unset, check if there are notes. |
||
| 194 | */ |
||
| 195 | public function showFooter() |
||
| 196 | { |
||
| 197 | if ($this->display() === self::SEAMLESS_STRUCT_DISPLAY) { |
||
| 198 | return false; |
||
| 199 | } else { |
||
| 200 | return parent::showFooter(); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Retrieve the form's object. |
||
| 206 | * |
||
| 207 | * @throws RuntimeException If the form doesn't have a model. |
||
| 208 | * @return \Charcoal\Model\ModelInterface |
||
| 209 | */ |
||
| 210 | public function obj() |
||
| 211 | { |
||
| 212 | if ($this->obj === null) { |
||
| 213 | $formGroup = $this->formGroup(); |
||
| 214 | if ($formGroup instanceof self) { |
||
| 215 | $prop = $formGroup->storageProperty(); |
||
| 216 | $val = $formGroup->obj()->propertyValue($prop->ident()); |
||
|
|
|||
| 217 | |||
| 218 | $this->obj = $prop->structureVal($val, [ 'default_data' => true ]); |
||
| 219 | if ($this->obj === null) { |
||
| 220 | $this->obj = clone $prop->structureProto(); |
||
| 221 | } |
||
| 222 | } elseif ($this->form() instanceof ObjectContainerInterface) { |
||
| 223 | $this->obj = $this->form()->obj(); |
||
| 224 | } |
||
| 225 | |||
| 226 | if ($this->obj === null) { |
||
| 227 | throw new RuntimeException(sprintf( |
||
| 228 | 'The [%1$s] widget has no data model.', |
||
| 229 | static::CLASS |
||
| 230 | )); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | return $this->obj; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Set the form input's parent group. |
||
| 239 | * |
||
| 240 | * @param FormGroupInterface $formGroup The parent form group object. |
||
| 241 | * @return self |
||
| 242 | */ |
||
| 243 | public function setFormGroup(FormGroupInterface $formGroup) |
||
| 244 | { |
||
| 245 | $this->formGroup = $formGroup; |
||
| 246 | |||
| 247 | return $this; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Retrieve the input's parent group. |
||
| 252 | * |
||
| 253 | * @return FormGroupInterface|null |
||
| 254 | */ |
||
| 255 | public function formGroup() |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Clear the group's parent group. |
||
| 262 | * |
||
| 263 | * @return self |
||
| 264 | */ |
||
| 265 | public function clearFormGroup() |
||
| 266 | { |
||
| 267 | $this->formGroup = null; |
||
| 268 | |||
| 269 | return $this; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Set the form group's storage target. |
||
| 274 | * |
||
| 275 | * Must be a property of the form's object model that will receive an associative array |
||
| 276 | * of the form group's data. |
||
| 277 | * |
||
| 278 | * @param string|ModelStructureProperty $propertyIdent The property identifier—or instance—of a storage property. |
||
| 279 | * @throws InvalidArgumentException If the property identifier is not a string. |
||
| 280 | * @throws UnexpectedValueException If a property is invalid. |
||
| 281 | * @return self |
||
| 282 | */ |
||
| 283 | public function setStorageProperty($propertyIdent) |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Retrieve the form group's storage property master. |
||
| 324 | * |
||
| 325 | * @throws RuntimeException If the storage property was not previously set. |
||
| 326 | * @return ModelStructureProperty |
||
| 327 | */ |
||
| 328 | public function storageProperty() |
||
| 329 | { |
||
| 330 | if ($this->storageProperty === null) { |
||
| 331 | throw new RuntimeException(sprintf( |
||
| 332 | 'Storage property owner is not defined for "%s"', |
||
| 333 | get_class($this) |
||
| 334 | )); |
||
| 335 | } |
||
| 336 | |||
| 337 | return $this->storageProperty; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Retrieve the properties from the storage property's structure. |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | public function structProperties() |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Finalize the form group's properies, entries, and layout. |
||
| 362 | * |
||
| 363 | * @param boolean $reload Rebuild the form group's structure. |
||
| 364 | * @return void |
||
| 365 | */ |
||
| 366 | protected function finalizeStructure($reload = false) |
||
| 367 | { |
||
| 368 | if ($reload || !$this->isStructureFinalized) { |
||
| 369 | $this->isStructureFinalized = true; |
||
| 370 | |||
| 371 | $property = $this->storageProperty(); |
||
| 372 | |||
| 373 | $struct = $property->structureMetadata(); |
||
| 374 | $formGroup = null; |
||
| 375 | if (isset($struct['admin']['default_form_group'])) { |
||
| 376 | $groupName = $struct['admin']['default_form_group']; |
||
| 377 | if (isset($struct['admin']['form_groups'][$groupName])) { |
||
| 378 | $formGroup = $struct['admin']['form_groups'][$groupName]; |
||
| 379 | } |
||
| 380 | } elseif (isset($struct['admin']['form_group'])) { |
||
| 381 | if (is_string($struct['admin']['form_group'])) { |
||
| 382 | $groupName = $struct['admin']['form_group']; |
||
| 383 | if (isset($struct['admin']['form_groups'][$groupName])) { |
||
| 384 | $formGroup = $struct['admin']['form_groups'][$groupName]; |
||
| 385 | } |
||
| 386 | } else { |
||
| 387 | $formGroup = $struct['admin']['form_group']; |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | if ($formGroup) { |
||
| 392 | if (is_array($this->rawData)) { |
||
| 393 | $widgetData = array_replace($formGroup, $this->rawData); |
||
| 394 | $this->setData($widgetData); |
||
| 395 | } else { |
||
| 396 | $this->setData($formGroup); |
||
| 397 | } |
||
| 398 | } |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Parse the form group and model properties. |
||
| 404 | * |
||
| 405 | * @return array |
||
| 406 | */ |
||
| 407 | protected function parsedFormProperties() |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Retrieve the object's properties from the form. |
||
| 449 | * |
||
| 450 | * @todo Add support to StructureProperty and StructureFormGroup for multiple-values: |
||
| 451 | * `($store->multiple() ? '%1$s['.uniqid().'][%2$s]' : '%1$s[%2$s]' )`. |
||
| 452 | * @throws UnexpectedValueException If a property data is invalid. |
||
| 453 | * @return \Charcoal\Admin\Widget\FormPropertyWidget[]|\Generator |
||
| 454 | */ |
||
| 455 | public function formProperties() |
||
| 539 | } |
||
| 540 | } |
||
| 541 | } |
||
| 543 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.