| Total Complexity | 46 |
| Total Lines | 243 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ClientAnnotationManager 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 ClientAnnotationManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class ClientAnnotationManager extends ClientAnnotationAbstract |
||
| 12 | {
|
||
| 13 | /** |
||
| 14 | * @var array $exceptionParams |
||
| 15 | */ |
||
| 16 | protected $exceptionParams = []; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string $annotation |
||
| 20 | */ |
||
| 21 | protected $annotation; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * RequestAnnotationManager constructor. |
||
| 25 | * @param ApplicationContracts $app |
||
| 26 | * @param $request |
||
| 27 | */ |
||
| 28 | public function __construct(ApplicationContracts $app,$request) |
||
| 29 | {
|
||
| 30 | parent::__construct($app); |
||
| 31 | |||
| 32 | $this->setReflection($request); |
||
| 33 | |||
| 34 | $this->getInputs(); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * check annotation |
||
| 39 | * |
||
| 40 | * @param $method |
||
| 41 | * @param $key |
||
| 42 | * @return mixed|void |
||
| 43 | */ |
||
| 44 | public function annotation($method,$key) |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * catch exception from regex method |
||
| 65 | * |
||
| 66 | * @param string $key |
||
| 67 | * @param null|string $data |
||
| 68 | */ |
||
| 69 | private function catchException($key,$data) |
||
| 70 | {
|
||
| 71 | if(isset($this->exceptionParams[$key])){
|
||
| 72 | |||
| 73 | //get key params for exception params |
||
| 74 | $keyParams = ($this->exceptionParams[$key]['params']) ?? []; |
||
| 75 | |||
| 76 | //catch exception |
||
| 77 | exception($this->exceptionParams[$key]['name'],$keyParams) |
||
| 78 | ->unexpectedValue($this->exceptionParams[$key]['name'].' input value is not valid as format ('.$data.')');
|
||
| 79 | } |
||
| 80 | else{
|
||
| 81 | //catch exception |
||
| 82 | exception()->unexpectedValue($key.' input value is not valid as format ('.$data.')');
|
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * get request exception from annotation |
||
| 88 | * |
||
| 89 | * @param $key |
||
| 90 | */ |
||
| 91 | private function getException($key) |
||
| 92 | {
|
||
| 93 | if(preg_match('@exception\((.*?)\)|exception\((.*?)\)\r\n@is',$this->annotation,$exception)){
|
||
| 94 | |||
| 95 | $exceptionSpaceExplode = explode(" ",$exception[1]);
|
||
| 96 | |||
| 97 | foreach ($exceptionSpaceExplode as $exceptions){
|
||
| 98 | $exceptionsDotExplode = explode(":",$exceptions);
|
||
| 99 | $this->exceptionParams[$key][$exceptionsDotExplode[0]] = $exceptionsDotExplode[1]; |
||
| 100 | } |
||
| 101 | |||
| 102 | if(isset($this->exceptionParams[$key]['params'])){
|
||
| 103 | |||
| 104 | $paramsCommaExplode = explode(",",$this->exceptionParams[$key]['params']);
|
||
| 105 | unset($this->exceptionParams[$key]['params']); |
||
| 106 | |||
| 107 | foreach ($paramsCommaExplode as $params){
|
||
| 108 | $paramsEqualExplode = explode("=",$params);
|
||
| 109 | if(isset($paramsEqualExplode[0]) && isset($paramsEqualExplode[1])){
|
||
| 110 | $this->exceptionParams[$key]['params'][$paramsEqualExplode[0]] = $paramsEqualExplode[1]; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * get regular expression from request object |
||
| 119 | * |
||
| 120 | * @param $key |
||
| 121 | */ |
||
| 122 | private function getRegex($key) |
||
| 123 | {
|
||
| 124 | // with the method based regex annotation, |
||
| 125 | // we check the rule definition for our requests. |
||
| 126 | if(preg_match('@regex\((.*?)\)|regex\((.*?)\)\r\n@is',$this->annotation,$regex)){
|
||
| 127 | if(isset($this->inputs[$key])){
|
||
| 128 | |||
| 129 | // for the definition of rules, |
||
| 130 | // our inputs can be array and in this case we offer array option for user comfort. |
||
| 131 | if(is_array($this->inputs[$key])){
|
||
| 132 | |||
| 133 | foreach ($this->inputs[$key] as $this->inputsKey => $this->inputsValue){
|
||
| 134 | |||
| 135 | if(is_array($this->inputsValue)){
|
||
| 136 | |||
| 137 | foreach ($this->inputsValue as $inputsValueKey => $inputsValueItem){
|
||
| 138 | if(!preg_match('@'.$regex[1].'@is',$inputsValueItem)){
|
||
| 139 | $this->catchException($key,$regex[1]); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | } |
||
| 144 | else{
|
||
| 145 | if(!preg_match('@'.$regex[1].'@is',$this->inputsValue)){
|
||
| 146 | $this->catchException($key,$regex[1]); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | } |
||
| 151 | } |
||
| 152 | else{
|
||
| 153 | |||
| 154 | // we control the regex rule that evaluates when only string arrives. |
||
| 155 | if(!preg_match('@'.$regex[1].'@is',$this->inputs[$key])){
|
||
| 156 | $this->catchException($key,$regex[1]); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * get remove regex pattern with request object |
||
| 165 | * |
||
| 166 | * @param string $key |
||
| 167 | * @return void|mixed |
||
| 168 | */ |
||
| 169 | private function getRemove($key) |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * check rules from request |
||
| 182 | * |
||
| 183 | * @param $key |
||
| 184 | */ |
||
| 185 | private function getRules($key) |
||
| 265 | } |