Complex classes like BlocksSiteTreeExtension 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 BlocksSiteTreeExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | use SheaDawson\Blocks\forms\GridFieldConfigBlockManager; |
||
| 8 | |||
| 9 | use SilverStripe\CMS\Model\SiteTreeExtension; |
||
| 10 | |||
| 11 | use SilverStripe\Forms\ReadonlyField; |
||
| 12 | use SilverStripe\Forms\CheckboxField; |
||
| 13 | use SilverStripe\Forms\FieldList; |
||
| 14 | |||
| 15 | use SilverStripe\View\SSViewer; |
||
| 16 | |||
| 17 | use SilverStripe\ORM\ArrayList; |
||
| 18 | |||
| 19 | use SilverStripe\Security\Permission; |
||
| 20 | |||
| 21 | use SilverStripe\Control\Controller; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * BlocksSiteTreeExtension. |
||
| 25 | * |
||
| 26 | * @author Shea Dawson <[email protected]> |
||
| 27 | */ |
||
| 28 | class BlocksSiteTreeExtension extends SiteTreeExtension |
||
| 29 | { |
||
| 30 | private static $db = array( |
||
|
|
|||
| 31 | 'InheritBlockSets' => 'Boolean', |
||
| 32 | ); |
||
| 33 | private static $many_many = array( |
||
| 34 | "Blocks" => "SheaDawson\Blocks\model\Block", |
||
| 35 | "DisabledBlocks" => "SheaDawson\Blocks\model\Block", |
||
| 36 | ); |
||
| 37 | public static $many_many_extraFields = array( |
||
| 38 | 'Blocks' => array( |
||
| 39 | 'Sort' => 'Int', |
||
| 40 | 'BlockArea' => 'Varchar', |
||
| 41 | ), |
||
| 42 | ); |
||
| 43 | private static $defaults = array( |
||
| 44 | 'InheritBlockSets' => 1, |
||
| 45 | ); |
||
| 46 | private static $dependencies = array( |
||
| 47 | 'blockManager' => '%$SheaDawson\\Blocks\\BlockManager', |
||
| 48 | ); |
||
| 49 | |||
| 50 | public $blockManager; |
||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * Check if the Blocks CMSFields should be displayed for this Page |
||
| 55 | * |
||
| 56 | * @return boolean |
||
| 57 | **/ |
||
| 58 | public function showBlocksFields() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Block manager for Pages. |
||
| 80 | * */ |
||
| 81 | public function updateCMSFields(FieldList $fields) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Called from templates to get rendered blocks for the given area. |
||
| 147 | * |
||
| 148 | * @param string $area |
||
| 149 | * @param int $limit Limit the items to this number, or null for no limit |
||
| 150 | */ |
||
| 151 | public function BlockArea($area, $limit = null) |
||
| 184 | |||
| 185 | public function HasBlockArea($area) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get a merged list of all blocks on this page and ones inherited from BlockSets. |
||
| 204 | * |
||
| 205 | * @param string|null $area filter by block area |
||
| 206 | * @param bool $includeDisabled Include blocks that have been explicitly excluded from this page |
||
| 207 | * i.e. blocks from block sets added to the "disable inherited blocks" list |
||
| 208 | * |
||
| 209 | * @return ArrayList |
||
| 210 | * */ |
||
| 211 | public function getBlockList($area = null, $includeDisabled = false) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get Any BlockSets that apply to this page. |
||
| 250 | * |
||
| 251 | * @return ArrayList |
||
| 252 | * */ |
||
| 253 | public function getAppliedSets() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get all Blocks from BlockSets that apply to this page. |
||
| 291 | * |
||
| 292 | * @return ArrayList |
||
| 293 | * */ |
||
| 294 | public function getBlocksFromAppliedBlockSets($area = null, $includeDisabled = false) |
||
| 295 | { |
||
| 296 | $blocks = ArrayList::create(); |
||
| 297 | $sets = $this->getAppliedSets(); |
||
| 298 | |||
| 299 | if (!$sets->count()) { |
||
| 300 | return $blocks; |
||
| 301 | } |
||
| 302 | |||
| 303 | foreach ($sets as $set) { |
||
| 304 | $setBlocks = $set->Blocks()->sort('Sort DESC'); |
||
| 305 | |||
| 306 | if (!$includeDisabled) { |
||
| 307 | $setBlocks = $setBlocks->exclude('ID', $this->owner->DisabledBlocks()->column('ID')); |
||
| 308 | } |
||
| 309 | |||
| 310 | if ($area) { |
||
| 311 | $setBlocks = $setBlocks->filter('BlockArea', $area); |
||
| 312 | } |
||
| 313 | |||
| 314 | $blocks->merge($setBlocks); |
||
| 315 | } |
||
| 316 | |||
| 317 | $blocks->removeDuplicates(); |
||
| 318 | |||
| 319 | return $blocks; |
||
| 320 | } |
||
| 342 |
This check marks private properties in classes that are never used. Those properties can be removed.