Total Complexity | 41 |
Total Lines | 213 |
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 |
||
7 | class ClientAnnotationManager extends ClientAnnotationAbstract |
||
8 | { |
||
9 | /** |
||
10 | * @var array $exceptionParams |
||
11 | */ |
||
12 | protected $exceptionParams = []; |
||
13 | |||
14 | /** |
||
15 | * @var string $annotation |
||
16 | */ |
||
17 | protected $annotation; |
||
18 | |||
19 | /** |
||
20 | * RequestAnnotationManager constructor. |
||
21 | * @param ApplicationContracts $app |
||
22 | * @param $request |
||
23 | */ |
||
24 | public function __construct(ApplicationContracts $app,$request) |
||
25 | { |
||
26 | parent::__construct($app); |
||
27 | |||
28 | $this->setReflection($request); |
||
29 | |||
30 | $this->getInputs(); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * check annotation |
||
35 | * |
||
36 | * @param $method |
||
37 | * @param $key |
||
38 | * @return mixed|void |
||
39 | */ |
||
40 | public function annotation($method,$key) |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * catch exception from regex method |
||
61 | * |
||
62 | * @param string $key |
||
63 | * @param null|string $data |
||
64 | */ |
||
65 | private function catchException($key,$data) |
||
66 | { |
||
67 | if(isset($this->exceptionParams[$key])){ |
||
68 | |||
69 | //get key params for exception params |
||
70 | $keyParams = ($this->exceptionParams[$key]['params']) ?? []; |
||
71 | |||
72 | //catch exception |
||
73 | exception($this->exceptionParams[$key]['name'],$keyParams) |
||
74 | ->unexpectedValue($this->exceptionParams[$key]['name'].' input value is not valid as format ('.$data.')'); |
||
75 | } |
||
76 | else{ |
||
77 | //catch exception |
||
78 | exception()->unexpectedValue($key.' input value is not valid as format ('.$data.')'); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * get request exception from annotation |
||
84 | * |
||
85 | * @param $key |
||
86 | */ |
||
87 | private function getException($key) |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * get regular expression from request object |
||
115 | * |
||
116 | * @param $key |
||
117 | */ |
||
118 | private function getRegex($key) |
||
119 | { |
||
120 | // with the method based regex annotation, |
||
121 | // we check the rule definition for our requests. |
||
122 | if(preg_match('@regex\((.*?)\)\r\n@is',$this->annotation,$regex)){ |
||
123 | if(isset($this->inputs[$key])){ |
||
124 | |||
125 | // for the definition of rules, |
||
126 | // our inputs can be array and in this case we offer array option for user comfort. |
||
127 | if(is_array($this->inputs[$key])){ |
||
128 | |||
129 | foreach ($this->inputs[$key] as $this->inputsKey => $this->inputsValue){ |
||
130 | |||
131 | if(is_array($this->inputsValue)){ |
||
132 | |||
133 | foreach ($this->inputsValue as $inputsValueKey => $inputsValueItem){ |
||
134 | if(!preg_match('@'.$regex[1].'@is',$inputsValueItem)){ |
||
135 | $this->catchException($key,$regex[1]); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | } |
||
140 | else{ |
||
141 | if(!preg_match('@'.$regex[1].'@is',$this->inputsValue)){ |
||
142 | $this->catchException($key,$regex[1]); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | } |
||
147 | } |
||
148 | else{ |
||
149 | |||
150 | // we control the regex rule that evaluates when only string arrives. |
||
151 | if(!preg_match('@'.$regex[1].'@is',$this->inputs[$key])){ |
||
152 | $this->catchException($key,$regex[1]); |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * get remove regex pattern with request object |
||
161 | * |
||
162 | * @param string $key |
||
163 | * @return void|mixed |
||
164 | */ |
||
165 | private function getRemove($key) |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * check rules from request |
||
178 | * |
||
179 | * @param $key |
||
180 | */ |
||
181 | private function getRules($key) |
||
231 | } |