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