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 Generator 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 Generator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Generator extends TwigGeneratorGenerator |
||
15 | { |
||
16 | const TEMP_DIR_PREFIX = 'Admingenerator'; |
||
17 | |||
18 | /** |
||
19 | * @var array $yaml The yaml array. |
||
20 | */ |
||
21 | protected $yaml; |
||
22 | |||
23 | /** |
||
24 | * @var string $baseController The base controller. |
||
25 | */ |
||
26 | protected $baseController; |
||
27 | |||
28 | /** |
||
29 | * @var string $columnClass The column class. |
||
30 | */ |
||
31 | protected $columnClass = 'Admingenerator\GeneratorBundle\Generator\Column'; |
||
32 | |||
33 | /** |
||
34 | * @var string $baseAdminTemplate The base admin template. |
||
35 | */ |
||
36 | protected $baseAdminTemplate = 'AdmingeneratoroldThemeBundle::base.html.twig'; |
||
37 | |||
38 | /** |
||
39 | * @var string $baseGeneratorName The base generator name. |
||
40 | */ |
||
41 | protected $baseGeneratorName; |
||
42 | |||
43 | /** |
||
44 | * @var array $bundleConfig Generator bundle config. |
||
45 | */ |
||
46 | protected $bundleConfig; |
||
47 | |||
48 | /** |
||
49 | * @var \Symfony\Component\Routing\RouterInterface |
||
50 | */ |
||
51 | protected $router; |
||
52 | |||
53 | /** |
||
54 | * Init a new generator and automatically define the base of tempDir |
||
55 | * |
||
56 | * @param string $cacheDir |
||
57 | * @param Filepath $yaml |
||
58 | */ |
||
59 | public function __construct($cacheDir, $yaml) |
||
64 | |||
65 | /** |
||
66 | * @return string The base admin template. |
||
67 | */ |
||
68 | public function getBaseAdminTemplate() |
||
72 | |||
73 | /** |
||
74 | * @param string $baseAdminTemplate The base admin template. |
||
75 | * @return void |
||
76 | */ |
||
77 | public function setBaseAdminTemplate($baseAdminTemplate) |
||
81 | |||
82 | /** |
||
83 | * Add a builder |
||
84 | * @param BuilderInterface $builder |
||
85 | * |
||
86 | * @return void |
||
87 | */ |
||
88 | public function addBuilder(BuilderInterface $builder) |
||
103 | |||
104 | /** |
||
105 | * Merge parameters from global definition with builder definition |
||
106 | * Fields and actions have special behaviors: |
||
107 | * - fields are merged and all global fields are still available |
||
108 | * from a builder |
||
109 | * - actions depend of builder. List of available actions come |
||
110 | * from builder, configuration is a merge between builder configuration |
||
111 | * and global configuration |
||
112 | * |
||
113 | * @param array $global |
||
114 | * @param array $builder |
||
115 | * |
||
116 | * @return array |
||
117 | */ |
||
118 | protected function mergeParameters(array $global, array $builder) |
||
174 | |||
175 | /** |
||
176 | * Merge configuration on a single level |
||
177 | * |
||
178 | * @param array $global |
||
179 | * @param array $builder |
||
180 | * |
||
181 | * @return array |
||
182 | */ |
||
183 | protected function mergeConfiguration(array $global, array $builder) |
||
205 | |||
206 | /** |
||
207 | * Recursively replaces Base array values with Replacement array values |
||
208 | * while keeping indexes of Replacement array |
||
209 | * |
||
210 | * @param array $base Base array |
||
211 | * @param array $replacement Replacement array |
||
212 | * |
||
213 | * @return array |
||
214 | */ |
||
215 | protected function recursiveReplace($base, $replacement) |
||
234 | |||
235 | /** |
||
236 | * Inject default batch and object actions settings |
||
237 | * |
||
238 | * @return array |
||
239 | */ |
||
240 | protected function applyActionsBuilderDefaults(array $params) |
||
299 | |||
300 | /** |
||
301 | * @param string $columnClass |
||
302 | * @return void |
||
303 | */ |
||
304 | public function setColumnClass($columnClass) |
||
308 | |||
309 | /** |
||
310 | * @return string The column class. |
||
311 | */ |
||
312 | protected function getColumnClass() |
||
316 | |||
317 | /** |
||
318 | * Set the yaml to pass all the vars to the builders |
||
319 | * |
||
320 | * @param array $yaml |
||
321 | * @return void |
||
322 | */ |
||
323 | protected function setYamlConfig(array $yaml) |
||
330 | |||
331 | /** |
||
332 | * @param string $yamlPath Path string with point for levels. |
||
333 | * @param mixed $default Value to default to, path key not found. |
||
334 | * @return mixed |
||
335 | */ |
||
336 | public function getFromYaml($yamlPath, $default = null) |
||
344 | |||
345 | /** |
||
346 | * @param object $fieldGuesser The fieldguesser. |
||
347 | * @return void |
||
348 | */ |
||
349 | public function setFieldGuesser($fieldGuesser) |
||
353 | |||
354 | /** |
||
355 | * @return object The fieldguesser. |
||
356 | */ |
||
357 | public function getFieldGuesser() |
||
361 | |||
362 | /** |
||
363 | * @param string $baseController |
||
364 | */ |
||
365 | public function setBaseController($baseController) |
||
369 | |||
370 | /** |
||
371 | * @return string Base controller. |
||
372 | */ |
||
373 | public function getBaseController() |
||
377 | |||
378 | /** |
||
379 | * @param string $baseGeneratorName |
||
380 | */ |
||
381 | public function setBaseGeneratorName($baseGeneratorName) |
||
385 | |||
386 | /** |
||
387 | * @return string Base generator name. |
||
388 | */ |
||
389 | public function getBaseGeneratorName() |
||
393 | |||
394 | /** |
||
395 | * @return string Generated controller directory. |
||
396 | */ |
||
397 | public function getGeneratedControllerFolder() |
||
401 | |||
402 | /** |
||
403 | * @param \Symfony\Component\Routing\RouterInterface $router |
||
404 | * @return void |
||
405 | */ |
||
406 | public function setRouter(RouterInterface $router) |
||
410 | |||
411 | /** |
||
412 | * @return \Symfony\Component\Routing\RouterInterface $router |
||
413 | */ |
||
414 | public function getRouter() |
||
418 | |||
419 | /** |
||
420 | * @param array $bundleConfig |
||
421 | */ |
||
422 | public function setBundleConfig(array $bundleConfig) |
||
426 | |||
427 | /** |
||
428 | * @return array |
||
429 | */ |
||
430 | public function getBundleConfig() |
||
434 | |||
435 | /** |
||
436 | * @param string $configPath Path string with point for levels. |
||
437 | * @param mixed $default Value to default to, path key not found. |
||
438 | * @return mixed |
||
439 | */ |
||
440 | public function getFromBundleConfig($configPath, $default = null) |
||
448 | |||
449 | /** |
||
450 | * @param array $array The array to traverse. |
||
451 | * @param string $path Path string with point for levels. |
||
452 | * @param mixed $default Value to default to, path key not found. |
||
453 | * @return mixed |
||
454 | */ |
||
455 | View Code Duplication | protected function getFromArray($array, $path, $default = null) |
|
468 | } |
||
469 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: