Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BlockManager 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 BlockManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class BlockManager extends ViewableData |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Use default ContentBlock class. |
||
| 22 | * |
||
| 23 | * @var bool |
||
| 24 | **/ |
||
| 25 | private static $use_default_blocks = true; |
||
|
|
|||
| 26 | |||
| 27 | /** |
||
| 28 | * Show a block area preview button in CMS |
||
| 29 | * |
||
| 30 | * @var bool |
||
| 31 | **/ |
||
| 32 | private static $block_area_preview = true; |
||
| 33 | |||
| 34 | public function __construct() |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Gets an array of all areas defined for blocks. |
||
| 41 | * |
||
| 42 | * @param bool $keyAsValue |
||
| 43 | * |
||
| 44 | * @return array $areas |
||
| 45 | **/ |
||
| 46 | public function getAreas($keyAsValue = true) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Gets an array of all areas defined that are compatible with pages of type $class. |
||
| 62 | * |
||
| 63 | * @param string $class |
||
| 64 | * |
||
| 65 | * @return array $areas |
||
| 66 | **/ |
||
| 67 | public function getAreasForPageType($class) |
||
| 113 | |||
| 114 | public function getBlockClasses() |
||
| 142 | |||
| 143 | /* |
||
| 144 | * Usage of BlockSets configurable from yaml |
||
| 145 | */ |
||
| 146 | public function getUseBlockSets() |
||
| 152 | |||
| 153 | /* |
||
| 154 | * Exclusion of blocks from page types defined in yaml |
||
| 155 | */ |
||
| 156 | public function getExcludeFromPageTypes() |
||
| 162 | |||
| 163 | /* |
||
| 164 | * getWhiteListedPageTypes optionally configured by the developer |
||
| 165 | */ |
||
| 166 | public function getWhiteListedPageTypes() |
||
| 171 | |||
| 172 | /* |
||
| 173 | * getBlackListedPageTypes optionally configured by the developer |
||
| 174 | * Includes blacklisted page types defined in the old exclude_from_page_types array |
||
| 175 | */ |
||
| 176 | public function getBlackListedPageTypes() |
||
| 183 | |||
| 184 | /* |
||
| 185 | * Usage of extra css classes configurable from yaml |
||
| 186 | */ |
||
| 187 | public function getUseExtraCSSClasses() |
||
| 193 | |||
| 194 | /* |
||
| 195 | * Prefix for the default CSSClasses |
||
| 196 | */ |
||
| 197 | public function getPrefixDefaultCSSClasses() |
||
| 203 | } |
||
| 204 |
This check marks private properties in classes that are never used. Those properties can be removed.