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 |
||
33 | class ServiceProvider implements ServiceProviderInterface, BootableProviderInterface |
||
34 | { |
||
35 | |||
36 | /** |
||
37 | * Holds the data instances. |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $datas; |
||
41 | |||
42 | /** |
||
43 | * Holds the map for overriding templates. |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $templates = []; |
||
47 | |||
48 | /** |
||
49 | * Initializes needed but yet missing service providers. |
||
50 | * |
||
51 | * @param Container $app |
||
52 | * the application container |
||
53 | */ |
||
54 | 77 | protected function initMissingServiceProviders(Container $app) |
|
73 | |||
74 | /** |
||
75 | * Initializes the available locales. |
||
76 | * |
||
77 | * @param Translator $translator |
||
78 | * the translator |
||
79 | * |
||
80 | * @return array |
||
81 | * the available locales |
||
82 | */ |
||
83 | 76 | protected function initLocales(Translator $translator) |
|
93 | |||
94 | /** |
||
95 | * Initializes the children of the data entries. |
||
96 | */ |
||
97 | 76 | protected function initChildren() |
|
108 | |||
109 | /** |
||
110 | * Gets a map with localized entity labels from the CRUD YML. |
||
111 | * |
||
112 | * @param array $locales |
||
113 | * the available locales |
||
114 | * @param array $crud |
||
115 | * the CRUD entity map |
||
116 | * |
||
117 | * @return array |
||
118 | * the map with localized entity labels |
||
119 | */ |
||
120 | 76 | protected function getLocaleLabels(array $locales, array $crud) |
|
130 | |||
131 | /** |
||
132 | * Configures the EntityDefinition according to the given |
||
133 | * CRUD entity map. |
||
134 | * |
||
135 | * @param EntityDefinition $definition |
||
136 | * the definition to configure |
||
137 | * @param array $crud |
||
138 | * the CRUD entity map |
||
139 | */ |
||
140 | 76 | protected function configureDefinition(EntityDefinition $definition, array $crud) |
|
161 | |||
162 | /** |
||
163 | * Creates and setups an EntityDefinition instance. |
||
164 | * |
||
165 | * @param Translator $translator |
||
166 | * the Translator to use for some standard field labels |
||
167 | * @param EntityDefinitionFactoryInterface $entityDefinitionFactory |
||
168 | * the EntityDefinitionFactory to use |
||
169 | * @param array $locales |
||
170 | * the available locales |
||
171 | * @param array $crud |
||
172 | * the parsed YAML of a CRUD entity |
||
173 | * @param string $name |
||
174 | * the name of the entity |
||
175 | * |
||
176 | * @return EntityDefinition |
||
177 | * the EntityDefinition good to go |
||
178 | */ |
||
179 | 76 | protected function createDefinition(Translator $translator, EntityDefinitionFactoryInterface $entityDefinitionFactory, array $locales, array $crud, $name) |
|
200 | |||
201 | /** |
||
202 | * Validates the parsed entity definition. |
||
203 | * |
||
204 | * @param Container $app |
||
205 | * the application container |
||
206 | * @param array $entityDefinition |
||
207 | * the entity definition to validate |
||
208 | */ |
||
209 | 76 | protected function validateEntityDefinition(Container $app, array $entityDefinition) |
|
219 | |||
220 | /** |
||
221 | * Initializes the instance. |
||
222 | * |
||
223 | * @param string|null $crudFileCachingDirectory |
||
224 | * the writable directory to store the CRUD YAML file cache |
||
225 | * @param Container $app |
||
226 | * the application container |
||
227 | */ |
||
228 | 77 | public function init($crudFileCachingDirectory, Container $app) |
|
247 | |||
248 | /** |
||
249 | * Implements ServiceProviderInterface::register() registering $app['crud']. |
||
250 | * $app['crud'] contains an instance of the ServiceProvider afterwards. |
||
251 | * |
||
252 | * @param Container $app |
||
253 | * the Container instance of the Silex application |
||
254 | */ |
||
255 | 11 | public function register(Container $app) |
|
268 | |||
269 | /** |
||
270 | * Initializes the crud service right after boot. |
||
271 | * |
||
272 | * @param Application $app |
||
273 | * the Container instance of the Silex application |
||
274 | */ |
||
275 | 77 | public function boot(Application $app) |
|
281 | |||
282 | /** |
||
283 | * Getter for the AbstractData instances. |
||
284 | * |
||
285 | * @param string $name |
||
286 | * the entity name of the desired Data instance |
||
287 | * |
||
288 | * @return AbstractData |
||
289 | * the AbstractData instance or null on invalid name |
||
290 | */ |
||
291 | 70 | public function getData($name) |
|
298 | |||
299 | /** |
||
300 | * Getter for all available entity names. |
||
301 | * |
||
302 | * @return string[] |
||
303 | * a list of all available entity names |
||
304 | */ |
||
305 | 7 | public function getEntities() |
|
309 | |||
310 | /** |
||
311 | * Getter for the entities for the navigation bar. |
||
312 | * |
||
313 | * @return string[] |
||
314 | * a list of all available entity names with their group |
||
315 | */ |
||
316 | 10 | public function getEntitiesNavBar() |
|
329 | |||
330 | /** |
||
331 | * Sets a template to use instead of the build in ones. |
||
332 | * |
||
333 | * @param $key |
||
334 | * the template key to use in this format: |
||
335 | * $section.$action.$entity |
||
336 | * $section.$action |
||
337 | * $section |
||
338 | * @param $template |
||
339 | */ |
||
340 | 12 | public function setTemplate($key, $template) |
|
344 | |||
345 | /** |
||
346 | * Determines the Twig template to use for the given parameters depending on |
||
347 | * the existance of certain template keys set in this order: |
||
348 | * |
||
349 | * $section.$action.$entity |
||
350 | * $section.$action |
||
351 | * $section |
||
352 | * |
||
353 | * If nothing exists, this string is returned: "@crud/<action>.twig" |
||
354 | * |
||
355 | * @param string $section |
||
356 | * the section of the template, either "layout" or "template" |
||
357 | * @param string $action |
||
358 | * the current calling action like "create" or "show" |
||
359 | * @param string $entity |
||
360 | * the current calling entity |
||
361 | * |
||
362 | * @return string |
||
363 | * the best fitting template |
||
364 | */ |
||
365 | 11 | public function getTemplate($section, $action, $entity) |
|
383 | |||
384 | /** |
||
385 | * Sets the locale to be used. |
||
386 | * |
||
387 | * @param string $locale |
||
388 | * the locale to be used. |
||
389 | */ |
||
390 | 10 | public function setLocale($locale) |
|
396 | |||
397 | /** |
||
398 | * Gets the available locales. |
||
399 | * |
||
400 | * @return array |
||
401 | * the available locales |
||
402 | */ |
||
403 | 77 | public function getLocales() |
|
421 | |||
422 | } |
||
423 |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.