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 |
||
| 7 | class BlockManager extends Object |
||
|
|
|||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Define areas and config on a per theme basis. |
||
| 11 | * |
||
| 12 | * @var array |
||
| 13 | **/ |
||
| 14 | private static $themes = array(); |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Use default ContentBlock class. |
||
| 18 | * |
||
| 19 | * @var bool |
||
| 20 | **/ |
||
| 21 | private static $use_default_blocks = true; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Show a block area preview button in CMS |
||
| 25 | * |
||
| 26 | * @var bool |
||
| 27 | **/ |
||
| 28 | private static $block_area_preview = true; |
||
| 29 | |||
| 30 | public function __construct() |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Gets an array of all areas defined for the current theme. |
||
| 37 | * |
||
| 38 | * @param string $theme |
||
| 39 | * @param bool $keyAsValue |
||
| 40 | * |
||
| 41 | * @return array $areas |
||
| 42 | **/ |
||
| 43 | public function getAreasForTheme($theme = null, $keyAsValue = true) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Gets an array of all areas defined for the current theme that are compatible |
||
| 66 | * with pages of type $class. |
||
| 67 | * |
||
| 68 | * @param string $class |
||
| 69 | * |
||
| 70 | * @return array $areas |
||
| 71 | **/ |
||
| 72 | public function getAreasForPageType($class) |
||
| 118 | |||
| 119 | public function getBlockClasses() |
||
| 147 | |||
| 148 | /* |
||
| 149 | * Get the current/active theme or 'default' to support theme-less sites |
||
| 150 | */ |
||
| 151 | private function getTheme() |
||
| 163 | |||
| 164 | /* |
||
| 165 | * Get the block config for the current theme |
||
| 166 | */ |
||
| 167 | private function getThemeConfig() |
||
| 174 | |||
| 175 | /* |
||
| 176 | * Usage of BlockSets configurable from yaml |
||
| 177 | */ |
||
| 178 | public function getUseBlockSets() |
||
| 184 | |||
| 185 | /* |
||
| 186 | * Exclusion of blocks from page types defined in yaml |
||
| 187 | */ |
||
| 188 | public function getExcludeFromPageTypes() |
||
| 194 | |||
| 195 | /* |
||
| 196 | * getWhiteListedPageTypes optionally configured by the developer |
||
| 197 | */ |
||
| 198 | public function getWhiteListedPageTypes() |
||
| 203 | |||
| 204 | /* |
||
| 205 | * getBlackListedPageTypes optionally configured by the developer |
||
| 206 | * Includes blacklisted page types defined in the old exclude_from_page_types array |
||
| 207 | */ |
||
| 208 | public function getBlackListedPageTypes() |
||
| 215 | |||
| 216 | /* |
||
| 217 | * Usage of extra css classes configurable from yaml |
||
| 218 | */ |
||
| 219 | public function getUseExtraCSSClasses() |
||
| 225 | |||
| 226 | /* |
||
| 227 | * Prefix for the default CSSClasses |
||
| 228 | */ |
||
| 229 | public function getPrefixDefaultCSSClasses() |
||
| 235 | } |
||
| 236 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.