Total Complexity | 42 |
Total Lines | 341 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Action 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 Action, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Action |
||
23 | { |
||
24 | /** |
||
25 | * Action::$methods |
||
26 | * |
||
27 | * Action Methods |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private $methods; |
||
32 | |||
33 | /** |
||
34 | * Action::$domain |
||
35 | * |
||
36 | * Routing map domain. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $domain; |
||
41 | |||
42 | /** |
||
43 | * Action::$path |
||
44 | * |
||
45 | * Action Path |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | private $path; |
||
50 | |||
51 | /** |
||
52 | * Action::$closure |
||
53 | * |
||
54 | * Action Closure |
||
55 | * |
||
56 | * @var \Closure |
||
57 | */ |
||
58 | private $closure; |
||
59 | |||
60 | /** |
||
61 | * Action::$closureParameters |
||
62 | * |
||
63 | * Action Closure Parameters |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | private $closureParameters = []; |
||
68 | |||
69 | // ------------------------------------------------------------------------ |
||
70 | |||
71 | /** |
||
72 | * Action::__construct |
||
73 | * |
||
74 | * @param string $method The route method. |
||
75 | * @param string $path The route path. |
||
76 | * @param \Closure $closure The route closure. |
||
77 | * @param string $domain The route domain. |
||
78 | */ |
||
79 | public function __construct($method, $path, \Closure $closure, $domain = null) |
||
80 | { |
||
81 | $this->methods = explode('|', $method); |
||
82 | $this->methods = array_map('strtoupper', $this->methods); |
||
83 | |||
84 | $this->path = $path; |
||
85 | $this->closure = $closure; |
||
86 | $this->domain = is_null($domain) |
||
87 | ? isset($_SERVER[ 'HTTP_HOST' ]) |
||
88 | ? @$_SERVER[ 'HTTP_HOST' ] |
||
89 | : @$_SERVER[ 'SERVER_NAME' ] |
||
90 | : $domain; |
||
91 | |||
92 | // Remove www |
||
93 | if (strpos($this->domain, 'www.') !== false) { |
||
94 | $this->domain = str_replace('www.', '', $this->domain); |
||
95 | } |
||
96 | |||
97 | if (preg_match_all("/{(.*)}/", $this->domain, $matches)) { |
||
98 | foreach ($matches[ 1 ] as $match) { |
||
99 | $this->closureParameters[] = $match; |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | |||
104 | // ------------------------------------------------------------------------ |
||
105 | |||
106 | /** |
||
107 | * Action::getMethods |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | public function getMethods() |
||
112 | { |
||
113 | return $this->methods; |
||
114 | } |
||
115 | |||
116 | // ------------------------------------------------------------------------ |
||
117 | |||
118 | /** |
||
119 | * Action::getDomain |
||
120 | * |
||
121 | * @return mixed|string |
||
122 | */ |
||
123 | public function getDomain() |
||
124 | { |
||
125 | return $this->domain; |
||
126 | } |
||
127 | |||
128 | // ------------------------------------------------------------------------ |
||
129 | |||
130 | /** |
||
131 | * Action::getPath |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | public function getPath() |
||
136 | { |
||
137 | return $this->path; |
||
138 | } |
||
139 | |||
140 | // ------------------------------------------------------------------------ |
||
141 | |||
142 | /** |
||
143 | * Action::getClosure |
||
144 | * |
||
145 | * @return mixed |
||
146 | */ |
||
147 | public function getClosure() |
||
148 | { |
||
149 | return call_user_func_array($this->closure, $this->closureParameters); |
||
150 | } |
||
151 | |||
152 | // ------------------------------------------------------------------------ |
||
153 | |||
154 | /** |
||
155 | * Action::addClosureParameters |
||
156 | * |
||
157 | * @param mixed $value |
||
158 | * |
||
159 | * @return static |
||
160 | */ |
||
161 | public function addClosureParameters($value) |
||
162 | { |
||
163 | $this->closureParameters[] = $value; |
||
164 | |||
165 | return $this; |
||
166 | } |
||
167 | |||
168 | // ------------------------------------------------------------------------ |
||
169 | |||
170 | /** |
||
171 | * Action::getClosureParameters |
||
172 | * |
||
173 | * @return array |
||
174 | */ |
||
175 | public function getClosureParameters() |
||
176 | { |
||
177 | return $this->closureParameters; |
||
178 | } |
||
179 | |||
180 | // ------------------------------------------------------------------------ |
||
181 | |||
182 | /** |
||
183 | * Action::setClosureParameters |
||
184 | * |
||
185 | * @param array $parameters |
||
186 | * |
||
187 | * @return static |
||
188 | */ |
||
189 | public function setClosureParameters(array $parameters) |
||
190 | { |
||
191 | $this->closureParameters = $parameters; |
||
192 | |||
193 | return $this; |
||
194 | } |
||
195 | |||
196 | // ------------------------------------------------------------------------ |
||
197 | |||
198 | /** |
||
199 | * Action::isValidDomain |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | public function isValidDomain() |
||
204 | { |
||
205 | $domain = isset($_SERVER[ 'HTTP_HOST' ]) |
||
206 | ? $_SERVER[ 'HTTP_HOST' ] |
||
207 | : $_SERVER[ 'SERVER_NAME' ]; |
||
208 | |||
209 | if ($this->domain === $domain) { |
||
210 | return true; |
||
211 | } |
||
212 | |||
213 | return false; |
||
214 | } |
||
215 | |||
216 | // ------------------------------------------------------------------------ |
||
217 | |||
218 | /** |
||
219 | * Action::isValidUriString |
||
220 | * |
||
221 | * @param string $uriString |
||
222 | * |
||
223 | * @return bool |
||
224 | * @throws \ReflectionException |
||
225 | */ |
||
226 | public function isValidUriString($uriString) |
||
227 | { |
||
228 | $uriString = '/' . ltrim($uriString, '/'); |
||
229 | |||
230 | if (strtolower($uriString) === $this->path) { |
||
231 | $this->closureParameters = array_merge( |
||
232 | $this->closureParameters, |
||
233 | array_filter(explode('/', $uriString)) |
||
234 | ); |
||
235 | |||
236 | return true; |
||
237 | } elseif (false !== ($matches = $this->getParseUriString($uriString))) { |
||
238 | $parameters = []; |
||
239 | $closure = new \ReflectionFunction($this->closure); |
||
240 | |||
241 | if (is_string(key($matches))) { |
||
|
|||
242 | foreach ($closure->getParameters() as $index => $parameter) { |
||
243 | if (($class = $parameter->getClass()) instanceof \ReflectionClass) { |
||
244 | $className = $class->getName(); |
||
245 | if (class_exists($className)) { |
||
246 | if (isset($matches[ $parameter->name ])) { |
||
247 | $parameters[ $index ] = new $className($matches[ $parameter->name ]); |
||
248 | } |
||
249 | } |
||
250 | } elseif (isset($matches[ $parameter->name ])) { |
||
251 | $parameters[ $index ] = $matches[ $parameter->name ]; |
||
252 | } else { |
||
253 | $parameters[ $index ] = null; |
||
254 | } |
||
255 | } |
||
256 | } else { |
||
257 | foreach ($closure->getParameters() as $index => $parameter) { |
||
258 | if (isset($matches[ $index ])) { |
||
259 | $parameters[ $index ] = $matches[ $index ]; |
||
260 | } else { |
||
261 | $parameters[ $index ] = null; |
||
262 | } |
||
263 | } |
||
264 | } |
||
265 | |||
266 | $this->closureParameters = $parameters; |
||
267 | |||
268 | return true; |
||
269 | } |
||
270 | |||
271 | return false; |
||
272 | } |
||
273 | |||
274 | // ------------------------------------------------------------------------ |
||
275 | |||
276 | /** |
||
277 | * Action::getParseUriString |
||
278 | * |
||
279 | * @param string $uriString |
||
280 | * |
||
281 | * @return array|bool |
||
282 | */ |
||
283 | public function getParseUriString($uriString) |
||
284 | { |
||
285 | // Convert wildcards to RegEx |
||
286 | $regex = str_replace(['/(:any?)', ':any', ':num'], ['/?([^/]+)?', '[^/]+', '[0-9]+'], $this->path); |
||
287 | $regex = str_replace('/', '\/', $regex); |
||
288 | |||
289 | $uriString = '/' . ltrim($uriString, '/'); |
||
290 | |||
291 | // CodeIgniter Like Routing |
||
292 | if (preg_match('/' . $regex . '/', $uriString, $matches)) { |
||
293 | |||
294 | // Remove first match |
||
295 | array_shift($matches); |
||
296 | |||
297 | return (count($matches) ? $matches : false); |
||
298 | } |
||
299 | |||
300 | // Laravel Like Routing |
||
301 | if (preg_match("/{(.*)}/", $this->path)) { |
||
302 | // Try to find from each parts |
||
303 | $pathParts = explode('/', $this->path); |
||
304 | $stringParts = explode('/', $uriString); |
||
305 | |||
306 | $pathParts = array_filter($pathParts); |
||
307 | $stringParts = array_filter($stringParts); |
||
308 | |||
309 | $matches = []; |
||
310 | $parameters = []; |
||
311 | |||
312 | for ($i = 0; $i <= count($pathParts); $i++) { |
||
313 | if (isset($pathParts[ $i ]) && isset($stringParts[ $i ])) { |
||
314 | if ($pathParts[ $i ] == $stringParts[ $i ]) { |
||
315 | $matches[ $i ] = $stringParts[ $i ]; |
||
316 | } |
||
317 | } |
||
318 | |||
319 | if (isset($pathParts[ $i ])) { |
||
320 | if (preg_match("/{(.*)}/", $pathParts[ $i ])) { |
||
321 | $index = str_replace(['{$', '}'], '', $pathParts[ $i ]); |
||
322 | $parameters[ $index ] = isset($stringParts[ $i ]) ? $stringParts[ $i ] : null; |
||
323 | } |
||
324 | } |
||
325 | } |
||
326 | |||
327 | return (count($matches) ? $parameters : false); |
||
328 | } |
||
329 | |||
330 | return false; |
||
331 | } |
||
332 | |||
333 | // ------------------------------------------------------------------------ |
||
334 | |||
335 | /** |
||
336 | * Action::isValidHttpMethod |
||
337 | * |
||
338 | * @param string $method |
||
339 | * |
||
340 | * @return bool |
||
341 | */ |
||
342 | public function isValidHttpMethod($method) |
||
343 | { |
||
344 | $method = strtoupper($method); |
||
345 | |||
346 | if (in_array('ANY', $this->methods)) { |
||
347 | return true; |
||
348 | } |
||
349 | |||
350 | return (bool)in_array($method, $this->methods); |
||
351 | } |
||
352 | |||
353 | // ------------------------------------------------------------------------ |
||
354 | |||
355 | /** |
||
356 | * Action::isAnyHttpMethod |
||
357 | * |
||
358 | * @return bool |
||
359 | */ |
||
360 | public function isAnyHttpMethod() |
||
363 | } |
||
364 | } |