Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FOSRestExtension 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 FOSRestExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class FOSRestExtension extends Extension |
||
23 | { |
||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | */ |
||
27 | 1 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
28 | { |
||
29 | 1 | return new Configuration($container->getParameter('kernel.debug')); |
|
30 | } |
||
31 | |||
32 | /** |
||
33 | * Loads the services based on your application configuration. |
||
34 | * |
||
35 | * @param array $configs |
||
36 | * @param ContainerBuilder $container |
||
37 | * |
||
38 | * @throws \InvalidArgumentException |
||
39 | * @throws \LogicException |
||
40 | */ |
||
41 | 55 | public function load(array $configs, ContainerBuilder $container) |
|
42 | { |
||
43 | 55 | $configuration = new Configuration($container->getParameter('kernel.debug')); |
|
44 | 55 | $config = $this->processConfiguration($configuration, $configs); |
|
45 | |||
46 | 50 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
47 | 50 | $loader->load('view.xml'); |
|
48 | 50 | $loader->load('routing.xml'); |
|
49 | 50 | $loader->load('request.xml'); |
|
50 | 50 | $loader->load('serializer.xml'); |
|
51 | |||
52 | 50 | $container->getDefinition('fos_rest.routing.loader.controller')->replaceArgument(4, $config['routing_loader']['default_format']); |
|
53 | 50 | $container->getDefinition('fos_rest.routing.loader.yaml_collection')->replaceArgument(4, $config['routing_loader']['default_format']); |
|
54 | 50 | $container->getDefinition('fos_rest.routing.loader.xml_collection')->replaceArgument(4, $config['routing_loader']['default_format']); |
|
55 | |||
56 | 50 | $container->getDefinition('fos_rest.routing.loader.yaml_collection')->replaceArgument(2, $config['routing_loader']['include_format']); |
|
57 | 50 | $container->getDefinition('fos_rest.routing.loader.xml_collection')->replaceArgument(2, $config['routing_loader']['include_format']); |
|
58 | 50 | $container->getDefinition('fos_rest.routing.loader.reader.action')->replaceArgument(3, $config['routing_loader']['include_format']); |
|
59 | |||
60 | // The validator service alias is only set if validation is enabled for the request body converter |
||
61 | 50 | $validator = $config['service']['validator']; |
|
62 | 50 | unset($config['service']['validator']); |
|
63 | |||
64 | 50 | foreach ($config['service'] as $key => $service) { |
|
65 | 50 | if (null !== $service) { |
|
66 | 50 | $container->setAlias('fos_rest.'.$key, $service); |
|
67 | 50 | } |
|
68 | 50 | } |
|
69 | |||
70 | 50 | $this->loadForm($config, $loader, $container); |
|
71 | 50 | $this->loadException($config, $loader, $container); |
|
72 | 50 | $this->loadBodyConverter($config, $validator, $loader, $container); |
|
73 | 50 | $this->loadView($config, $loader, $container); |
|
74 | |||
75 | 50 | $this->loadBodyListener($config, $loader, $container); |
|
76 | 50 | $this->loadFormatListener($config, $loader, $container); |
|
77 | 50 | $this->loadVersioning($config, $loader, $container); |
|
78 | 50 | $this->loadParamFetcherListener($config, $loader, $container); |
|
79 | 50 | $this->loadAllowedMethodsListener($config, $loader, $container); |
|
80 | 50 | $this->loadAccessDeniedListener($config, $loader, $container); |
|
81 | 50 | $this->loadZoneMatcherListener($config, $loader, $container); |
|
82 | |||
83 | // Needs RequestBodyParamConverter and View Handler loaded. |
||
84 | 50 | $this->loadSerializer($config, $container); |
|
85 | 50 | } |
|
86 | |||
87 | 50 | private function loadForm(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
94 | |||
95 | 50 | private function loadAccessDeniedListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
110 | |||
111 | 50 | View Code Duplication | private function loadAllowedMethodsListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
124 | |||
125 | 50 | private function loadBodyListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
150 | |||
151 | 50 | private function loadFormatListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
152 | { |
||
153 | 50 | View Code Duplication | if ($config['format_listener']['enabled'] && !empty($config['format_listener']['rules'])) { |
154 | 6 | $loader->load('format_listener.xml'); |
|
155 | |||
156 | 6 | if (!empty($config['format_listener']['service'])) { |
|
157 | $service = $container->getDefinition('fos_rest.format_listener'); |
||
158 | $service->clearTag('kernel.event_listener'); |
||
159 | } |
||
160 | |||
161 | 6 | $container->setParameter( |
|
162 | 6 | 'fos_rest.format_listener.rules', |
|
163 | 6 | $config['format_listener']['rules'] |
|
164 | 6 | ); |
|
165 | 6 | } |
|
166 | 50 | } |
|
167 | |||
168 | 50 | private function loadVersioning(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
169 | { |
||
170 | 50 | if (!empty($config['versioning']['enabled'])) { |
|
171 | 2 | $loader->load('versioning.xml'); |
|
172 | |||
173 | 2 | $versionListener = $container->getDefinition('fos_rest.versioning.listener'); |
|
174 | 2 | $versionListener->replaceArgument(2, $config['versioning']['default_version']); |
|
175 | |||
176 | 2 | $resolvers = []; |
|
177 | 2 | View Code Duplication | if ($config['versioning']['resolvers']['query']['enabled']) { |
178 | 2 | $resolvers['query'] = $container->getDefinition('fos_rest.versioning.query_parameter_resolver'); |
|
179 | 2 | $resolvers['query']->replaceArgument(0, $config['versioning']['resolvers']['query']['parameter_name']); |
|
180 | 2 | } |
|
181 | 2 | View Code Duplication | if ($config['versioning']['resolvers']['custom_header']['enabled']) { |
182 | 2 | $resolvers['custom_header'] = $container->getDefinition('fos_rest.versioning.header_resolver'); |
|
183 | 2 | $resolvers['custom_header']->replaceArgument(0, $config['versioning']['resolvers']['custom_header']['header_name']); |
|
184 | 2 | } |
|
185 | 2 | View Code Duplication | if ($config['versioning']['resolvers']['media_type']['enabled']) { |
186 | 2 | $resolvers['media_type'] = $container->getDefinition('fos_rest.versioning.media_type_resolver'); |
|
187 | 2 | $resolvers['media_type']->replaceArgument(0, $config['versioning']['resolvers']['media_type']['regex']); |
|
188 | 2 | } |
|
189 | |||
190 | 2 | $chainResolver = $container->getDefinition('fos_rest.versioning.chain_resolver'); |
|
191 | 2 | foreach ($config['versioning']['guessing_order'] as $resolver) { |
|
192 | 2 | if (isset($resolvers[$resolver])) { |
|
193 | 2 | $chainResolver->addMethodCall('addResolver', [$resolvers[$resolver]]); |
|
194 | 2 | } |
|
195 | 2 | } |
|
196 | 2 | } |
|
197 | 50 | } |
|
198 | |||
199 | 50 | View Code Duplication | private function loadParamFetcherListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
214 | |||
215 | 50 | private function loadBodyConverter(array $config, $validator, XmlFileLoader $loader, ContainerBuilder $container) |
|
233 | |||
234 | 50 | private function loadView(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
312 | |||
313 | 50 | private function loadException(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
314 | { |
||
315 | 50 | if ($config['exception']['enabled']) { |
|
316 | 14 | $loader->load('exception_listener.xml'); |
|
342 | |||
343 | 50 | private function loadSerializer(array $config, ContainerBuilder $container) |
|
364 | |||
365 | 50 | private function loadZoneMatcherListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
383 | |||
384 | 2 | private function createZoneRequestMatcher(ContainerBuilder $container, $path = null, $host = null, $methods = array(), $ip = null) |
|
406 | } |
||
407 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.