Complex classes like ComponentLibrary 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 ComponentLibrary, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class ComponentLibrary { |
||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | const DEFAULT_ATTRIBUTES = [ 'class', 'id', 'style' ]; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * File, that holds the component definition list. |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | const DEFINITIONS_FILE = __DIR__ . DIRECTORY_SEPARATOR . 'ComponentsDefinition.json'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | const HANDLER_TYPE_PARSER_FUNCTION = 'function'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | const HANDLER_TYPE_TAG_EXTENSION = 'tag'; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | const PARSER_HOOK_PREFIX = 'bootstrap_'; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * This array holds all the data for all known components, whether they are registered or not. |
||
| 69 | * |
||
| 70 | * Array has form |
||
| 71 | * <pre> |
||
| 72 | * "componentIdentifier" => [ |
||
| 73 | * "class" => <className>, |
||
| 74 | * "name" => <componentName> |
||
| 75 | * "handlerType" => <handlerType>, |
||
| 76 | * "attributes" => [ "attr1", "attr2", ... ], |
||
| 77 | * "aliases" => [ "alias" => "attribute", ... ] |
||
| 78 | * "modules" => [ |
||
| 79 | * "default" => [ "module1", "module2", ... ], |
||
| 80 | * "<skin>" => [ "module1", "module2", ... ], |
||
| 81 | * ] |
||
| 82 | * ] |
||
| 83 | * </pre> |
||
| 84 | * |
||
| 85 | * @var array $componentDataStore |
||
| 86 | */ |
||
| 87 | private $componentDataStore; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The list of registered/allowed bootstrap components, name or alias |
||
| 91 | * |
||
| 92 | * @var string[] $registeredComponents |
||
| 93 | */ |
||
| 94 | private $registeredComponents; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $componentName |
||
| 98 | * |
||
| 99 | 15 | * @return string |
|
| 100 | 15 | */ |
|
| 101 | public static function compileParserHookStringFor( $componentName ) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * ComponentLibrary constructor. |
||
| 107 | * |
||
| 108 | * Do not instantiate directly, but use {@see ApplicationFactory::getComponentLibrary} instead. |
||
| 109 | * |
||
| 110 | * @param bool|array $componentWhiteList (see {@see \BootstrapComponents\ComponentLibrary::$componentWhiteList}) |
||
| 111 | * |
||
| 112 | * @throws \ConfigException cascading {@see \ConfigFactory::makeConfig} and |
||
| 113 | * @see ApplicationFactory::getComponentLibrary |
||
| 114 | 87 | * |
|
| 115 | */ |
||
| 116 | 87 | public function __construct( $componentWhiteList = null ) { |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Compiles an array for all bootstrap component parser functions to be uses in the BootstrapComponents.magic.php file. |
||
| 131 | * |
||
| 132 | * @return array |
||
| 133 | 1 | */ |
|
| 134 | 1 | public function compileMagicWordsArray() { |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Checks, if component $componentIdentifier is registered |
||
| 147 | * |
||
| 148 | * @param string $componentIdentifier |
||
| 149 | * |
||
| 150 | * @return bool |
||
| 151 | 46 | */ |
|
| 152 | 46 | public function isRegistered( $componentIdentifier ) { |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Returns the defined/allowed attribute aliases for component/alias $componentIdentifier. |
||
| 158 | * |
||
| 159 | * @param string $componentIdentifier |
||
| 160 | * |
||
| 161 | 21 | * @return array |
|
| 162 | 21 | * @throws MWException provided component is not known |
|
| 163 | 1 | */ |
|
| 164 | public function getAliasesFor( $componentIdentifier ) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Returns the defined/allowed attributes for component/alias $componentIdentifier. |
||
| 170 | * |
||
| 171 | * @param string $componentIdentifier |
||
| 172 | * |
||
| 173 | * @return array |
||
| 174 | * @throws MWException provided component is not known |
||
| 175 | */ |
||
| 176 | 32 | public function getAttributesFor( $componentIdentifier ) { |
|
| 179 | |||
| 180 | 30 | /** |
|
| 181 | * Returns class name for a registered component/alias. |
||
| 182 | * |
||
| 183 | * @param string $componentIdentifier |
||
| 184 | * |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function getClassFor( $componentIdentifier ) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | 38 | * Returns handler type for a registered component/alias. 'UNKNOWN' if unknown component. |
|
| 193 | 38 | * |
|
| 194 | 1 | * @param string $componentIdentifier |
|
| 195 | * |
||
| 196 | 37 | * @return string |
|
| 197 | * @see \BootstrapComponents\ComponentLibrary::HANDLER_TYPE_PARSER_FUNCTION, |
||
| 198 | * \BootstrapComponents\ComponentLibrary::HANDLER_TYPE_TAG_EXTENSION |
||
| 199 | * |
||
| 200 | */ |
||
| 201 | public function getHandlerTypeFor( $componentIdentifier ) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Returns an array of all the known components' names, _excluding_ aliases, |
||
| 211 | * |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | public function getKnownComponents() { |
||
| 217 | 30 | ||
| 218 | 30 | /** |
|
| 219 | 30 | * Returns all the needed modules for a component/alias. False, if there are none. |
|
| 220 | 30 | * If skin is set, returns all modules especially registered for that skin as well |
|
| 221 | 30 | * |
|
| 222 | 24 | * @param string $componentIdentifier |
|
| 223 | * @param string $skin |
||
| 224 | 11 | * |
|
| 225 | 11 | * @return array |
|
| 226 | 11 | */ |
|
| 227 | 11 | public function getModulesFor( $componentIdentifier, $skin = null ) { |
|
| 241 | |||
| 242 | 31 | /** |
|
| 243 | * Returns the component name for a given class. |
||
| 244 | * |
||
| 245 | * @param string $componentClass |
||
| 246 | * |
||
| 247 | * @throws MWException if supplied class is not registered |
||
| 248 | * |
||
| 249 | * @return string |
||
| 250 | 8 | */ |
|
| 251 | 8 | public function getNameFor( $componentClass ) { |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Returns an array of all the registered component's names. Including aliases. |
||
| 269 | * |
||
| 270 | * @return string[] |
||
| 271 | */ |
||
| 272 | 6 | public function getRegisteredComponents() { |
|
| 275 | |||
| 276 | /** |
||
| 277 | * True, if referenced component is registered as parser function. |
||
| 278 | * |
||
| 279 | * @param string $componentName |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | 87 | public function isParserFunction( $componentName ) { |
|
| 286 | |||
| 287 | 3 | /** |
|
| 288 | 3 | * True, if referenced component is registered as tag extension. |
|
| 289 | 3 | * |
|
| 290 | 3 | * @param string $componentName |
|
| 291 | 3 | * |
|
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | public function isTagExtension( $componentName ) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $componentIdentifier |
||
| 300 | * @param string $field |
||
| 301 | * |
||
| 302 | 87 | * @throws MWException on non existing $componentIdentifier or $field |
|
| 303 | 87 | * @return mixed |
|
| 304 | 87 | */ |
|
| 305 | 87 | protected function accessComponentDataStore( $componentIdentifier, $field ) { |
|
| 313 | 87 | ||
| 314 | /** |
||
| 315 | * Sees to it, that the whitelist (if it is an array) contains only lowercase strings. |
||
| 316 | * |
||
| 317 | * @param bool|array $componentWhiteList |
||
| 318 | * |
||
| 319 | * @return bool|array |
||
| 320 | */ |
||
| 321 | private function mangle( $componentWhiteList ) { |
||
| 331 | |||
| 332 | 87 | /** |
|
| 333 | * This adds the default attributes to the attribute list. |
||
| 334 | 4 | * |
|
| 335 | * @param array $componentAttributes |
||
| 336 | * |
||
| 337 | 86 | * @return array |
|
| 338 | 86 | */ |
|
| 339 | 87 | private function normalizeAttributes( $componentAttributes ) { |
|
| 349 | 87 | ||
| 350 | /** |
||
| 351 | * Generates the array for registered components containing all whitelisted component identifiers |
||
| 352 | 87 | * |
|
| 353 | 87 | * @param bool|array $componentWhiteList |
|
| 354 | * |
||
| 355 | 87 | * @return string[] list of registered component identifiers |
|
| 356 | 87 | */ |
|
| 357 | 87 | private function registerComponents( $componentWhiteList ) { |
|
| 369 | 87 | ||
| 370 | /** |
||
| 371 | 87 | * Raw library data used in registration process. |
|
| 372 | 87 | * |
|
| 373 | * @return array |
||
| 374 | 87 | */ |
|
| 375 | 87 | private function getComponentDataStore() { |
|
| 407 | } |
||
| 408 |