Complex classes like CallableRepository 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 CallableRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class CallableRepository |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * The registered namespaces |
||
| 27 | * |
||
| 28 | * These are the namespaces specified when registering directories. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $aNamespaceOptions = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The registered classes |
||
| 36 | * |
||
| 37 | * These are registered classes, and classes in directories registered without a namespace. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $aClassOptions = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The namespaces |
||
| 45 | * |
||
| 46 | * These are all the namespaces found in registered directories |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $aNamespaces = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The created callable objects |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $aCallableObjects = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The options to be applied to callable objects |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $aCallableOptions = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * If the underscore is used as separator in js class names |
||
| 68 | * |
||
| 69 | * @var boolean |
||
| 70 | */ |
||
| 71 | protected $bUsingUnderscore = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The Composer autoloader |
||
| 75 | * |
||
| 76 | * @var Autoloader |
||
| 77 | */ |
||
| 78 | private $xAutoloader = null; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The class constructor |
||
| 82 | */ |
||
| 83 | public function __construct() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * |
||
| 95 | * @param string $sClassName The name of the class being registered |
||
| 96 | * @param array|string $aOptions The associated options |
||
| 97 | * |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | public function addClass($sClassName, $aOptions) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get a given class options from specified directory options |
||
| 108 | * |
||
| 109 | * @param string $sClassName The name of the class |
||
| 110 | * @param array $aDirectoryOptions The directory options |
||
| 111 | * @param array $aDefaultOptions The default options |
||
| 112 | * |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | private function getClassOptions($sClassName, array $aDirectoryOptions, array $aDefaultOptions = []) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * |
||
| 140 | * @param string $sDirectory The directory being registered |
||
| 141 | * @param array $aOptions The associated options |
||
| 142 | * |
||
| 143 | * @return void |
||
| 144 | */ |
||
| 145 | public function addDirectory($sDirectory, $aOptions) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * |
||
| 173 | * @param string $sNamespace The namespace of the directory being registered |
||
| 174 | * @param array $aOptions The associated options |
||
| 175 | * |
||
| 176 | * @return void |
||
| 177 | */ |
||
| 178 | public function addNamespace($sNamespace, array $aOptions) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Find a class name is register with Jaxon::CALLABLE_CLASS type |
||
| 210 | * |
||
| 211 | * @param string $sClassName The class name of the callable object |
||
| 212 | * |
||
| 213 | * @return array|null |
||
| 214 | */ |
||
| 215 | private function getOptionsFromClass($sClassName) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Find a class name is register with Jaxon::CALLABLE_DIR type |
||
| 226 | * |
||
| 227 | * @param string $sClassName The class name of the callable object |
||
| 228 | * @param string|null $sNamespace The namespace |
||
| 229 | * |
||
| 230 | * @return array|null |
||
| 231 | */ |
||
| 232 | private function getOptionsFromNamespace($sClassName, $sNamespace = null) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Find a callable object by class name |
||
| 263 | * |
||
| 264 | * @param string $sClassName The class name of the callable object |
||
| 265 | * @param array $aOptions The callable object options |
||
| 266 | * |
||
| 267 | * @return object |
||
| 268 | */ |
||
| 269 | protected function _getCallableObject($sClassName, array $aOptions) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Find a callable object by class name |
||
| 316 | * |
||
| 317 | * @param string $sClassName The class name of the callable object |
||
| 318 | * |
||
| 319 | * @return object |
||
| 320 | */ |
||
| 321 | public function getCallableObject($sClassName) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Create callable objects for all registered namespaces |
||
| 352 | * |
||
| 353 | * @return void |
||
| 354 | */ |
||
| 355 | public function createCallableObjects() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Find a user registered callable object by class name |
||
| 415 | * |
||
| 416 | * @param string $sClassName The class name of the callable object |
||
| 417 | * |
||
| 418 | * @return object |
||
| 419 | */ |
||
| 420 | protected function getRegisteredObject($sClassName) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Get all registered namespaces |
||
| 429 | * |
||
| 430 | * @return array |
||
| 431 | */ |
||
| 432 | public function getNamespaces() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Get all registered callable objects |
||
| 439 | * |
||
| 440 | * @return array |
||
| 441 | */ |
||
| 442 | public function getCallableObjects() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Get all registered callable objects options |
||
| 449 | * |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | public function getCallableOptions() |
||
| 456 | } |
||
| 457 |