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 |
||
8 | class PageTemplateConfigurationParser implements PageTemplateConfigurationParserInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var KernelInterface |
||
12 | */ |
||
13 | private $kernel; |
||
14 | |||
15 | private $presets = []; |
||
16 | |||
17 | /** |
||
18 | * @param KernelInterface $kernel |
||
19 | * @param array $presets |
||
20 | */ |
||
21 | 2 | public function __construct(KernelInterface $kernel, array $presets = []) |
|
26 | |||
27 | /** |
||
28 | * This will read the $name file and parse it to the PageTemplate |
||
29 | * |
||
30 | * @param string $name |
||
31 | * |
||
32 | * @return PageTemplateInterface |
||
33 | * |
||
34 | * @throws \Exception |
||
35 | */ |
||
36 | 2 | public function parse($name) |
|
57 | |||
58 | /** |
||
59 | * This builds a Region out of the rawRegion from the Yaml |
||
60 | * |
||
61 | * @param array $rawRegion |
||
62 | * |
||
63 | * @return Region |
||
64 | */ |
||
65 | 2 | private function buildRegion($rawRegion) |
|
66 | { |
||
67 | 2 | $children = []; |
|
68 | 2 | $rows = []; |
|
69 | 2 | $rawRegion = array_replace(['regions' => [], 'rows' => []], $rawRegion); |
|
70 | |||
71 | 2 | foreach ($rawRegion['regions'] as $child) { |
|
72 | $children[] = $this->buildRegion($child); |
||
73 | } |
||
74 | |||
75 | 2 | foreach ($rawRegion['rows'] as $row) { |
|
76 | $rows[] = $this->buildRow($row); |
||
77 | } |
||
78 | |||
79 | 2 | $rawRegion = array_replace([ |
|
80 | 2 | 'name' => null, |
|
81 | 'span' => 12, |
||
82 | 'template' => null, |
||
83 | ], $rawRegion); |
||
84 | |||
85 | 2 | return new Region($rawRegion['name'], $rawRegion['span'], $rawRegion['template'], $children, $rows); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * This builds a Row out of the rawRow from the Yaml |
||
90 | * |
||
91 | * @param array $rawRow |
||
92 | * |
||
93 | * @return Row |
||
94 | */ |
||
95 | private function buildRow($rawRow) |
||
105 | |||
106 | /** |
||
107 | * @param $name |
||
108 | * |
||
109 | * @return array |
||
110 | * |
||
111 | * @throws \Exception |
||
112 | */ |
||
113 | 2 | View Code Duplication | private function getRawData($name) |
133 | } |
||
134 |
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.