| Total Complexity | 43 |
| Total Lines | 209 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Complex classes like HandleRepeaters 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 HandleRepeaters, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | trait HandleRepeaters |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @param \A17\Twill\Models\Model $object |
||
| 14 | * @param array $fields |
||
| 15 | * @return void |
||
| 16 | */ |
||
| 17 | public function afterSaveHandleRepeaters($object, $fields) |
||
| 18 | { |
||
| 19 | if (property_exists($this, 'repeaters')) { |
||
| 20 | foreach ($this->repeaters as $module) { |
||
| 21 | if (is_string($module)) { |
||
| 22 | $model = Str::studly(Str::singular($module)); |
||
| 23 | $repeaterName = Str::singular($module); |
||
| 24 | $this->updateRepeater($object, $fields, $module, $model, $repeaterName); |
||
| 25 | } elseif (is_array($module)) { |
||
| 26 | $relation = !empty($module['relation']) ? $module['relation'] : key($module); |
||
| 27 | $model = isset($module['model']) ? $module['model'] : Str::studly(Str::singular(key($module))); |
||
| 28 | $repeaterName = !empty($module['repeaterName']) ? $module['repeaterName'] : Str::singular(key($module)); |
||
| 29 | $this->updateRepeater($object, $fields, $relation, $model, $repeaterName); |
||
| 30 | } |
||
| 31 | } |
||
| 32 | } |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param \A17\Twill\Models\Model $object |
||
| 37 | * @param array $fields |
||
| 38 | * @return array |
||
| 39 | */ |
||
| 40 | public function getFormFieldsHandleRepeaters($object, $fields) |
||
| 41 | { |
||
| 42 | if (property_exists($this, 'repeaters')) { |
||
| 43 | foreach ($this->repeaters as $module) { |
||
| 44 | if (is_string($module)) { |
||
| 45 | $model = Str::studly(Str::singular($module)); |
||
| 46 | $repeaterName = Str::singular($module); |
||
| 47 | $fields = $this->getFormFieldsForRepeater($object, $fields, $module, $model, $repeaterName); |
||
| 48 | } elseif (is_array($module)) { |
||
| 49 | $model = isset($module['model']) ? $module['model'] : Str::studly(Str::singular(key($module))); |
||
| 50 | $relation = !empty($module['relation']) ? $module['relation'] : key($module); |
||
| 51 | $repeaterName = !empty($module['repeaterName']) ? $module['repeaterName'] : Str::singular(key($module)); |
||
| 52 | $fields = $this->getFormFieldsForRepeater($object, $fields, $relation, $model, $repeaterName); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | return $fields; |
||
| 58 | } |
||
| 59 | |||
| 60 | public function updateRepeaterMany($object, $fields, $relation, $keepExisting = true, $model = null) |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | public function updateRepeater($object, $fields, $relation, $model = null, $repeaterName = null) |
||
| 121 | ]); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | public function getFormFieldsForRepeater($object, $fields, $relation, $model = null, $repeaterName = null) |
||
| 219 | } |
||
| 220 | } |
||
| 221 |