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 declare(strict_types=1); |
||
11 | class YamlConfigFileLoader extends ConfigFileLoader |
||
12 | { |
||
13 | const KEY_HEADER = 'header'; |
||
14 | |||
15 | const KEY_DYNAMIC_VARIABLES = 'dynamic'; |
||
16 | |||
17 | const KEY_CONST_VARIABLES = 'const'; |
||
18 | |||
19 | const KEY_DOTENV_PATHS = 'dotenv'; |
||
20 | |||
21 | const KEY_COMMAND_PATHS = 'paths'; |
||
22 | |||
23 | const KEY_ENVIRONMENTS = 'environments'; |
||
24 | |||
25 | const KEY_TEMPLATES = 'templates'; |
||
26 | |||
27 | /** |
||
28 | * @var Parser |
||
29 | */ |
||
30 | private $yamlReader; |
||
31 | |||
32 | /** |
||
33 | * @var ConfigBuilder |
||
34 | */ |
||
35 | private $configBuilder; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $applicationRootDirectory; |
||
41 | |||
42 | /** |
||
43 | * @param Parser $yamlReader |
||
44 | * @param ConfigBuilder $configBuilder |
||
45 | * @param string $applicationRootDirectory |
||
46 | */ |
||
47 | public function __construct(Parser $yamlReader, ConfigBuilder $configBuilder, string $applicationRootDirectory) |
||
53 | |||
54 | /** |
||
55 | * @inheritdoc |
||
56 | */ |
||
57 | public function isSupported(string $file): bool |
||
61 | |||
62 | /** |
||
63 | * @inheritdoc |
||
64 | */ |
||
65 | View Code Duplication | public function load(string $file, array $params): Config |
|
|
|||
66 | { |
||
67 | $contents = $this->loadFileContents($file); |
||
68 | $rawConfigData = $this->parseFileContents($contents); |
||
69 | |||
70 | $this->configBuilder->start(); |
||
71 | |||
72 | $this->configBuilder |
||
73 | ->setHeader( |
||
74 | $this->extractData(self::KEY_HEADER, $rawConfigData, '') |
||
75 | ); |
||
76 | |||
77 | $this->setConfigData($file, $rawConfigData); |
||
78 | |||
79 | $environments = $this->extractData(self::KEY_ENVIRONMENTS, $rawConfigData, []); |
||
80 | |||
81 | foreach ($environments as $name => $data) { |
||
82 | $this->configBuilder->start($name); |
||
83 | $this->setConfigData($file, $data); |
||
84 | } |
||
85 | |||
86 | return $this->configBuilder |
||
87 | ->create($params); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param string $file |
||
92 | * @param array $rawConfigData |
||
93 | */ |
||
94 | private function setConfigData(string $file, array $rawConfigData) |
||
116 | |||
117 | /** |
||
118 | * @param string $key |
||
119 | * @param array $rawConfig |
||
120 | * @param mixed $default |
||
121 | * @return mixed|null |
||
122 | */ |
||
123 | private function extractData(string $key, array $rawConfig, $default = false) |
||
131 | |||
132 | |||
133 | |||
134 | /** |
||
135 | * @param string $contents |
||
136 | * @return array |
||
137 | */ |
||
138 | private function parseFileContents(string $contents): array |
||
142 | |||
143 | /** |
||
144 | * @param string $file |
||
145 | * @param array $rawConfigData |
||
146 | * @param string $key |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | private function extractPaths(string $file, array $rawConfigData, string $key): array |
||
158 | |||
159 | /** |
||
160 | * @param string $file |
||
161 | * @param array $rawConfigData |
||
162 | * @return array |
||
163 | */ |
||
164 | private function extractTemplates(string $file, array $rawConfigData): array |
||
175 | } |
||
176 |
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.