| Total Complexity | 80 |
| Total Lines | 527 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Client extends ClientAbstract implements HandleContracts |
||
| 11 | {
|
||
| 12 | /** |
||
| 13 | * @var array |
||
| 14 | */ |
||
| 15 | protected $capsule = []; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $except = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $method; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var ReflectionProcess |
||
| 29 | */ |
||
| 30 | protected $reflection; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var null|object |
||
| 34 | */ |
||
| 35 | protected $requestHttp; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var null|array |
||
| 39 | */ |
||
| 40 | protected $clientData; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $requestData = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $generatorList = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Request constructor. |
||
| 54 | * |
||
| 55 | * @param null|array $clientData |
||
| 56 | * |
||
| 57 | * @throws ReflectionExceptionAlias |
||
| 58 | */ |
||
| 59 | public function __construct($clientData=null) |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * auto validate |
||
| 76 | * |
||
| 77 | * @param $validate |
||
| 78 | */ |
||
| 79 | private function autoValidate($validate) |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * capsule inputs |
||
| 112 | * |
||
| 113 | * @return void|mixed |
||
| 114 | */ |
||
| 115 | private function capsule() |
||
| 116 | {
|
||
| 117 | // expected method is executed. |
||
| 118 | // this method is a must for http method values to be found in this property. |
||
| 119 | if($this->checkProperties('capsule')){
|
||
| 120 | |||
| 121 | if(property_exists($this,'auto_capsule') && is_array($this->auto_capsule)){
|
||
| 122 | $this->capsule = array_merge($this->capsule,$this->auto_capsule); |
||
| 123 | } |
||
| 124 | $caret = $this->capsuleCaret(); |
||
| 125 | |||
| 126 | foreach($this->inputs as $input=>$value){
|
||
| 127 | |||
| 128 | if(isset($caret[$input]) || ( |
||
| 129 | $this->checkProperties('capsule') && !in_array($input,$this->capsule)
|
||
| 130 | )){
|
||
| 131 | exception('clientCapsule',['key'=>$input])
|
||
| 132 | ->overflow('The '.$input.' value cannot be sent.');
|
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * get capsule caret for request |
||
| 140 | * |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | private function capsuleCaret() |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * check http method |
||
| 164 | * |
||
| 165 | * @return void|mixed |
||
| 166 | */ |
||
| 167 | private function checkHttpMethod() |
||
| 168 | {
|
||
| 169 | //get http method |
||
| 170 | $method = $this->requestHttp->getMethod(); |
||
|
|
|||
| 171 | |||
| 172 | // Determines which HTTP method |
||
| 173 | // the request object will be exposed to. |
||
| 174 | if($this->checkProperties('http')){
|
||
| 175 | |||
| 176 | // if the current http method does not exist |
||
| 177 | // in the http object, the exception will be thrown. |
||
| 178 | if(!in_array($method,$this->http)){
|
||
| 179 | |||
| 180 | //exception batMethodCall |
||
| 181 | exception()->badMethodCall( |
||
| 182 | 'Invalid http method process for '.class_basename($this).'.That is accepted http methods ['.implode(",",$this->http).'] ');
|
||
| 183 | } |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * check properties |
||
| 189 | * |
||
| 190 | * @param $properties |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | private function checkProperties($properties) |
||
| 194 | {
|
||
| 195 | // from the properties of the object properties to |
||
| 196 | // the existing variables, control the array and at least one element. |
||
| 197 | return (property_exists($this,$properties) |
||
| 198 | && is_array($this->{$properties}) && count($this->{$properties})) ? true : false;
|
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * register container for request |
||
| 203 | * |
||
| 204 | * @return mixed|void |
||
| 205 | */ |
||
| 206 | private function containerRegister() |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * get request except |
||
| 215 | * |
||
| 216 | * @param $except |
||
| 217 | * @return $this |
||
| 218 | */ |
||
| 219 | public function except($except) |
||
| 220 | {
|
||
| 221 | // the except parameter is a callable value. |
||
| 222 | if(is_callable($except)){
|
||
| 223 | $call = call_user_func_array($except,[$this]); |
||
| 224 | $except = $call; |
||
| 225 | } |
||
| 226 | |||
| 227 | // except with the except exceptions property |
||
| 228 | // and then assigning them to the inputs property. |
||
| 229 | $this->except = array_merge($this->except,$except); |
||
| 230 | $this->inputs = array_diff_key($this->inputs,array_flip($this->except)); |
||
| 231 | |||
| 232 | return $this; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * expected inputs |
||
| 237 | * |
||
| 238 | * @return void|mixed |
||
| 239 | */ |
||
| 240 | private function expectedInputs() |
||
| 265 | } |
||
| 266 | } |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * generator manager |
||
| 272 | * |
||
| 273 | * @throws ReflectionExceptionAlias |
||
| 274 | */ |
||
| 275 | private function generatorManager() |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * generator method |
||
| 296 | * |
||
| 297 | * @param $generators |
||
| 298 | * |
||
| 299 | * @throws ReflectionExceptionAlias |
||
| 300 | */ |
||
| 301 | private function generatorMethod($generators) |
||
| 343 | } |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * request handle |
||
| 349 | * |
||
| 350 | * @return mixed|void |
||
| 351 | * |
||
| 352 | * @throws ReflectionExceptionAlias |
||
| 353 | */ |
||
| 354 | public function handle() |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * get init client |
||
| 380 | * |
||
| 381 | * @return void |
||
| 382 | */ |
||
| 383 | private function initClient() |
||
| 384 | {
|
||
| 385 | // we use the http method to write |
||
| 386 | // the values to the inputs and origin properties. |
||
| 387 | foreach($this->clientData as $key=>$value){
|
||
| 388 | |||
| 389 | //inputs and origin properties |
||
| 390 | $this->inputs[$key] = $value; |
||
| 391 | $this->origin[$key] = $value; |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * the values specified in request except property |
||
| 397 | * are subtracted from all input values. |
||
| 398 | * |
||
| 399 | * @return mixed|void |
||
| 400 | */ |
||
| 401 | private function requestExcept() |
||
| 402 | {
|
||
| 403 | if(property_exists($this,'requestExcept') && is_array($this->requestExcept)){
|
||
| 404 | foreach ($this->requestExcept as $item){
|
||
| 405 | if(isset($this->inputs[$item])){
|
||
| 406 | unset($this->inputs[$item]); |
||
| 407 | } |
||
| 408 | } |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * request properties |
||
| 414 | * |
||
| 415 | * @throws ReflectionExceptionAlias |
||
| 416 | */ |
||
| 417 | private function requestProperties() |
||
| 418 | {
|
||
| 419 | // contrary to capsule method, |
||
| 420 | // expected values must be in the key being sent. |
||
| 421 | $this->expectedInputs(); |
||
| 422 | |||
| 423 | // get capsule as mandatory values |
||
| 424 | $this->capsule(); |
||
| 425 | |||
| 426 | // this method determines |
||
| 427 | // how the request object will be requested, |
||
| 428 | $this->checkHttpMethod(); |
||
| 429 | |||
| 430 | // it passes all keys that are sent through |
||
| 431 | // a validation method on the user side. |
||
| 432 | $this->validation(); |
||
| 433 | |||
| 434 | // the values specified in request except property |
||
| 435 | // are subtracted from all input values. |
||
| 436 | $this->requestExcept(); |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * set client objects |
||
| 441 | * |
||
| 442 | * @throws ReflectionExceptionAlias |
||
| 443 | */ |
||
| 444 | private function setClientObjects() |
||
| 445 | {
|
||
| 446 | $clientObjects = $this->getClientObjects(); |
||
| 447 | |||
| 448 | // we update the input values after |
||
| 449 | // we receive and check the saved objects. |
||
| 450 | foreach ($clientObjects as $key=>$value){
|
||
| 451 | |||
| 452 | if(!in_array($key,$this->generatorList) && isset($clientObjects['origin'][$key])){
|
||
| 453 | |||
| 454 | $this->{$key} = $clientObjects['origin'][$key];
|
||
| 455 | $this->inputs[$key] = $this->{$key};
|
||
| 456 | |||
| 457 | // the request update to be performed using |
||
| 458 | // the method name to be used with the http method. |
||
| 459 | $this->registerRequestInputs($key); |
||
| 460 | } |
||
| 461 | } |
||
| 462 | |||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * register request inputs |
||
| 467 | * |
||
| 468 | * @param $key |
||
| 469 | * |
||
| 470 | * @throws ReflectionExceptionAlias |
||
| 471 | */ |
||
| 472 | private function registerRequestInputs($key) |
||
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * set request inputs |
||
| 489 | * |
||
| 490 | * @param $method |
||
| 491 | * @param $key |
||
| 492 | * |
||
| 493 | * @throws ReflectionExceptionAlias |
||
| 494 | */ |
||
| 495 | private function setRequestInputs($method,$key) |
||
| 519 | } |
||
| 520 | |||
| 521 | } |
||
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * validation for request |
||
| 527 | * |
||
| 528 | * @return void |
||
| 529 | */ |
||
| 530 | private function validation() |
||
| 539 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.