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 |
||
| 16 | class BlockManager extends Object |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Define areas and config on a per theme basis. |
||
| 20 | * |
||
| 21 | * @var array |
||
| 22 | **/ |
||
| 23 | private static $themes = array(); |
||
|
|
|||
| 24 | |||
| 25 | /** |
||
| 26 | * Use default ContentBlock class. |
||
| 27 | * |
||
| 28 | * @var bool |
||
| 29 | **/ |
||
| 30 | private static $use_default_blocks = true; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Show a block area preview button in CMS |
||
| 34 | * |
||
| 35 | * @var bool |
||
| 36 | **/ |
||
| 37 | private static $block_area_preview = true; |
||
| 38 | |||
| 39 | public function __construct() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Gets an array of all areas defined for the current theme. |
||
| 46 | * |
||
| 47 | * @param string $theme |
||
| 48 | * @param bool $keyAsValue |
||
| 49 | * |
||
| 50 | * @return array $areas |
||
| 51 | **/ |
||
| 52 | public function getAreasForTheme($theme = null, $keyAsValue = true) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Gets an array of all areas defined for the current theme that are compatible |
||
| 75 | * with pages of type $class. |
||
| 76 | * |
||
| 77 | * @param string $class |
||
| 78 | * |
||
| 79 | * @return array $areas |
||
| 80 | **/ |
||
| 81 | public function getAreasForPageType($class) |
||
| 127 | |||
| 128 | public function getBlockClasses() |
||
| 156 | |||
| 157 | /* |
||
| 158 | * Get the current/active theme or 'default' to support theme-less sites |
||
| 159 | */ |
||
| 160 | private function getTheme() |
||
| 172 | |||
| 173 | /* |
||
| 174 | * Get the block config for the current theme |
||
| 175 | */ |
||
| 176 | private function getThemeConfig() |
||
| 183 | |||
| 184 | /* |
||
| 185 | * Usage of BlockSets configurable from yaml |
||
| 186 | */ |
||
| 187 | public function getUseBlockSets() |
||
| 193 | |||
| 194 | /* |
||
| 195 | * Exclusion of blocks from page types defined in yaml |
||
| 196 | */ |
||
| 197 | public function getExcludeFromPageTypes() |
||
| 203 | |||
| 204 | /* |
||
| 205 | * getWhiteListedPageTypes optionally configured by the developer |
||
| 206 | */ |
||
| 207 | public function getWhiteListedPageTypes() |
||
| 212 | |||
| 213 | /* |
||
| 214 | * getBlackListedPageTypes optionally configured by the developer |
||
| 215 | * Includes blacklisted page types defined in the old exclude_from_page_types array |
||
| 216 | */ |
||
| 217 | public function getBlackListedPageTypes() |
||
| 224 | |||
| 225 | /* |
||
| 226 | * Usage of extra css classes configurable from yaml |
||
| 227 | */ |
||
| 228 | public function getUseExtraCSSClasses() |
||
| 234 | |||
| 235 | /* |
||
| 236 | * Prefix for the default CSSClasses |
||
| 237 | */ |
||
| 238 | public function getPrefixDefaultCSSClasses() |
||
| 244 | } |
||
| 245 |
This check marks private properties in classes that are never used. Those properties can be removed.