Complex classes like RouteHandler 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 RouteHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class RouteHandler |
||
14 | { |
||
15 | /** |
||
16 | * @var ContainerInterface $di the dependency/service container. |
||
17 | */ |
||
18 | protected $di; |
||
19 | |||
20 | |||
21 | |||
22 | /** |
||
23 | * Handle the action for a route and return the results. |
||
24 | * |
||
25 | * @param string $method the request method. |
||
26 | * @param string $path that was matched. |
||
27 | * @param string|array $action base for the callable. |
||
28 | * @param array $arguments optional arguments. |
||
29 | * @param ContainerInjectableInterface $di container with services. |
||
30 | * |
||
31 | * @return mixed as the result from the route handler. |
||
32 | */ |
||
33 | 143 | public function handle( |
|
69 | |||
70 | |||
71 | |||
72 | /** |
||
73 | * Get an informative string representing the handler type. |
||
74 | * |
||
75 | * @param string|array $action base for the callable. |
||
76 | * @param ContainerInjectableInterface $di container with services. |
||
77 | * |
||
78 | * @return string as the type of handler. |
||
79 | */ |
||
80 | 2 | public function getHandlerType( |
|
81 | $action, |
||
82 | ContainerInterface $di = null |
||
83 | ) { |
||
84 | 2 | if (is_null($action)) { |
|
85 | 1 | return "null"; |
|
86 | } |
||
87 | |||
88 | 2 | if (is_callable($action)) { |
|
89 | 1 | return "callable"; |
|
90 | } |
||
91 | |||
92 | 2 | if (is_string($action) && class_exists($action)) { |
|
93 | 1 | $callable = $this->isControllerAction(null, null, $action); |
|
94 | 1 | if ($callable) { |
|
95 | 1 | return "controller"; |
|
96 | } |
||
97 | } |
||
98 | |||
99 | 2 | if ($di |
|
100 | 2 | && is_array($action) |
|
101 | 2 | && isset($action[0]) |
|
102 | 2 | && isset($action[1]) |
|
103 | 2 | && is_string($action[0]) |
|
104 | 2 | && $di->has($action[0]) |
|
105 | 2 | && is_callable([$di->get($action[0]), $action[1]]) |
|
106 | ) { |
||
107 | 1 | return "di"; |
|
108 | } |
||
109 | |||
110 | 1 | return "not found"; |
|
111 | } |
||
112 | |||
113 | |||
114 | |||
115 | /** |
||
116 | * Check if items can be used to call a controller action, verify |
||
117 | * that the controller exists, the action has a class-method to call. |
||
118 | * |
||
119 | * @param string $method the request method. |
||
120 | * @param string $path the matched path, base for the controller action |
||
121 | * and the arguments. |
||
122 | * @param string $class the controller class |
||
123 | * |
||
124 | * @return array with callable details. |
||
125 | */ |
||
126 | 22 | protected function isControllerAction( |
|
154 | |||
155 | |||
156 | |||
157 | /** |
||
158 | * Call the controller action with optional arguments and call |
||
159 | * initialisation methods if available. |
||
160 | * |
||
161 | * @param string $callable with details on what controller action to call. |
||
162 | * |
||
163 | * @return mixed result from the handler. |
||
164 | */ |
||
165 | 19 | protected function handleAsControllerAction(array $callable) |
|
196 | |||
197 | |||
198 | |||
199 | /** |
||
200 | * Handle as callable support callables where the method is not static. |
||
201 | * |
||
202 | * @param string|array $action base for the callable |
||
203 | * @param array $arguments optional arguments |
||
204 | * @param ContainerInjectableInterface $di container with services |
||
205 | * |
||
206 | * @return mixed as the result from the route handler. |
||
207 | */ |
||
208 | 117 | protected function handleAsCallable( |
|
238 | |||
239 | |||
240 | |||
241 | /** |
||
242 | * Load callable as a service from the $di container. |
||
243 | * |
||
244 | * @param string|array $action base for the callable |
||
245 | * @param array $arguments optional arguments |
||
246 | * @param ContainerInjectableInterface $di container with services |
||
247 | * |
||
248 | * @return mixed as the result from the route handler. |
||
249 | */ |
||
250 | 3 | protected function handleUsingDi( |
|
271 | } |
||
272 |