| Total Complexity | 47 |
| Total Lines | 344 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like CallableObjectOptions 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.
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 CallableObjectOptions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class CallableObjectOptions |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Check if the js code for this object must be generated |
||
| 28 | * |
||
| 29 | * @var bool |
||
| 30 | */ |
||
| 31 | private $bExcluded = false; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The character to use as separator in javascript class names |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $sSeparator = '.'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * A list of methods of the user registered callable object the library must not export to javascript |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private $aProtectedMethods = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * A list of methods to call before processing the request |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private $aBeforeMethods = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * A list of methods to call after processing the request |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private $aAfterMethods = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The javascript class options |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | private $aJsOptions = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The DI options |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private $aDiOptions = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The constructor |
||
| 77 | * |
||
| 78 | * @param array $aOptions |
||
| 79 | * @param array $aAnnotations |
||
| 80 | */ |
||
| 81 | public function __construct(array $aOptions, array $aAnnotations) |
||
| 82 | { |
||
| 83 | [$bExcluded, $aAnnotationOptions, $aAnnotationProtected] = $aAnnotations; |
||
| 84 | $this->bExcluded = $bExcluded || (bool)($aOptions['excluded'] ?? false); |
||
| 85 | if($this->bExcluded) |
||
| 86 | { |
||
| 87 | return; |
||
| 88 | } |
||
| 89 | |||
| 90 | $this->setSeparator($aOptions['separator']); |
||
| 91 | $this->addProtectedMethods($aOptions['protected']); |
||
| 92 | $this->addProtectedMethods($aAnnotationProtected); |
||
| 93 | |||
| 94 | foreach($aOptions['functions'] as $sNames => $aFunctionOptions) |
||
| 95 | { |
||
| 96 | $aFunctionNames = explode(',', $sNames); // Names are in comma-separated list. |
||
| 97 | foreach($aFunctionNames as $sFunctionName) |
||
| 98 | { |
||
| 99 | $this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | foreach($aAnnotationOptions as $sFunctionName => $aFunctionOptions) |
||
| 103 | { |
||
| 104 | $this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $sSeparator |
||
| 110 | * |
||
| 111 | * @return void |
||
| 112 | */ |
||
| 113 | private function setSeparator(string $sSeparator) |
||
| 114 | { |
||
| 115 | if($sSeparator === '_' || $sSeparator === '.') |
||
| 116 | { |
||
| 117 | $this->sSeparator = $sSeparator; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param mixed $xMethods |
||
| 123 | * |
||
| 124 | * @return void |
||
| 125 | */ |
||
| 126 | private function addProtectedMethods($xMethods) |
||
| 127 | { |
||
| 128 | if(!is_array($xMethods)) |
||
| 129 | { |
||
| 130 | $this->aProtectedMethods[trim((string)$xMethods)] = true; |
||
| 131 | return; |
||
| 132 | } |
||
| 133 | foreach($xMethods as $sMethod) |
||
| 134 | { |
||
| 135 | $this->aProtectedMethods[trim((string)$sMethod)] = true; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Check if the js code for this object must be generated |
||
| 141 | * |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | public function excluded(): bool |
||
| 145 | { |
||
| 146 | return $this->bExcluded; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | public function separator(): string |
||
| 153 | { |
||
| 154 | return $this->sSeparator; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $sMethodName |
||
| 159 | * |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | public function isProtectedMethod(string $sMethodName): bool |
||
| 163 | { |
||
| 164 | return isset($this->aProtectedMethods[$sMethodName]) || isset($this->aProtectedMethods['*']); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | public function beforeMethods(): array |
||
| 171 | { |
||
| 172 | return $this->aBeforeMethods; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public function afterMethods(): array |
||
| 179 | { |
||
| 180 | return $this->aAfterMethods; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | public function diOptions(): array |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | public function jsOptions(): array |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Set hook methods |
||
| 201 | * |
||
| 202 | * @param array $aHookMethods The array of hook methods |
||
| 203 | * @param string|array $xValue The value of the configuration option |
||
| 204 | * |
||
| 205 | * @return void |
||
| 206 | */ |
||
| 207 | private function setHookMethods(array &$aHookMethods, $xValue) |
||
| 208 | { |
||
| 209 | foreach($xValue as $sCalledMethod => $xMethodToCall) |
||
| 210 | { |
||
| 211 | if(is_array($xMethodToCall)) |
||
| 212 | { |
||
| 213 | $aHookMethods[$sCalledMethod] = $xMethodToCall; |
||
| 214 | } |
||
| 215 | elseif(is_string($xMethodToCall)) |
||
| 216 | { |
||
| 217 | $aHookMethods[$sCalledMethod] = [$xMethodToCall]; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param array $aDiOptions |
||
| 224 | */ |
||
| 225 | private function addDiOption(array $aDiOptions) |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Set configuration options / call options for each method |
||
| 232 | * |
||
| 233 | * @param string $sName The name of the configuration option |
||
| 234 | * @param string|array $xValue The value of the configuration option |
||
| 235 | * |
||
| 236 | * @return void |
||
| 237 | */ |
||
| 238 | public function addOption(string $sName, $xValue) |
||
| 239 | { |
||
| 240 | switch($sName) |
||
| 241 | { |
||
| 242 | // Set the separator |
||
| 243 | case 'separator': |
||
| 244 | $this->setSeparator((string)$xValue); |
||
| 245 | break; |
||
| 246 | case 'excluded': |
||
| 247 | $this->bExcluded = (bool)$xValue; |
||
| 248 | break; |
||
| 249 | // Set the protected methods |
||
| 250 | case 'protected': |
||
| 251 | $this->addProtectedMethods($xValue); |
||
| 252 | break; |
||
| 253 | // Set the methods to call before processing the request |
||
| 254 | case '__before': |
||
| 255 | $this->setHookMethods($this->aBeforeMethods, $xValue); |
||
| 256 | break; |
||
| 257 | // Set the methods to call after processing the request |
||
| 258 | case '__after': |
||
| 259 | $this->setHookMethods($this->aAfterMethods, $xValue); |
||
| 260 | break; |
||
| 261 | // Set the attributes to inject in the callable object |
||
| 262 | case '__di': |
||
| 263 | $this->addDiOption($xValue); |
||
| 264 | break; |
||
| 265 | default: |
||
| 266 | break; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $sFunctionName |
||
| 272 | * @param string $sOptionName |
||
| 273 | * @param mixed $xOptionValue |
||
| 274 | * |
||
| 275 | * @return void |
||
| 276 | */ |
||
| 277 | private function _addJsArrayOption(string $sFunctionName, string $sOptionName, $xOptionValue) |
||
| 278 | { |
||
| 279 | if(is_string($xOptionValue)) |
||
| 280 | { |
||
| 281 | $xOptionValue = [$xOptionValue]; |
||
| 282 | } |
||
| 283 | if(!is_array($xOptionValue)) |
||
| 284 | { |
||
| 285 | return; // Do not save. |
||
| 286 | } |
||
| 287 | $aOptions = $this->aJsOptions[$sFunctionName][$sOptionName] ?? []; |
||
| 288 | $this->aJsOptions[$sFunctionName][$sOptionName] = array_merge($aOptions, $xOptionValue); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $sFunctionName |
||
| 293 | * @param string $sOptionName |
||
| 294 | * @param mixed $xOptionValue |
||
| 295 | * |
||
| 296 | * @return void |
||
| 297 | */ |
||
| 298 | private function _setJsOption(string $sFunctionName, string $sOptionName, $xOptionValue) |
||
| 299 | { |
||
| 300 | $this->aJsOptions[$sFunctionName][$sOptionName] = $xOptionValue; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param string $sFunctionName |
||
| 305 | * @param string $sOptionName |
||
| 306 | * @param mixed $xOptionValue |
||
| 307 | * |
||
| 308 | * @return void |
||
| 309 | */ |
||
| 310 | private function addJsOption(string $sFunctionName, string $sOptionName, $xOptionValue) |
||
| 311 | { |
||
| 312 | switch($sOptionName) |
||
| 313 | { |
||
| 314 | case 'excluded': |
||
| 315 | if((bool)$xOptionValue) |
||
| 316 | { |
||
| 317 | $this->addProtectedMethods($sFunctionName); |
||
| 318 | } |
||
| 319 | break; |
||
| 320 | // For databags, all the value are merged in a single array. |
||
| 321 | case 'bags': |
||
| 322 | $this->_addJsArrayOption($sFunctionName, $sOptionName, $xOptionValue); |
||
| 323 | return; |
||
| 324 | // For all the other options, including callback, only the last value is kept. |
||
| 325 | case 'callback': |
||
| 326 | default: |
||
| 327 | $this->_setJsOption($sFunctionName, $sOptionName, $xOptionValue); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param string $sFunctionName |
||
| 333 | * @param array $aFunctionOptions |
||
| 334 | * |
||
| 335 | * @return void |
||
| 336 | */ |
||
| 337 | private function addFunctionOptions(string $sFunctionName, array $aFunctionOptions) |
||
| 338 | { |
||
| 339 | foreach($aFunctionOptions as $sOptionName => $xOptionValue) |
||
| 340 | { |
||
| 341 | substr($sOptionName, 0, 2) === '__' ? |
||
| 342 | // Options for PHP classes. They start with "__". |
||
| 343 | $this->addOption($sOptionName, [$sFunctionName => $xOptionValue]) : |
||
| 344 | // Options for javascript code. |
||
| 345 | $this->addJsOption($sFunctionName, $sOptionName, $xOptionValue); |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param string $sMethodName |
||
| 351 | * |
||
| 352 | * @return array |
||
| 353 | */ |
||
| 354 | public function getMethodOptions(string $sMethodName): array |
||
| 368 | } |
||
| 369 | } |
||
| 370 |