Complex classes like ServiceProvider 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 ServiceProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class ServiceProvider implements ServiceProviderInterface, BootableProviderInterface { |
||
33 | |||
34 | /** |
||
35 | * Holds the {@see AbstractData} instances. |
||
36 | */ |
||
37 | protected $datas; |
||
38 | |||
39 | /** |
||
40 | * Initializes needed but yet missing service providers. |
||
41 | * |
||
42 | * @param Container $app |
||
43 | * the application container |
||
44 | */ |
||
45 | 75 | protected function initMissingServiceProviders(Container $app) { |
|
63 | |||
64 | /** |
||
65 | * Initializes the available locales. |
||
66 | * |
||
67 | * @param Container $app |
||
68 | * the application container |
||
69 | * |
||
70 | * @return array |
||
71 | * the available locales |
||
72 | */ |
||
73 | 74 | protected function initLocales(Container $app) { |
|
82 | |||
83 | /** |
||
84 | * Initializes the children of the data entries. |
||
85 | */ |
||
86 | 74 | protected function initChildren() { |
|
96 | |||
97 | /** |
||
98 | * Gets a map with localized entity labels from the CRUD YML. |
||
99 | * |
||
100 | * @param array $locales |
||
101 | * the available locales |
||
102 | * @param array $crud |
||
103 | * the CRUD entity map |
||
104 | * |
||
105 | * @return array |
||
106 | * the map with localized entity labels |
||
107 | */ |
||
108 | 74 | protected function getLocaleLabels($locales, $crud) { |
|
117 | |||
118 | /** |
||
119 | * Configures the EntityDefinition according to the given |
||
120 | * CRUD entity map. |
||
121 | * |
||
122 | * @param EntityDefinition $definition |
||
123 | * the definition to configure |
||
124 | * @param array $crud |
||
125 | * the CRUD entity map |
||
126 | */ |
||
127 | 74 | protected function configureDefinition(EntityDefinition $definition, array $crud) { |
|
146 | |||
147 | /** |
||
148 | * Creates and setups an EntityDefinition instance. |
||
149 | * |
||
150 | * @param Container $app |
||
151 | * the application container |
||
152 | * @param array $locales |
||
153 | * the available locales |
||
154 | * @param array $crud |
||
155 | * the parsed YAML of a CRUD entity |
||
156 | * @param string $name |
||
157 | * the name of the entity |
||
158 | * |
||
159 | * @return EntityDefinition |
||
160 | * the EntityDefinition good to go |
||
161 | */ |
||
162 | 74 | protected function createDefinition(Container $app, array $locales, array $crud, $name) { |
|
184 | |||
185 | /** |
||
186 | * Validates the parsed entity definition. |
||
187 | * |
||
188 | * @param Container $app |
||
189 | * the application container |
||
190 | * @param array $entityDefinition |
||
191 | * the entity definition to validate |
||
192 | */ |
||
193 | 74 | protected function validateEntityDefinition(Container $app, array $entityDefinition) { |
|
202 | |||
203 | /** |
||
204 | * Initializes the instance. |
||
205 | * |
||
206 | * @param DataFactoryInterface $dataFactory |
||
|
|||
207 | * the factory to create the concrete AbstractData instances |
||
208 | * @param string $crudFile |
||
209 | * the CRUD YAML file to parse |
||
210 | * @param string|null $crudFileCachingDirectory |
||
211 | * the writable directory to store the CRUD YAML file cache |
||
212 | * @param Container $app |
||
213 | * the application container |
||
214 | */ |
||
215 | 75 | public function init($crudFileCachingDirectory, Container $app) { |
|
232 | |||
233 | /** |
||
234 | * Implements ServiceProviderInterface::register() registering $app['crud']. |
||
235 | * $app['crud'] contains an instance of the ServiceProvider afterwards. |
||
236 | * |
||
237 | * @param Container $app |
||
238 | * the Container instance of the Silex application |
||
239 | */ |
||
240 | 11 | public function register(Container $app) { |
|
251 | |||
252 | /** |
||
253 | * Initializes the crud service right after boot. |
||
254 | * |
||
255 | * @param Application $app |
||
256 | * the Container instance of the Silex application |
||
257 | */ |
||
258 | 75 | public function boot(Application $app) { |
|
263 | |||
264 | /** |
||
265 | * Getter for the {@see AbstractData} instances. |
||
266 | * |
||
267 | * @param string $name |
||
268 | * the entity name of the desired Data instance |
||
269 | * |
||
270 | * @return AbstractData |
||
271 | * the AbstractData instance or null on invalid name |
||
272 | */ |
||
273 | 68 | public function getData($name) { |
|
279 | |||
280 | /** |
||
281 | * Getter for all available entity names. |
||
282 | * |
||
283 | * @return string[] |
||
284 | * a list of all available entity names |
||
285 | */ |
||
286 | 3 | public function getEntities() { |
|
289 | |||
290 | /** |
||
291 | * Getter for the entities for the navigation bar. |
||
292 | * |
||
293 | * @return string[] |
||
294 | * a list of all available entity names with their group |
||
295 | */ |
||
296 | 10 | public function getEntitiesNavBar() { |
|
308 | |||
309 | /** |
||
310 | * Determines the Twig template to use for the given parameters depending on |
||
311 | * the existance of certain keys in the Container $app in this order: |
||
312 | * |
||
313 | * crud.$section.$action.$entity |
||
314 | * crud.$section.$action |
||
315 | * crud.$section |
||
316 | * |
||
317 | * If nothing exists, this string is returned: "@crud/<action>.twig" |
||
318 | * |
||
319 | * @param Container $app |
||
320 | * the Silex application |
||
321 | * @param string $section |
||
322 | * the section of the template, either "layout" or "template" |
||
323 | * @param string $action |
||
324 | * the current calling action like "create" or "show" |
||
325 | * @param string $entity |
||
326 | * the current calling entity |
||
327 | * |
||
328 | * @return string |
||
329 | * the best fitting template |
||
330 | */ |
||
331 | 11 | public function getTemplate(Container $app, $section, $action, $entity) { |
|
349 | |||
350 | /** |
||
351 | * Sets the locale to be used. |
||
352 | * |
||
353 | * @param string $locale |
||
354 | * the locale to be used. |
||
355 | */ |
||
356 | 10 | public function setLocale($locale) { |
|
361 | |||
362 | /** |
||
363 | * Gets the available locales. |
||
364 | * |
||
365 | * @return array |
||
366 | * the available locales |
||
367 | */ |
||
368 | 75 | public function getLocales() { |
|
385 | |||
386 | } |
||
387 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.