| Total Complexity | 54 | 
| Total Lines | 306 | 
| 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  | 
            ||
| 46 | class ElementalAreasExtension extends DataExtension  | 
            ||
| 47 | { | 
            ||
| 48 | use Extensible;  | 
            ||
| 49 | |||
| 50 | /**  | 
            ||
| 51 | * Classes to ignore adding elements to  | 
            ||
| 52 | * @config  | 
            ||
| 53 | * @var array $ignored_classes  | 
            ||
| 54 | */  | 
            ||
| 55 | private static $ignored_classes = [];  | 
            ||
| 56 | |||
| 57 | /**  | 
            ||
| 58 | * On saving the element area, should Elemental reset the main website  | 
            ||
| 59 | * `$Content` field.  | 
            ||
| 60 | *  | 
            ||
| 61 | * @config  | 
            ||
| 62 | * @var boolean  | 
            ||
| 63 | */  | 
            ||
| 64 | private static $clear_contentfield = false;  | 
            ||
| 65 | |||
| 66 | /**  | 
            ||
| 67 | * Whether to sort the elements alphabetically by their title  | 
            ||
| 68 | *  | 
            ||
| 69 | * @config  | 
            ||
| 70 | * @var boolean  | 
            ||
| 71 | */  | 
            ||
| 72 | private static $sort_types_alphabetically = true;  | 
            ||
| 73 | |||
| 74 | /**  | 
            ||
| 75 | * Whether or not to replace the default SiteTree content field  | 
            ||
| 76 | * Applies globally, across all page types; unless a page type overrides this with its own config setting of  | 
            ||
| 77 | * `elemental_keep_content_field`  | 
            ||
| 78 | *  | 
            ||
| 79 | * @var boolean  | 
            ||
| 80 | * @config  | 
            ||
| 81 | */  | 
            ||
| 82 | private static $keep_content_fields = false;  | 
            ||
| 83 | |||
| 84 | /**  | 
            ||
| 85 | * Get the available element types for this page type,  | 
            ||
| 86 | *  | 
            ||
| 87 | * Uses allowed_elements, stop_element_inheritance, disallowed_elements in  | 
            ||
| 88 | * order to get to correct list.  | 
            ||
| 89 | *  | 
            ||
| 90 | * @return array  | 
            ||
| 91 | */  | 
            ||
| 92 | public function getElementalTypes()  | 
            ||
| 93 |     { | 
            ||
| 94 | $config = $this->owner->config();  | 
            ||
| 95 | |||
| 96 |         if (is_array($config->get('allowed_elements'))) { | 
            ||
| 97 |             if ($config->get('stop_element_inheritance')) { | 
            ||
| 98 |                 $availableClasses = $config->get('allowed_elements', Config::UNINHERITED); | 
            ||
| 99 |             } else { | 
            ||
| 100 |                 $availableClasses = $config->get('allowed_elements'); | 
            ||
| 101 | }  | 
            ||
| 102 |         } else { | 
            ||
| 103 | $availableClasses = ClassInfo::subclassesFor(BaseElement::class);  | 
            ||
| 104 | }  | 
            ||
| 105 | |||
| 106 |         if ($config->get('stop_element_inheritance')) { | 
            ||
| 107 |             $disallowedElements = (array) $config->get('disallowed_elements', Config::UNINHERITED); | 
            ||
| 108 |         } else { | 
            ||
| 109 |             $disallowedElements = (array) $config->get('disallowed_elements'); | 
            ||
| 110 | }  | 
            ||
| 111 | $list = [];  | 
            ||
| 112 | |||
| 113 |         foreach ($availableClasses as $availableClass) { | 
            ||
| 114 | /** @var BaseElement $inst */  | 
            ||
| 115 | $inst = singleton($availableClass);  | 
            ||
| 116 | |||
| 117 |             if (!in_array($availableClass, $disallowedElements) && $inst->canCreate()) { | 
            ||
| 118 |                 if ($inst->hasMethod('canCreateElement') && !$inst->canCreateElement()) { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 119 | continue;  | 
            ||
| 120 | }  | 
            ||
| 121 | |||
| 122 | $list[$availableClass] = $inst->getType();  | 
            ||
| 123 | }  | 
            ||
| 124 | }  | 
            ||
| 125 | |||
| 126 |         if ($config->get('sort_types_alphabetically') !== false) { | 
            ||
| 127 | asort($list);  | 
            ||
| 128 | }  | 
            ||
| 129 | |||
| 130 |         if (isset($list[BaseElement::class])) { | 
            ||
| 131 | unset($list[BaseElement::class]);  | 
            ||
| 132 | }  | 
            ||
| 133 | |||
| 134 | $class = get_class($this->owner);  | 
            ||
| 135 |         $this->owner->invokeWithExtensions('updateAvailableTypesForClass', $class, $list); | 
            ||
| 136 | |||
| 137 | return $list;  | 
            ||
| 138 | }  | 
            ||
| 139 | |||
| 140 | /**  | 
            ||
| 141 | * Returns an array of the relation names to ElementAreas. Ignores any  | 
            ||
| 142 | * has_one fields named `Parent` as that would indicate that this is child  | 
            ||
| 143 | * of an existing area  | 
            ||
| 144 | *  | 
            ||
| 145 | * @return array  | 
            ||
| 146 | */  | 
            ||
| 147 | public function getElementalRelations()  | 
            ||
| 148 |     { | 
            ||
| 149 | $hasOnes = $this->owner->hasOne();  | 
            ||
| 150 | |||
| 151 |         if (!$hasOnes) { | 
            ||
| 152 | return false;  | 
            ||
| 153 | }  | 
            ||
| 154 | |||
| 155 | $elementalAreaRelations = [];  | 
            ||
| 156 | |||
| 157 |         foreach ($hasOnes as $hasOneName => $hasOneClass) { | 
            ||
| 158 |             if ($hasOneName === 'Parent' || $hasOneName === 'ParentID') { | 
            ||
| 159 | continue;  | 
            ||
| 160 | }  | 
            ||
| 161 | |||
| 162 |             if ($hasOneClass == ElementalArea::class || is_subclass_of($hasOneClass, ElementalArea::class)) { | 
            ||
| 163 | $elementalAreaRelations[] = $hasOneName;  | 
            ||
| 164 | }  | 
            ||
| 165 | }  | 
            ||
| 166 | |||
| 167 | return $elementalAreaRelations;  | 
            ||
| 168 | }  | 
            ||
| 169 | |||
| 170 | /**  | 
            ||
| 171 | * Setup the CMS Fields  | 
            ||
| 172 | *  | 
            ||
| 173 | * @param FieldList  | 
            ||
| 174 | */  | 
            ||
| 175 | public function updateCMSFields(FieldList $fields)  | 
            ||
| 213 | }  | 
            ||
| 214 | |||
| 215 | /**  | 
            ||
| 216 | * Make sure there is always an ElementalArea for adding Elements  | 
            ||
| 217 | */  | 
            ||
| 218 | public function onBeforeWrite()  | 
            ||
| 253 | }  | 
            ||
| 254 | }  | 
            ||
| 255 | |||
| 256 | /**  | 
            ||
| 257 | * @return boolean  | 
            ||
| 258 | */  | 
            ||
| 259 | public function supportsElemental()  | 
            ||
| 260 |     { | 
            ||
| 261 |         if ($this->owner->hasMethod('includeElemental')) { | 
            ||
| 262 | $res = $this->owner->includeElemental();  | 
            ||
| 263 | |||
| 264 |             if ($res !== null) { | 
            ||
| 265 | return $res;  | 
            ||
| 266 | }  | 
            ||
| 267 | }  | 
            ||
| 268 | |||
| 269 |         if (is_a($this->owner, RedirectorPage::class) || is_a($this->owner, VirtualPage::class)) { | 
            ||
| 270 | return false;  | 
            ||
| 271 |         } elseif ($ignored = Config::inst()->get(ElementalPageExtension::class, 'ignored_classes')) { | 
            ||
| 272 |             foreach ($ignored as $check) { | 
            ||
| 273 |                 if (is_a($this->owner, $check)) { | 
            ||
| 274 | return false;  | 
            ||
| 275 | }  | 
            ||
| 276 | }  | 
            ||
| 277 | }  | 
            ||
| 278 | |||
| 279 | return true;  | 
            ||
| 280 | }  | 
            ||
| 281 | |||
| 282 | /**  | 
            ||
| 283 | * Set all has_one relationships to an ElementalArea to a valid ID if they're unset  | 
            ||
| 284 | *  | 
            ||
| 285 | * @param array $elementalAreaRelations indexed array of relationship names that are to ElementalAreas  | 
            ||
| 286 | * @return DataObject  | 
            ||
| 287 | */  | 
            ||
| 288 | public function ensureElementalAreasExist($elementalAreaRelations)  | 
            ||
| 289 |     { | 
            ||
| 290 |         foreach ($elementalAreaRelations as $eaRelationship) { | 
            ||
| 291 | $areaID = $eaRelationship . 'ID';  | 
            ||
| 292 | |||
| 293 |             if (!$this->owner->$areaID) { | 
            ||
| 294 | $area = ElementalArea::create();  | 
            ||
| 295 | $area->OwnerClassName = get_class($this->owner);  | 
            ||
| 296 | $area->write();  | 
            ||
| 297 | $this->owner->$areaID = $area->ID;  | 
            ||
| 298 | }  | 
            ||
| 299 | }  | 
            ||
| 300 | return $this->owner;  | 
            ||
| 301 | }  | 
            ||
| 302 | |||
| 303 | /**  | 
            ||
| 304 |      * Extension hook {@see DataObject::requireDefaultRecords} | 
            ||
| 305 | *  | 
            ||
| 306 | * @return void  | 
            ||
| 307 | */  | 
            ||
| 308 | public function requireDefaultRecords()  | 
            ||
| 352 | }  | 
            ||
| 353 | }  | 
            ||
| 354 |