Total Complexity | 44 |
Total Lines | 253 |
Duplicated Lines | 0 % |
Changes | 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 |
||
45 | class ElementalAreasExtension extends DataExtension |
||
46 | { |
||
47 | /** |
||
48 | * Classes to ignore adding elements to |
||
49 | * @config |
||
50 | * @var array $ignored_classes |
||
51 | */ |
||
52 | private static $ignored_classes = []; |
||
53 | |||
54 | /** |
||
55 | * On saving the element area, should Elemental reset the main website |
||
56 | * `$Content` field. |
||
57 | * |
||
58 | * @config |
||
59 | * @var boolean |
||
60 | */ |
||
61 | private static $clear_contentfield = false; |
||
62 | |||
63 | /** |
||
64 | * Whether to sort the elements alphabetically by their title |
||
65 | * |
||
66 | * @config |
||
67 | * @var boolean |
||
68 | */ |
||
69 | private static $sort_types_alphabetically = true; |
||
70 | |||
71 | /** |
||
72 | * Get the available element types for this page type, |
||
73 | * |
||
74 | * Uses allowed_elements, stop_element_inheritance, disallowed_elements in |
||
75 | * order to get to correct list. |
||
76 | * |
||
77 | * @return array |
||
78 | */ |
||
79 | public function getElementalTypes() |
||
80 | { |
||
81 | $config = $this->owner->config(); |
||
82 | |||
83 | if (is_array($config->get('allowed_elements'))) { |
||
84 | if ($config->get('stop_element_inheritance')) { |
||
85 | $availableClasses = $config->get('allowed_elements', Config::UNINHERITED); |
||
86 | } else { |
||
87 | $availableClasses = $config->get('allowed_elements'); |
||
88 | } |
||
89 | } else { |
||
90 | $availableClasses = ClassInfo::subclassesFor(BaseElement::class); |
||
91 | } |
||
92 | |||
93 | if ($config->get('stop_element_inheritance')) { |
||
94 | $disallowedElements = (array) $config->get('disallowed_elements', Config::UNINHERITED); |
||
95 | } else { |
||
96 | $disallowedElements = (array) $config->get('disallowed_elements'); |
||
97 | } |
||
98 | $list = []; |
||
99 | |||
100 | foreach ($availableClasses as $availableClass) { |
||
101 | /** @var BaseElement $inst */ |
||
102 | $inst = singleton($availableClass); |
||
103 | |||
104 | if (!in_array($availableClass, $disallowedElements) && $inst->canCreate()) { |
||
105 | if ($inst->hasMethod('canCreateElement') && !$inst->canCreateElement()) { |
||
106 | continue; |
||
107 | } |
||
108 | |||
109 | $list[$availableClass] = $inst->getType(); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | if ($config->get('sort_types_alphabetically') !== false) { |
||
114 | asort($list); |
||
115 | } |
||
116 | |||
117 | if (isset($list[BaseElement::class])) { |
||
118 | unset($list[BaseElement::class]); |
||
119 | } |
||
120 | |||
121 | $this->owner->invokeWithExtensions('updateAvailableTypesForClass', $class, $list); |
||
122 | |||
123 | return $list; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Returns an array of the relation names to ElementAreas. Ignores any |
||
128 | * has_one fields named `Parent` as that would indicate that this is child |
||
129 | * of an existing area |
||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | public function getElementalRelations() |
||
134 | { |
||
135 | $hasOnes = $this->owner->hasOne(); |
||
136 | |||
137 | if (!$hasOnes) { |
||
138 | return false; |
||
139 | } |
||
140 | |||
141 | $elementalAreaRelations = []; |
||
142 | |||
143 | foreach ($hasOnes as $hasOneName => $hasOneClass) { |
||
144 | if ($hasOneName === 'Parent' || $hasOneName === 'ParentID') { |
||
145 | continue; |
||
146 | } |
||
147 | |||
148 | if ($hasOneClass == ElementalArea::class || is_subclass_of($hasOneClass, ElementalArea::class)) { |
||
149 | $elementalAreaRelations[] = $hasOneName; |
||
150 | } |
||
151 | } |
||
152 | |||
153 | return $elementalAreaRelations; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Setup the CMS Fields |
||
158 | * |
||
159 | * @param FieldList |
||
160 | */ |
||
161 | public function updateCMSFields(FieldList $fields) |
||
162 | { |
||
163 | if (!$this->supportsElemental()) { |
||
164 | return; |
||
165 | } |
||
166 | |||
167 | // add an empty holder for content as some module explicitly use insert |
||
168 | // after content. |
||
169 | $fields->replaceField('Content', new LiteralField('Content', '')); |
||
170 | $elementalAreaRelations = $this->owner->getElementalRelations(); |
||
171 | |||
172 | foreach ($elementalAreaRelations as $eaRelationship) { |
||
173 | $key = $eaRelationship . 'ID'; |
||
174 | |||
175 | // remove the scaffold dropdown |
||
176 | $fields->removeByName($key); |
||
177 | |||
178 | // remove the field, but don't add anything. |
||
179 | if (!$this->owner->isInDb()) { |
||
180 | continue; |
||
181 | } |
||
182 | |||
183 | // Example: $eaRelationship = 'ElementalArea'; |
||
184 | $area = $this->owner->$eaRelationship(); |
||
185 | |||
186 | $editor = ElementalAreaField::create($eaRelationship, $area, $this->getElementalTypes()); |
||
187 | |||
188 | if ($this->owner instanceof SiteTree && $fields->findOrMakeTab('Root.Main')->fieldByName('Metadata')) { |
||
189 | $fields->addFieldToTab('Root.Main', $editor, 'Metadata'); |
||
190 | } else { |
||
191 | $fields->addFieldToTab('Root.Main', $editor); |
||
192 | } |
||
193 | } |
||
194 | |||
195 | return $fields; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Make sure there is always an ElementalArea for adding Elements |
||
200 | */ |
||
201 | public function onBeforeWrite() |
||
202 | { |
||
203 | parent::onBeforeWrite(); |
||
204 | |||
205 | if (!$this->supportsElemental()) { |
||
206 | return; |
||
207 | } |
||
208 | |||
209 | $elementalAreaRelations = $this->owner->getElementalRelations(); |
||
210 | |||
211 | $this->ensureElementalAreasExist($elementalAreaRelations); |
||
212 | |||
213 | if (Config::inst()->get(self::class, 'clear_contentfield')) { |
||
214 | $this->owner->Content = ''; |
||
215 | } |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @return boolean |
||
220 | */ |
||
221 | public function supportsElemental() |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Set all has_one relationships to an ElementalArea to a valid ID if they're unset |
||
246 | * |
||
247 | * @param array $elementalAreaRelations indexed array of relationship names that are to ElementalAreas |
||
248 | * @return DataObject |
||
249 | */ |
||
250 | public function ensureElementalAreasExist($elementalAreaRelations) |
||
251 | { |
||
252 | foreach ($elementalAreaRelations as $eaRelationship) { |
||
253 | $areaID = $eaRelationship . 'ID'; |
||
254 | |||
255 | if (!$this->owner->$areaID) { |
||
256 | $area = ElementalArea::create(); |
||
257 | $area->OwnerClassName = get_class($this->owner); |
||
258 | $area->write(); |
||
259 | $this->owner->$areaID = $area->ID; |
||
260 | } |
||
261 | } |
||
262 | return $this->owner; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Extension hook {@see DataObject::requireDefaultRecords} |
||
267 | * |
||
268 | * @return void |
||
269 | */ |
||
270 | public function requireDefaultRecords() |
||
298 | } |
||
299 | } |
||
300 | } |
||
301 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths