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  | 
            ||
| 18 | class RoutingServiceProvider implements  | 
            ||
| 19 | ServiceProviderInterface,  | 
            ||
| 20 | BootableProviderInterface,  | 
            ||
| 21 | EventListenerProviderInterface  | 
            ||
| 22 | { | 
            ||
| 23 | /**  | 
            ||
| 24 | * @var  | 
            ||
| 25 | */  | 
            ||
| 26 | protected $appRoutingKey;  | 
            ||
| 27 | |||
| 28 | /**  | 
            ||
| 29 | * @param string $appRoutingKey  | 
            ||
| 30 | */  | 
            ||
| 31 | public function __construct($appRoutingKey = 'config.routes')  | 
            ||
| 35 | |||
| 36 | /**  | 
            ||
| 37 | * @param Container $app  | 
            ||
| 38 | * @throws \InvalidArgumentException  | 
            ||
| 39 | */  | 
            ||
| 40 | public function register(Container $app)  | 
            ||
| 50 | |||
| 51 | /**  | 
            ||
| 52 | * @param Application $app  | 
            ||
| 53 | * @codeCoverageIgnore  | 
            ||
| 54 | */  | 
            ||
| 55 | public function boot(Application $app)  | 
            ||
| 58 | |||
| 59 | /**  | 
            ||
| 60 | * @param Container $app  | 
            ||
| 61 | * @param EventDispatcherInterface $dispatcher  | 
            ||
| 62 | * @codeCoverageIgnore  | 
            ||
| 63 | */  | 
            ||
| 64 | public function subscribe(Container $app, EventDispatcherInterface $dispatcher)  | 
            ||
| 67 | |||
| 68 | /**  | 
            ||
| 69 | * Adds all routes  | 
            ||
| 70 | *  | 
            ||
| 71 | * @param Container $app  | 
            ||
| 72 | * @param $routes  | 
            ||
| 73 | */  | 
            ||
| 74 | public function addRoutes(Container $app, $routes)  | 
            ||
| 85 | |||
| 86 | /**  | 
            ||
| 87 | * Adds a route, a given route-name (for named routes) and all of its methods  | 
            ||
| 88 | *  | 
            ||
| 89 | * @param Container $app  | 
            ||
| 90 | * @param array $route  | 
            ||
| 91 | * @throws InvalidArgumentException  | 
            ||
| 92 | */  | 
            ||
| 93 | public function addRoute(Container $app, array $route, $name = '')  | 
            ||
| 127 | |||
| 128 | /**  | 
            ||
| 129 | * Validates the given methods. Only get, put, post, delete, options, head  | 
            ||
| 130 | * are allowed  | 
            ||
| 131 | *  | 
            ||
| 132 | * @param array $methods  | 
            ||
| 133 | */  | 
            ||
| 134 | protected function validateMethods(Array $methods)  | 
            ||
| 143 | |||
| 144 | /**  | 
            ||
| 145 | * Validates the given $route Array  | 
            ||
| 146 | *  | 
            ||
| 147 | * @param $route  | 
            ||
| 148 | * @throws \InvalidArgumentException  | 
            ||
| 149 | */  | 
            ||
| 150 | protected function validateRoute($route)  | 
            ||
| 169 | |||
| 170 | |||
| 171 | /**  | 
            ||
| 172 | * Sanitizes the routeName for named route:  | 
            ||
| 173 | *  | 
            ||
| 174 | * - replaces '/', ':', '|', '-' with '_'  | 
            ||
| 175 | * - removes special characters  | 
            ||
| 176 | *  | 
            ||
| 177 | * Algorithm copied from \Silex\Controller->generateRouteName  | 
            ||
| 178 | * see: https://github.com/silexphp/Silex/blob/1.2/src/Silex/Controller.php  | 
            ||
| 179 | *  | 
            ||
| 180 | * @param string $routeName  | 
            ||
| 181 | * @return string  | 
            ||
| 182 | */  | 
            ||
| 183 | protected function sanitizeRouteName($routeName)  | 
            ||
| 196 | |||
| 197 | /**  | 
            ||
| 198 | * @param Controller $controller  | 
            ||
| 199 | * @param $actions  | 
            ||
| 200 | * @param $type  | 
            ||
| 201 | * @throws \InvalidArgumentException  | 
            ||
| 202 | */  | 
            ||
| 203 | protected function addActions(Controller $controller, $actions, $type)  | 
            ||
| 235 | |||
| 236 | /**  | 
            ||
| 237 | * @param Controller $controller  | 
            ||
| 238 | * @param $name  | 
            ||
| 239 | * @param $value  | 
            ||
| 240 | * @param $type  | 
            ||
| 241 | */  | 
            ||
| 242 | protected function addAction(Controller $controller, $name, $value, $type)  | 
            ||
| 246 | |||
| 247 | /**  | 
            ||
| 248 | * @param Controller $controller  | 
            ||
| 249 | * @param $type  | 
            ||
| 250 | * @param array $values  | 
            ||
| 251 | */  | 
            ||
| 252 | protected function addSecure(Controller $controller, $type, Array $values)  | 
            ||
| 256 | |||
| 257 | protected function isClosure($param)  | 
            ||
| 261 | |||
| 262 | /**  | 
            ||
| 263 | * Adds a middleware (before/after)  | 
            ||
| 264 | *  | 
            ||
| 265 | * @param Controller $controller  | 
            ||
| 266 | * @param string $type | 'before' or 'after'  | 
            ||
| 267 | * @param $value  | 
            ||
| 268 | */  | 
            ||
| 269 | protected function addBeforeAfterMiddleware(Controller $controller, $type, $value)  | 
            ||
| 290 | |||
| 291 | /**  | 
            ||
| 292 | * Adds a before/after middleware by its configuration  | 
            ||
| 293 | *  | 
            ||
| 294 | * @param Controller $controller  | 
            ||
| 295 | * @param $type  | 
            ||
| 296 | * @param $value  | 
            ||
| 297 | */  | 
            ||
| 298 | protected function addMiddlewareFromConfig(Controller $controller, $type, $value)  | 
            ||
| 320 | }  | 
            
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: