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 |
||
31 | class ServiceProvider implements ServiceProviderInterface, BootableProviderInterface { |
||
32 | |||
33 | /** |
||
34 | * Holds the {@see AbstractData} instances. |
||
35 | */ |
||
36 | protected $datas; |
||
37 | |||
38 | /** |
||
39 | * Initializes needed but yet missing service providers. |
||
40 | * |
||
41 | * @param Container $app |
||
42 | * the application container |
||
43 | */ |
||
44 | 75 | protected function initMissingServiceProviders(Container $app) { |
|
62 | |||
63 | /** |
||
64 | * Initializes the available locales. |
||
65 | * |
||
66 | * @param Container $app |
||
67 | * the application container |
||
68 | * |
||
69 | * @return array |
||
70 | * the available locales |
||
71 | */ |
||
72 | 74 | protected function initLocales(Container $app) { |
|
81 | |||
82 | /** |
||
83 | * Initializes the children of the data entries. |
||
84 | */ |
||
85 | 74 | protected function initChildren() { |
|
95 | |||
96 | /** |
||
97 | * Gets a map with localized entity labels from the CRUD YML. |
||
98 | * |
||
99 | * @param array $locales |
||
100 | * the available locales |
||
101 | * @param array $crud |
||
102 | * the CRUD entity map |
||
103 | * |
||
104 | * @return array |
||
105 | * the map with localized entity labels |
||
106 | */ |
||
107 | 74 | protected function getLocaleLabels($locales, $crud) { |
|
116 | |||
117 | /** |
||
118 | * Configures the EntityDefinition according to the given |
||
119 | * CRUD entity map. |
||
120 | * |
||
121 | * @param EntityDefinition $definition |
||
122 | * the definition to configure |
||
123 | * @param array $crud |
||
124 | * the CRUD entity map |
||
125 | */ |
||
126 | 74 | protected function configureDefinition(EntityDefinition $definition, array $crud) { |
|
145 | |||
146 | /** |
||
147 | * Creates and setups an EntityDefinition instance. |
||
148 | * |
||
149 | * @param Container $app |
||
150 | * the application container |
||
151 | * @param array $locales |
||
152 | * the available locales |
||
153 | * @param array $crud |
||
154 | * the parsed YAML of a CRUD entity |
||
155 | * @param string $name |
||
156 | * the name of the entity |
||
157 | * |
||
158 | * @return EntityDefinition |
||
159 | * the EntityDefinition good to go |
||
160 | */ |
||
161 | 74 | protected function createDefinition(Container $app, array $locales, array $crud, $name) { |
|
183 | |||
184 | /** |
||
185 | * Validates the parsed entity definition. |
||
186 | * |
||
187 | * @param Container $app |
||
188 | * the application container |
||
189 | * @param array $entityDefinition |
||
190 | * the entity definition to validate |
||
191 | */ |
||
192 | 74 | protected function validateEntityDefinition(Container $app, array $entityDefinition) { |
|
201 | |||
202 | /** |
||
203 | * Initializes the instance. |
||
204 | * |
||
205 | * @param string|null $crudFileCachingDirectory |
||
206 | * the writable directory to store the CRUD YAML file cache |
||
207 | * @param Container $app |
||
208 | * the application container |
||
209 | */ |
||
210 | 75 | public function init($crudFileCachingDirectory, Container $app) { |
|
227 | |||
228 | /** |
||
229 | * Implements ServiceProviderInterface::register() registering $app['crud']. |
||
230 | * $app['crud'] contains an instance of the ServiceProvider afterwards. |
||
231 | * |
||
232 | * @param Container $app |
||
233 | * the Container instance of the Silex application |
||
234 | */ |
||
235 | 11 | public function register(Container $app) { |
|
246 | |||
247 | /** |
||
248 | * Initializes the crud service right after boot. |
||
249 | * |
||
250 | * @param Application $app |
||
251 | * the Container instance of the Silex application |
||
252 | */ |
||
253 | 75 | public function boot(Application $app) { |
|
258 | |||
259 | /** |
||
260 | * Getter for the {@see AbstractData} instances. |
||
261 | * |
||
262 | * @param string $name |
||
263 | * the entity name of the desired Data instance |
||
264 | * |
||
265 | * @return AbstractData |
||
266 | * the AbstractData instance or null on invalid name |
||
267 | */ |
||
268 | 68 | public function getData($name) { |
|
274 | |||
275 | /** |
||
276 | * Getter for all available entity names. |
||
277 | * |
||
278 | * @return string[] |
||
279 | * a list of all available entity names |
||
280 | */ |
||
281 | 3 | public function getEntities() { |
|
284 | |||
285 | /** |
||
286 | * Getter for the entities for the navigation bar. |
||
287 | * |
||
288 | * @return string[] |
||
289 | * a list of all available entity names with their group |
||
290 | */ |
||
291 | 10 | public function getEntitiesNavBar() { |
|
303 | |||
304 | /** |
||
305 | * Determines the Twig template to use for the given parameters depending on |
||
306 | * the existance of certain keys in the Container $app in this order: |
||
307 | * |
||
308 | * crud.$section.$action.$entity |
||
309 | * crud.$section.$action |
||
310 | * crud.$section |
||
311 | * |
||
312 | * If nothing exists, this string is returned: "@crud/<action>.twig" |
||
313 | * |
||
314 | * @param Container $app |
||
315 | * the Silex application |
||
316 | * @param string $section |
||
317 | * the section of the template, either "layout" or "template" |
||
318 | * @param string $action |
||
319 | * the current calling action like "create" or "show" |
||
320 | * @param string $entity |
||
321 | * the current calling entity |
||
322 | * |
||
323 | * @return string |
||
324 | * the best fitting template |
||
325 | */ |
||
326 | 11 | public function getTemplate(Container $app, $section, $action, $entity) { |
|
344 | |||
345 | /** |
||
346 | * Sets the locale to be used. |
||
347 | * |
||
348 | * @param string $locale |
||
349 | * the locale to be used. |
||
350 | */ |
||
351 | 10 | public function setLocale($locale) { |
|
356 | |||
357 | /** |
||
358 | * Gets the available locales. |
||
359 | * |
||
360 | * @return array |
||
361 | * the available locales |
||
362 | */ |
||
363 | 75 | public function getLocales() { |
|
380 | |||
381 | } |
||
382 |