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