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) |
||
| 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) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param Controller $controller |
||
| 215 | * @param $name |
||
| 216 | * @param $value |
||
| 217 | * @param $type |
||
| 218 | */ |
||
| 219 | protected function addAction(Controller $controller, $name, $value, $type) |
||
| 223 | |||
| 224 | protected function isClosure($param) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Adds a middleware (before/after) |
||
| 231 | * |
||
| 232 | * @param Controller $controller |
||
| 233 | * @param string $type | 'before' or 'after' |
||
| 234 | * @param $value |
||
| 235 | */ |
||
| 236 | protected function addBeforeAfterMiddleware(Controller $controller, $type, $value) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Adds a before/after middleware by its configuration |
||
| 260 | * |
||
| 261 | * @param Controller $controller |
||
| 262 | * @param $type |
||
| 263 | * @param $value |
||
| 264 | */ |
||
| 265 | protected function addMiddlewareFromConfig(Controller $controller, $type, $value) |
||
| 287 | } |
||
| 288 |