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 |
||
10 | class Configuration |
||
11 | { |
||
12 | use DefaultConfiguration; |
||
13 | |||
14 | /** |
||
15 | * Configuration file name. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected static $configFileName = '.press-cli.json'; |
||
20 | |||
21 | /** |
||
22 | * Creates the configuration file. |
||
23 | * |
||
24 | * @param string $directory |
||
25 | * @param string $name |
||
26 | * @param InputInterface $input |
||
27 | * @param OutputInterface $output |
||
28 | * @param QuestionHelper $helper |
||
29 | */ |
||
30 | public static function create($directory, $name, InputInterface $input, OutputInterface $output, QuestionHelper $helper) |
||
31 | { |
||
32 | // Create the global configuration if needed. |
||
33 | if (!GlobalConfiguration::exists()) { |
||
34 | $output->writeln(''); |
||
35 | GlobalConfiguration::create($output); |
||
36 | $output->writeln(''); |
||
37 | } |
||
38 | |||
39 | // Start local configuration. |
||
40 | $output->writeln("\n<info>Creating project configuration...</info>"); |
||
41 | $configFile = rtrim($directory, '/') . '/' . self::$configFileName; |
||
42 | |||
43 | if (file_exists($configFile)) { |
||
44 | $output->writeln("<comment>Project configuration already exists at {$configFile}.</comment>"); |
||
45 | |||
46 | return; |
||
47 | } |
||
48 | |||
49 | // Add configuration from skeleton. |
||
50 | $skeleton = self::configSkeleton($name, $input, $output, $helper); |
||
51 | self::write($skeleton, $configFile); |
||
52 | |||
53 | $output->writeln("<info>\nProject configuration created at {$configFile}!</info>"); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Writes the configuration to the file. |
||
58 | * |
||
59 | * @param array $config |
||
60 | * @param string $configFile |
||
61 | */ |
||
62 | protected static function write($config, $configFile = null) |
||
63 | { |
||
64 | $configFile = is_null($configFile) ? self::$configFileName : $configFile; |
||
65 | $config = json_encode($config, JSON_PRETTY_PRINT); |
||
66 | $config = stripslashes($config); |
||
67 | file_put_contents($configFile, $config); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Removes the user password. |
||
72 | */ |
||
73 | public static function cleanUpConfigForVCS() |
||
74 | { |
||
75 | $global = GlobalConfiguration::get(); |
||
76 | $afterInstall = isset($global['settings']['afterInstall']) ? $global['settings']['afterInstall'] : []; |
||
77 | |||
78 | if (!$afterInstall) { |
||
79 | return; |
||
80 | } |
||
81 | |||
82 | $config = self::get(); |
||
83 | |||
84 | // Remove user password. |
||
85 | $removeUserPassword = isset($afterInstall['removeUserPassword']) ? (bool) $afterInstall['removeUserPassword'] : false; |
||
86 | if ($removeUserPassword) { |
||
87 | $config['user']['password'] = ''; |
||
88 | } |
||
89 | |||
90 | // Remove user email. |
||
91 | $removeUserPassword = isset($afterInstall['removeUserEmail']) ? (bool) $afterInstall['removeUserEmail'] : false; |
||
92 | if ($removeUserPassword) { |
||
93 | $config['user']['email'] = ''; |
||
94 | } |
||
95 | |||
96 | // Remove user name. |
||
97 | $removeUserPassword = isset($afterInstall['removeUserName']) ? (bool) $afterInstall['removeUserName'] : false; |
||
98 | if ($removeUserPassword) { |
||
99 | $config['user']['name'] = ''; |
||
100 | } |
||
101 | |||
102 | self::write($config); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Get the configuration. |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | View Code Duplication | public static function get() |
|
|
|||
111 | { |
||
112 | if (!self::exists()) { |
||
113 | return []; |
||
114 | } |
||
115 | |||
116 | $config = json_decode(file_get_contents(self::$configFileName), true); |
||
117 | |||
118 | return $config; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Configuration skeleton |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | protected static function configSkeleton($name, InputInterface $input, OutputInterface $output, QuestionHelper $helper) |
||
127 | { |
||
128 | $config = self::getDefaultConfiguration(); |
||
129 | |||
130 | // Merge global and local. |
||
131 | $config = self::emptyConfig($config); |
||
132 | $config = self::mergeConfiguration($config, GlobalConfiguration::get()); |
||
133 | $config = self::removeDuplicatePlugins($config); |
||
134 | |||
135 | // Build out some standard stuff. |
||
136 | $config['database']['name'] = self::makeDbName($name); |
||
137 | $config['site']['url'] = self::makeURL($name); |
||
138 | $config['theme']['name'] = self::makeThemeName($name); |
||
139 | |||
140 | // Validate the configuration options. |
||
141 | $config = Validator::validate($config, $helper, $input, $output); |
||
142 | |||
143 | // Remove settings for local configuration. |
||
144 | if (isset($config['settings'])) { |
||
145 | unset($config['settings']); |
||
146 | } |
||
147 | |||
148 | return $config; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Removes duplicate plugins. |
||
153 | * |
||
154 | * @param array $config |
||
155 | * |
||
156 | * @return array |
||
157 | */ |
||
158 | protected static function removeDuplicatePlugins($config) |
||
159 | { |
||
160 | $plugins = []; |
||
161 | foreach ($config['plugins'] as $plugin) { |
||
162 | $key = $plugin['plugin']; |
||
163 | $plugins[ $key ] = $plugin; |
||
164 | } |
||
165 | |||
166 | $config['plugins'] = array_values($plugins); |
||
167 | |||
168 | return $config; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Merges the global configuration into the local configuration. |
||
173 | * |
||
174 | * @param array $config1 |
||
175 | * @param array $config2 |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | protected static function mergeConfiguration($config1, $config2) |
||
180 | { |
||
181 | foreach ($config2 as $key => $value) { |
||
182 | if (array_key_exists($key, $config1) && is_array($value)) { |
||
183 | $config1[$key] = self::mergeConfiguration($config1[$key], $config2[$key]); |
||
184 | } else { |
||
185 | $config1[$key] = $value; |
||
186 | } |
||
187 | } |
||
188 | |||
189 | return $config1; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Creates the database name from the initialization name. |
||
194 | * |
||
195 | * @param string $name The name. |
||
196 | * |
||
197 | * @return string The database name. |
||
198 | */ |
||
199 | protected static function makeDbName($name) |
||
200 | { |
||
201 | $dbname = self::slugify($name, '_'); |
||
202 | |||
203 | return "wp_{$dbname}"; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Creates the URL from the initialization name. |
||
208 | * |
||
209 | * @param string $name The name. |
||
210 | * |
||
211 | * @return string The URL. |
||
212 | */ |
||
213 | protected static function makeURL($name) |
||
214 | { |
||
215 | $url = self::slugify($name); |
||
216 | |||
217 | return "http://{$url}.dev"; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Creates the theme name from the initialization name. |
||
222 | * |
||
223 | * @param string $name The name. |
||
224 | * |
||
225 | * @return string The theme name. |
||
226 | */ |
||
227 | protected static function makeThemeName($name) |
||
228 | { |
||
229 | $themeName = self::slugify($name); |
||
230 | |||
231 | return $themeName; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Sanitizes the value. |
||
236 | * |
||
237 | * @param string $value The value. |
||
238 | * @param string $separator The separator to use for spaces. |
||
239 | * |
||
240 | * @return string The sanitized value. |
||
241 | */ |
||
242 | protected static function slugify($value, $separator = '-') |
||
243 | { |
||
244 | $value = str_replace([' ', '-', '_'], $separator, $value); |
||
245 | $value = preg_replace('/[^A-Za-z0-9-_]/um', '', $value); |
||
246 | $value = strtolower($value); |
||
247 | |||
248 | return $value; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Checks if the configuration exists. |
||
253 | * |
||
254 | * @return boolean |
||
255 | */ |
||
256 | public static function exists() |
||
260 | |||
261 | /** |
||
262 | * Empty configuration values. |
||
263 | * |
||
264 | * @param array $config The configuration. |
||
265 | * |
||
266 | * @return array The emptied configuration. |
||
267 | */ |
||
268 | protected static function emptyConfigValues($config) { |
||
278 | |||
279 | /** |
||
280 | * Empties the configuration. |
||
281 | * |
||
282 | * @param array $config The configuration. |
||
283 | * |
||
284 | * @return array The emptied configuration. |
||
285 | */ |
||
286 | protected static function emptyConfig($config) { |
||
298 | } |
||
299 |
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.