Complex classes like RoutingServiceProvider 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 RoutingServiceProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class RoutingServiceProvider implements ServiceProviderInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var |
||
18 | */ |
||
19 | protected $appRoutingKey; |
||
20 | |||
21 | /** |
||
22 | * @param string $appRoutingKey |
||
23 | */ |
||
24 | public function __construct($appRoutingKey = 'config.routes') |
||
28 | |||
29 | /** |
||
30 | * @param Application $app |
||
31 | * @throws \InvalidArgumentException |
||
32 | */ |
||
33 | public function register(Application $app) |
||
34 | { |
||
35 | if (isset($app[$this->appRoutingKey])) { |
||
36 | if (is_array($app[$this->appRoutingKey])) { |
||
37 | $this->addRoutes($app, $app[$this->appRoutingKey]); |
||
38 | } else { |
||
39 | throw new InvalidArgumentException('config.routes must be of type Array'); |
||
40 | } |
||
41 | } |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param Application $app |
||
46 | * @codeCoverageIgnore |
||
47 | */ |
||
48 | public function boot(Application $app) |
||
51 | |||
52 | /** |
||
53 | * Adds all routes |
||
54 | * |
||
55 | * @param Application $app |
||
56 | * @param $routes |
||
57 | */ |
||
58 | public function addRoutes(Application $app, $routes) |
||
69 | |||
70 | /** |
||
71 | * Adds a route, a given route-name (for named routes) and all of its methods |
||
72 | * |
||
73 | * @param Application $app |
||
74 | * @param array $route |
||
75 | * @throws InvalidArgumentException |
||
76 | */ |
||
77 | public function addRoute(Application $app, array $route, $name = '') |
||
111 | |||
112 | /** |
||
113 | * Validates the given methods. Only get, put, post, delete, options, head |
||
114 | * are allowed |
||
115 | * |
||
116 | * @param array $methods |
||
117 | */ |
||
118 | protected function validateMethods(Array $methods) |
||
127 | |||
128 | /** |
||
129 | * Validates the given $route Array |
||
130 | * |
||
131 | * @param $route |
||
132 | * @throws \InvalidArgumentException |
||
133 | */ |
||
134 | protected function validateRoute($route) |
||
153 | |||
154 | |||
155 | /** |
||
156 | * Sanitizes the routeName for named route: |
||
157 | * |
||
158 | * - replaces '/', ':', '|', '-' with '_' |
||
159 | * - removes special characters |
||
160 | * |
||
161 | * Algorithm copied from \Silex\Controller->generateRouteName |
||
162 | * see: https://github.com/silexphp/Silex/blob/1.2/src/Silex/Controller.php |
||
163 | * |
||
164 | * @param string $routeName |
||
165 | * @return string |
||
166 | */ |
||
167 | protected function sanitizeRouteName($routeName) |
||
180 | |||
181 | /** |
||
182 | * @param Controller $controller |
||
183 | * @param $actions |
||
184 | * @param $type |
||
185 | * @throws \InvalidArgumentException |
||
186 | */ |
||
187 | protected function addActions(Controller $controller, $actions, $type) |
||
219 | |||
220 | /** |
||
221 | * @param Controller $controller |
||
222 | * @param $name |
||
223 | * @param $value |
||
224 | * @param $type |
||
225 | */ |
||
226 | protected function addAction(Controller $controller, $name, $value, $type) |
||
230 | |||
231 | /** |
||
232 | * @param Controller $controller |
||
233 | * @param $type |
||
234 | * @param array $values |
||
235 | */ |
||
236 | protected function addSecure(Controller $controller, $type, Array $values) |
||
240 | |||
241 | protected function isClosure($param) |
||
245 | |||
246 | /** |
||
247 | * Adds a middleware (before/after) |
||
248 | * |
||
249 | * @param Controller $controller |
||
250 | * @param string $type | 'before' or 'after' |
||
251 | * @param $value |
||
252 | */ |
||
253 | protected function addBeforeAfterMiddleware(Controller $controller, $type, $value) |
||
274 | |||
275 | /** |
||
276 | * Adds a before/after middleware by its configuration |
||
277 | * |
||
278 | * @param Controller $controller |
||
279 | * @param $type |
||
280 | * @param $value |
||
281 | */ |
||
282 | protected function addMiddlewareFromConfig(Controller $controller, $type, $value) |
||
304 | } |
||
305 |