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 |
||
| 22 | class CallableRepository |
||
| 23 | { |
||
| 24 | use \Jaxon\Utils\Traits\Config; |
||
| 25 | use \Jaxon\Utils\Traits\Template; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The registered namespaces |
||
| 29 | * |
||
| 30 | * These are the namespaces specified when registering directories. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $aNamespaceOptions = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The registered classes |
||
| 38 | * |
||
| 39 | * These are registered classes, and classes in directories registered without a namespace. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $aClassOptions = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The namespaces |
||
| 47 | * |
||
| 48 | * These are all the namespaces found in registered directories |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $aNamespaces = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The created callable objects |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $aCallableObjects = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The options to be applied to callable objects |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $aCallableOptions = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * |
||
| 70 | * @param string $sClassName The name of the class being registered |
||
| 71 | * @param array|string $aOptions The associated options |
||
| 72 | * |
||
| 73 | * @return void |
||
| 74 | */ |
||
| 75 | public function addClass($sClassName, $aOptions) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get a given class options from specified directory options |
||
| 84 | * |
||
| 85 | * @param string $sClassName The name of the class |
||
| 86 | * @param array $aDirectoryOptions The directory options |
||
| 87 | * @param array $aDefaultOptions The default options |
||
| 88 | * |
||
| 89 | * @return array |
||
| 90 | */ |
||
| 91 | private function getClassOptions($sClassName, array $aDirectoryOptions, array $aDefaultOptions = []) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * |
||
| 116 | * @param string $sDirectory The directory being registered |
||
| 117 | * @param array $aOptions The associated options |
||
| 118 | * |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | public function addDirectory($sDirectory, $aOptions) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * |
||
| 149 | * @param string $sNamespace The namespace of the directory being registered |
||
| 150 | * @param array $aOptions The associated options |
||
| 151 | * |
||
| 152 | * @return void |
||
| 153 | */ |
||
| 154 | public function addNamespace($sNamespace, array $aOptions) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Find a class name is register with Jaxon::CALLABLE_CLASS type |
||
| 166 | * |
||
| 167 | * @param string $sClassName The class name of the callable object |
||
| 168 | * |
||
| 169 | * @return array|null |
||
| 170 | */ |
||
| 171 | private function getOptionsFromClass($sClassName) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Find a class name is register with Jaxon::CALLABLE_DIR type |
||
| 182 | * |
||
| 183 | * @param string $sClassName The class name of the callable object |
||
| 184 | * @param string|null $sNamespace The namespace |
||
| 185 | * |
||
| 186 | * @return array|null |
||
| 187 | */ |
||
| 188 | private function getOptionsFromNamespace($sClassName, $sNamespace = null) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Find a callable object by class name |
||
| 219 | * |
||
| 220 | * @param string $sClassName The class name of the callable object |
||
| 221 | * @param array $aOptions The callable object options |
||
| 222 | * |
||
| 223 | * @return object |
||
| 224 | */ |
||
| 225 | protected function _getCallableObject($sClassName, array $aOptions) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Find a callable object by class name |
||
| 270 | * |
||
| 271 | * @param string $sClassName The class name of the callable object |
||
| 272 | * |
||
| 273 | * @return object |
||
| 274 | */ |
||
| 275 | public function getCallableObject($sClassName) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Create callable objects for all registered namespaces |
||
| 301 | * |
||
| 302 | * @return void |
||
| 303 | */ |
||
| 304 | private function createCallableObjects() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Find a user registered callable object by class name |
||
| 364 | * |
||
| 365 | * @param string $sClassName The class name of the callable object |
||
| 366 | * |
||
| 367 | * @return object |
||
| 368 | */ |
||
| 369 | protected function getRegisteredObject($sClassName) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Generate a hash for the registered callable objects |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | public function generateHash() |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Generate client side javascript code for the registered callable objects |
||
| 400 | * |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public function getScript() |
||
| 455 | } |
||
| 456 |