Complex classes like CallableClass 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 CallableClass, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class CallableClass extends RequestPlugin |
||
| 33 | { |
||
| 34 | use \Jaxon\Features\Config; |
||
| 35 | use \Jaxon\Features\Template; |
||
| 36 | use \Jaxon\Features\Validator; |
||
| 37 | use \Jaxon\Features\Translator; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The callable registrar |
||
| 41 | * |
||
| 42 | * @var CallableRegistry |
||
| 43 | */ |
||
| 44 | protected $xRegistry; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The callable repository |
||
| 48 | * |
||
| 49 | * @var CallableRepository |
||
| 50 | */ |
||
| 51 | protected $xRepository; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The value of the class parameter of the incoming Jaxon request |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $sRequestedClass = ''; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The value of the method parameter of the incoming Jaxon request |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $sRequestedMethod = ''; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The class constructor |
||
| 69 | * |
||
| 70 | * @param CallableRegistry $xRegistry The callable class registry |
||
| 71 | * @param CallableRepository $xRepository The callable object repository |
||
| 72 | */ |
||
| 73 | public function __construct(CallableRegistry $xRegistry, CallableRepository $xRepository) |
||
| 74 | { |
||
| 75 | $this->xRegistry = $xRegistry; |
||
| 76 | $this->xRepository = $xRepository; |
||
| 77 | |||
| 78 | if(!empty($_GET['jxncls'])) |
||
| 79 | { |
||
| 80 | $this->sRequestedClass = trim($_GET['jxncls']); |
||
| 81 | } |
||
| 82 | if(!empty($_GET['jxnmthd'])) |
||
| 83 | { |
||
| 84 | $this->sRequestedMethod = trim($_GET['jxnmthd']); |
||
| 85 | } |
||
| 86 | if(!empty($_POST['jxncls'])) |
||
| 87 | { |
||
| 88 | $this->sRequestedClass = trim($_POST['jxncls']); |
||
| 89 | } |
||
| 90 | if(!empty($_POST['jxnmthd'])) |
||
| 91 | { |
||
| 92 | $this->sRequestedMethod = trim($_POST['jxnmthd']); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @inheritDoc |
||
| 98 | */ |
||
| 99 | public function getName() |
||
| 100 | { |
||
| 101 | return Jaxon::CALLABLE_CLASS; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @inheritDoc |
||
| 106 | */ |
||
| 107 | public function getTarget() |
||
| 108 | { |
||
| 109 | if(!$this->sRequestedClass || !$this->sRequestedMethod) |
||
| 110 | { |
||
| 111 | return null; |
||
| 112 | } |
||
| 113 | return Target::makeClass($this->sRequestedClass, $this->sRequestedMethod); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Register a callable class |
||
| 118 | * |
||
| 119 | * @param string $sType The type of request handler being registered |
||
| 120 | * @param string $sClassName The name of the class being registered |
||
| 121 | * @param array|string $aOptions The associated options |
||
| 122 | * |
||
| 123 | * @return boolean |
||
| 124 | */ |
||
| 125 | public function register($sType, $sClassName, $aOptions) |
||
| 126 | { |
||
| 127 | $sType = trim($sType); |
||
| 128 | if($sType != $this->getName()) |
||
| 129 | { |
||
| 130 | return false; |
||
| 131 | } |
||
| 132 | |||
| 133 | if(!is_string($sClassName)) |
||
| 134 | { |
||
| 135 | throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration')); |
||
| 136 | } |
||
| 137 | if(is_string($aOptions)) |
||
| 138 | { |
||
| 139 | $aOptions = ['include' => $aOptions]; |
||
| 140 | } |
||
| 141 | if(!is_array($aOptions)) |
||
| 142 | { |
||
| 143 | throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration')); |
||
| 144 | } |
||
| 145 | |||
| 146 | $this->xRepository->addClass(trim($sClassName), $aOptions); |
||
| 147 | |||
| 148 | return true; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @inheritDoc |
||
| 153 | */ |
||
| 154 | public function getHash() |
||
| 155 | { |
||
| 156 | $this->xRegistry->createCallableObjects(); |
||
| 157 | $aNamespaces = $this->xRepository->getNamespaces(); |
||
| 158 | $aCallableObjects = $this->xRepository->getCallableObjects(); |
||
| 159 | $sHash = ''; |
||
| 160 | |||
| 161 | foreach($aNamespaces as $sNamespace => $aOptions) |
||
| 162 | { |
||
| 163 | $sHash .= $sNamespace . $aOptions['separator']; |
||
| 164 | } |
||
| 165 | foreach($aCallableObjects as $sClassName => $xCallableObject) |
||
| 166 | { |
||
| 167 | $sHash .= $sClassName . implode('|', $xCallableObject->getMethods()); |
||
| 168 | } |
||
| 169 | |||
| 170 | return md5($sHash); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Generate client side javascript code for namespaces |
||
| 175 | * |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | private function getNamespacesScript() |
||
| 179 | { |
||
| 180 | $sCode = ''; |
||
| 181 | $sPrefix = $this->getOption('core.prefix.class'); |
||
| 182 | $aJsClasses = []; |
||
|
1 ignored issue
–
show
|
|||
| 183 | $aNamespaces = array_keys($this->xRepository->getNamespaces()); |
||
| 184 | foreach($aNamespaces as $sNamespace) |
||
| 185 | { |
||
| 186 | $offset = 0; |
||
| 187 | $sJsNamespace = str_replace('\\', '.', $sNamespace); |
||
|
1 ignored issue
–
show
|
|||
| 188 | $sJsNamespace .= '.Null'; // This is a sentinel. The last token is not processed in the while loop. |
||
| 189 | while(($dotPosition = strpos($sJsNamespace, '.', $offset)) !== false) |
||
| 190 | { |
||
| 191 | $sJsClass = substr($sJsNamespace, 0, $dotPosition); |
||
| 192 | // Generate code for this object |
||
| 193 | if(!key_exists($sJsClass, $aJsClasses)) |
||
| 194 | { |
||
| 195 | $sCode .= "$sPrefix$sJsClass = {};\n"; |
||
| 196 | $aJsClasses[$sJsClass] = $sJsClass; |
||
| 197 | } |
||
| 198 | $offset = $dotPosition + 1; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | return $sCode; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Generate client side javascript code for a callable class |
||
| 206 | * |
||
| 207 | * @param string $sClassName The class name |
||
| 208 | * @param CallableObject $xCallableObject The corresponding callable object |
||
| 209 | * @param array $aProtectedMethods The protected methods |
||
| 210 | * |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | private function getCallableScript($sClassName, CallableObject $xCallableObject, array $aProtectedMethods) |
||
| 214 | { |
||
| 215 | $aCallableOptions = $this->xRepository->getCallableOptions(); |
||
| 216 | $aConfig = $aCallableOptions[$sClassName]; |
||
| 217 | $aCommonConfig = key_exists('*', $aConfig) ? $aConfig['*'] : []; |
||
| 218 | |||
| 219 | $_aProtectedMethods = is_subclass_of($sClassName, UserCallableClass::class) ? $aProtectedMethods : []; |
||
| 220 | $aMethods = []; |
||
| 221 | foreach($xCallableObject->getMethods() as $sMethodName) |
||
| 222 | { |
||
| 223 | // Don't export methods of the CallableClass class |
||
| 224 | if(in_array($sMethodName, $_aProtectedMethods)) |
||
| 225 | { |
||
| 226 | continue; |
||
| 227 | } |
||
| 228 | // Specific options for this method |
||
| 229 | $aMethodConfig = key_exists($sMethodName, $aConfig) ? |
||
| 230 | array_merge($aCommonConfig, $aConfig[$sMethodName]) : $aCommonConfig; |
||
| 231 | $aMethods[] = [ |
||
| 232 | 'name' => $sMethodName, |
||
| 233 | 'config' => $aMethodConfig, |
||
| 234 | ]; |
||
| 235 | } |
||
| 236 | |||
| 237 | $sPrefix = $this->getOption('core.prefix.class'); |
||
| 238 | return $this->render('jaxon::support/object.js', [ |
||
| 239 | 'sPrefix' => $sPrefix, |
||
| 240 | 'sClass' => $xCallableObject->getJsName(), |
||
| 241 | 'aMethods' => $aMethods, |
||
| 242 | ]); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Generate client side javascript code for the registered callable objects |
||
| 247 | * |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | public function getScript() |
||
| 251 | { |
||
| 252 | $this->xRegistry->createCallableObjects(); |
||
| 253 | |||
| 254 | // The methods of the \Jaxon\CallableClass class must not be exported |
||
| 255 | $xCallableClass = new \ReflectionClass(UserCallableClass::class); |
||
| 256 | $aProtectedMethods = []; |
||
| 257 | foreach($xCallableClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $xMethod) |
||
| 258 | { |
||
| 259 | $aProtectedMethods[] = $xMethod->getName(); |
||
| 260 | } |
||
| 261 | |||
| 262 | $sCode = $this->getNamespacesScript(); |
||
| 263 | |||
| 264 | $aCallableObjects = $this->xRepository->getCallableObjects(); |
||
| 265 | foreach($aCallableObjects as $sClassName => $xCallableObject) |
||
| 266 | { |
||
| 267 | $sCode .= $this->getCallableScript($sClassName, $xCallableObject, $aProtectedMethods); |
||
| 268 | } |
||
| 269 | |||
| 270 | return $sCode; |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @inheritDoc |
||
| 275 | */ |
||
| 276 | public function canProcessRequest() |
||
| 277 | { |
||
| 278 | // Check the validity of the class name |
||
| 279 | if(($this->sRequestedClass !== null && !$this->validateClass($this->sRequestedClass)) || |
||
| 280 | ($this->sRequestedMethod !== null && !$this->validateMethod($this->sRequestedMethod))) |
||
| 281 | { |
||
| 282 | $this->sRequestedClass = null; |
||
|
1 ignored issue
–
show
|
|||
| 283 | $this->sRequestedMethod = null; |
||
| 284 | } |
||
| 285 | return ($this->sRequestedClass !== null && $this->sRequestedMethod !== null); |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @inheritDoc |
||
| 290 | */ |
||
| 291 | public function processRequest() |
||
| 317 | } |
||
| 318 |