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:
1 | <?php |
||
19 | final class CrudsEntitiesConfigurator |
||
20 | { |
||
21 | /** @var ContainerBuilder */ |
||
22 | private $container; |
||
23 | |||
24 | /** |
||
25 | * CrudsEntitiesConfigurator constructor. |
||
26 | * |
||
27 | * @param ContainerBuilder $container |
||
28 | */ |
||
29 | 22 | public function __construct(ContainerBuilder $container) |
|
30 | { |
||
31 | 22 | $this->container = $container; |
|
32 | 22 | } |
|
33 | |||
34 | 22 | public function processEntityConfiguration($name, $config) |
|
35 | { |
||
36 | 22 | $class = $config['class']; |
|
37 | 22 | $actions = $config['actions']; |
|
38 | 22 | $prefix = $config['prefix']; |
|
39 | 22 | $manager = $config['manager']; |
|
40 | 22 | $repository = $config['repository']; |
|
41 | 22 | $mount = $config['mount']; |
|
42 | |||
43 | 22 | View Code Duplication | if (null === $manager) { |
1 ignored issue
–
show
|
|||
44 | 22 | $manager = new Definition(ObjectManager::class); |
|
45 | 22 | $manager->setFactory([new Reference('doctrine'), 'getManagerForClass']); |
|
46 | 22 | $manager->setArguments([$class]); |
|
47 | 22 | } else { |
|
48 | $manager = new Reference($this->filterReference($manager)); |
||
49 | } |
||
50 | |||
51 | 22 | View Code Duplication | if (null === $repository) { |
1 ignored issue
–
show
|
|||
52 | 22 | $repositoryDefinition = new Definition(EntityRepository::class); |
|
53 | 22 | $repositoryDefinition->setFactory([$manager, 'getRepository']); |
|
54 | 22 | $repositoryDefinition->setArguments([$class]); |
|
55 | 22 | } else { |
|
56 | $repositoryDefinition = new Reference($this->filterReference($repository)); |
||
57 | } |
||
58 | |||
59 | 22 | foreach ($actions as $action => $actionConfig) { |
|
60 | 22 | if (!$actionConfig['enabled']) { |
|
61 | continue; |
||
62 | } |
||
63 | |||
64 | 22 | $actionConfig['name'] = $name; |
|
65 | 22 | $actionConfig['class'] = $class; |
|
66 | 22 | $actionConfig['mount'] = $mount; |
|
67 | 22 | $actionConfig['repository'] = $repositoryDefinition; |
|
68 | 22 | $actionConfig['path'] = $prefix . $actionConfig['path']; |
|
69 | 22 | $actionConfig['manager'] = $manager; |
|
70 | 22 | $actionConfig['prefix'] = $prefix; |
|
71 | 22 | $function = new \ReflectionMethod($this, 'register' . ucfirst($action) . 'Action'); |
|
72 | 22 | $args = []; |
|
73 | |||
74 | 22 | foreach ($function->getParameters() as $parameter) { |
|
75 | 22 | if (array_key_exists($parameter->getName(), $actionConfig)) { |
|
1 ignored issue
–
show
|
|||
76 | 22 | $args[] = $actionConfig[$parameter->getName()]; |
|
1 ignored issue
–
show
|
|||
77 | 22 | } else { |
|
78 | $args[] = $parameter->getDefaultValue(); |
||
79 | } |
||
80 | 22 | } |
|
81 | 22 | $function->invokeArgs($this, $args); |
|
82 | 22 | } |
|
83 | 22 | } |
|
84 | |||
85 | /** |
||
86 | * @param string $reference |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | 22 | private function filterReference($reference) |
|
91 | { |
||
92 | 22 | return ltrim($reference, '@'); |
|
93 | } |
||
94 | |||
95 | 22 | public function registerCreateAction($mount, $name, $class, $factory, $processor, $path, $manager) |
|
96 | { |
||
97 | 22 | if (null === $factory) { |
|
98 | 22 | $factory = new DefinitionDecorator('cruds.factory.reflection'); |
|
99 | 22 | $factory->setArguments([$class, []]); |
|
100 | 22 | } else { |
|
101 | $factory = new Reference($this->filterReference($factory)); |
||
102 | } |
||
103 | |||
104 | 22 | View Code Duplication | if (null === $processor) { |
105 | 22 | $processor = new Reference('cruds.processor.property_access'); |
|
106 | 22 | } else { |
|
107 | $processor = new Reference($this->filterReference($processor)); |
||
108 | } |
||
109 | |||
110 | 22 | $definition = new Definition(CreateController::class); |
|
111 | 22 | $definition->setArguments( |
|
112 | [ |
||
113 | 22 | $processor, |
|
114 | 22 | $manager, |
|
115 | 22 | $factory, |
|
116 | 22 | $this->getEvm(), |
|
117 | ] |
||
118 | 22 | ); |
|
119 | 22 | $definition->setPublic(true); |
|
120 | |||
121 | 22 | $actionName = 'create'; |
|
122 | 22 | $controllerId = $this->generateControllerId($name, $actionName); |
|
123 | 22 | $this->container->setDefinition($controllerId, $definition); |
|
124 | |||
125 | 22 | $action = $controllerId . ':' . CreateController::ACTION; |
|
126 | 22 | $this->registerRoute( |
|
127 | 22 | $mount, |
|
128 | 22 | $name, |
|
129 | 22 | $actionName, |
|
130 | 22 | $path, |
|
131 | 22 | $action, |
|
132 | 22 | ['POST'], |
|
133 | 22 | ['class' => $class, 'arguments' => ['data']] |
|
134 | 22 | ); |
|
135 | 22 | } |
|
136 | |||
137 | /** |
||
138 | * @return Reference |
||
139 | */ |
||
140 | 22 | private function getEvm() |
|
141 | { |
||
142 | 22 | return new Reference('event_dispatcher'); |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param string $name |
||
147 | * @param string $actionName |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | 22 | private function generateControllerId($name, $actionName) |
|
152 | { |
||
153 | 22 | return $this->normalize('cruds.generated_controller.' . $name . '.' . $actionName); |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param string $name |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | 22 | private function normalize($name) |
|
162 | { |
||
163 | 22 | return str_replace('-', '_', $name); |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param string $mount |
||
168 | * @param string $name |
||
169 | * @param string $actionName |
||
170 | * @param string $path |
||
171 | * @param string $action |
||
172 | * @param array $methods |
||
173 | * @param array $options |
||
174 | * |
||
175 | * @return Definition |
||
176 | * @throws \InvalidArgumentException |
||
177 | */ |
||
178 | 22 | private function registerRoute($mount, $name, $actionName, $path, $action, array $methods, array $options = []) |
|
179 | { |
||
180 | 22 | return $this->getLoaderDefinition()->addMethodCall( |
|
181 | 22 | 'addRoute', |
|
182 | [ |
||
183 | 22 | $mount, |
|
184 | 22 | $this->normalize('cruds.routing.' . $name . '.' . $actionName), |
|
185 | 22 | $path, |
|
186 | 22 | $action, |
|
187 | 22 | $methods, |
|
188 | 22 | array_replace( |
|
189 | [ |
||
190 | 22 | 'action' => $actionName, |
|
191 | 22 | 'mount' => $mount, |
|
192 | 22 | ], |
|
193 | $options |
||
194 | 22 | ), |
|
195 | ] |
||
196 | 22 | ); |
|
197 | } |
||
198 | |||
199 | 22 | private function getLoaderDefinition() |
|
203 | |||
204 | 22 | View Code Duplication | public function registerReadAction($mount, $name, $path, $repository, $class) |
205 | { |
||
206 | 22 | $definition = new Definition(ReadController::class); |
|
207 | 22 | $definition->setArguments( |
|
208 | [ |
||
209 | 22 | $repository, |
|
210 | 22 | $this->getEvm(), |
|
211 | ] |
||
229 | |||
230 | 22 | public function registerUpdateAction($mount, $name, $path, $repository, $processor, $manager, $class) |
|
263 | |||
264 | 22 | View Code Duplication | public function registerDeleteAction($mount, $name, $path, $repository, $manager, $class) |
290 | |||
291 | 22 | public function registerSearchAction($mount, $name, $path, $class, $repository, $criteria, $count_path, $prefix) |
|
355 | } |
||
356 |
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.