Total Complexity | 52 |
Total Lines | 296 |
Duplicated Lines | 0 % |
Changes | 15 | ||
Bugs | 2 | Features | 0 |
Complex classes like ElementalAreasExtension 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 ElementalAreasExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | class ElementalAreasExtension extends DataExtension |
||
48 | { |
||
49 | use Extensible; |
||
50 | |||
51 | /** |
||
52 | * Classes to ignore adding elements to |
||
53 | * @config |
||
54 | * @var array $ignored_classes |
||
55 | */ |
||
56 | private static $ignored_classes = []; |
||
57 | |||
58 | /** |
||
59 | * On saving the element area, should Elemental reset the main website |
||
60 | * `$Content` field. |
||
61 | * |
||
62 | * @config |
||
63 | * @var boolean |
||
64 | */ |
||
65 | private static $clear_contentfield = false; |
||
66 | |||
67 | /** |
||
68 | * Whether to sort the elements alphabetically by their title |
||
69 | * |
||
70 | * @config |
||
71 | * @var boolean |
||
72 | */ |
||
73 | private static $sort_types_alphabetically = true; |
||
74 | |||
75 | /** |
||
76 | * Whether or not to replace the default SiteTree content field |
||
77 | * Applies globally, across all page types; unless a page type overrides this with its own config setting of |
||
78 | * `elemental_keep_content_field` |
||
79 | * |
||
80 | * @var boolean |
||
81 | * @config |
||
82 | */ |
||
83 | private static $keep_content_fields = false; |
||
84 | |||
85 | /** |
||
86 | * Get the available element types for this page type, |
||
87 | * |
||
88 | * Uses allowed_elements, stop_element_inheritance, disallowed_elements in |
||
89 | * order to get to correct list. |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | public function getElementalTypes() |
||
94 | { |
||
95 | $config = $this->owner->config(); |
||
96 | |||
97 | if (is_array($config->get('allowed_elements'))) { |
||
98 | if ($config->get('stop_element_inheritance')) { |
||
99 | $availableClasses = $config->get('allowed_elements', Config::UNINHERITED); |
||
100 | } else { |
||
101 | $availableClasses = $config->get('allowed_elements'); |
||
102 | } |
||
103 | } else { |
||
104 | $availableClasses = ClassInfo::subclassesFor(BaseElement::class); |
||
105 | } |
||
106 | |||
107 | if ($config->get('stop_element_inheritance')) { |
||
108 | $disallowedElements = (array) $config->get('disallowed_elements', Config::UNINHERITED); |
||
109 | } else { |
||
110 | $disallowedElements = (array) $config->get('disallowed_elements'); |
||
111 | } |
||
112 | $list = []; |
||
113 | |||
114 | foreach ($availableClasses as $availableClass) { |
||
115 | /** @var BaseElement $inst */ |
||
116 | $inst = singleton($availableClass); |
||
117 | |||
118 | if (!in_array($availableClass, $disallowedElements ?? []) && $inst->canCreate()) { |
||
119 | if ($inst->hasMethod('canCreateElement') && !$inst->canCreateElement()) { |
||
|
|||
120 | continue; |
||
121 | } |
||
122 | |||
123 | $list[$availableClass] = $inst->getType(); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | if ($config->get('sort_types_alphabetically') !== false) { |
||
128 | asort($list); |
||
129 | } |
||
130 | |||
131 | if (isset($list[BaseElement::class])) { |
||
132 | unset($list[BaseElement::class]); |
||
133 | } |
||
134 | |||
135 | $class = get_class($this->owner); |
||
136 | $this->owner->invokeWithExtensions('updateAvailableTypesForClass', $class, $list); |
||
137 | |||
138 | return $list; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Returns an array of the relation names to ElementAreas. Ignores any |
||
143 | * has_one fields named `Parent` as that would indicate that this is child |
||
144 | * of an existing area |
||
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | public function getElementalRelations() |
||
149 | { |
||
150 | $hasOnes = $this->owner->hasOne(); |
||
151 | |||
152 | if (!$hasOnes) { |
||
153 | return false; |
||
154 | } |
||
155 | |||
156 | $elementalAreaRelations = []; |
||
157 | |||
158 | foreach ($hasOnes as $hasOneName => $hasOneClass) { |
||
159 | if ($hasOneName === 'Parent' || $hasOneName === 'ParentID') { |
||
160 | continue; |
||
161 | } |
||
162 | |||
163 | if ($hasOneClass == ElementalArea::class || is_subclass_of($hasOneClass, ElementalArea::class)) { |
||
164 | $elementalAreaRelations[] = $hasOneName; |
||
165 | } |
||
166 | } |
||
167 | |||
168 | return $elementalAreaRelations; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Setup the CMS Fields |
||
173 | * |
||
174 | * @param FieldList |
||
175 | */ |
||
176 | public function updateCMSFields(FieldList $fields) |
||
177 | { |
||
178 | if (!$this->supportsElemental()) { |
||
179 | return; |
||
180 | } |
||
181 | |||
182 | // add an empty holder for content as some module explicitly use insert after content |
||
183 | $globalReplace = !Config::inst()->get(self::class, 'keep_content_fields'); |
||
184 | $classOverride = Config::inst()->get(get_class($this->owner), 'elemental_keep_content_field'); |
||
185 | if ($globalReplace && !$classOverride || $classOverride === false) { |
||
186 | $fields->replaceField('Content', new LiteralField('Content', '')); |
||
187 | } |
||
188 | $elementalAreaRelations = $this->owner->getElementalRelations(); |
||
189 | |||
190 | foreach ($elementalAreaRelations as $eaRelationship) { |
||
191 | $key = $eaRelationship . 'ID'; |
||
192 | |||
193 | // remove the scaffold dropdown |
||
194 | $fields->removeByName($key); |
||
195 | |||
196 | // remove the field, but don't add anything. |
||
197 | if (!$this->owner->isInDb()) { |
||
198 | continue; |
||
199 | } |
||
200 | |||
201 | // Example: $eaRelationship = 'ElementalArea'; |
||
202 | $area = $this->owner->$eaRelationship(); |
||
203 | |||
204 | $editor = ElementalAreaField::create($eaRelationship, $area, $this->getElementalTypes()); |
||
205 | |||
206 | if ($this->owner instanceof SiteTree && $fields->findOrMakeTab('Root.Main')->fieldByName('Metadata')) { |
||
207 | $fields->addFieldToTab('Root.Main', $editor, 'Metadata'); |
||
208 | } else { |
||
209 | $fields->addFieldToTab('Root.Main', $editor); |
||
210 | } |
||
211 | } |
||
212 | |||
213 | return $fields; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Make sure there is always an ElementalArea for adding Elements |
||
218 | */ |
||
219 | public function onBeforeWrite() |
||
244 | } |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @return boolean |
||
249 | */ |
||
250 | public function supportsElemental() |
||
251 | { |
||
252 | if ($this->owner->hasMethod('includeElemental')) { |
||
253 | $res = $this->owner->includeElemental(); |
||
254 | |||
255 | if ($res !== null) { |
||
256 | return $res; |
||
257 | } |
||
258 | } |
||
259 | |||
260 | if (is_a($this->owner, RedirectorPage::class) || is_a($this->owner, VirtualPage::class)) { |
||
261 | return false; |
||
262 | } elseif ($ignored = Config::inst()->get(ElementalPageExtension::class, 'ignored_classes')) { |
||
263 | foreach ($ignored as $check) { |
||
264 | if (is_a($this->owner, $check ?? '')) { |
||
265 | return false; |
||
266 | } |
||
267 | } |
||
268 | } |
||
269 | |||
270 | return true; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Set all has_one relationships to an ElementalArea to a valid ID if they're unset |
||
275 | * |
||
276 | * @param array $elementalAreaRelations indexed array of relationship names that are to ElementalAreas |
||
277 | * @return DataObject |
||
278 | */ |
||
279 | public function ensureElementalAreasExist($elementalAreaRelations) |
||
280 | { |
||
281 | foreach ($elementalAreaRelations as $eaRelationship) { |
||
282 | $areaID = $eaRelationship . 'ID'; |
||
283 | |||
284 | if (!$this->owner->$areaID) { |
||
285 | $area = ElementalArea::create(); |
||
286 | $area->OwnerClassName = get_class($this->owner); |
||
287 | $area->write(); |
||
288 | $this->owner->$areaID = $area->ID; |
||
289 | } |
||
290 | } |
||
291 | return $this->owner; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * Extension hook {@see DataObject::requireDefaultRecords} |
||
296 | * |
||
297 | * @return void |
||
298 | */ |
||
299 | public function requireDefaultRecords() |
||
343 | } |
||
344 | } |
||
345 |