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 Installer |
||
11 | { |
||
12 | /** |
||
13 | * Get a decorated message |
||
14 | * |
||
15 | * @param string $message |
||
16 | * @param mixed $type |
||
17 | * @param bool $isDecorated |
||
18 | * @return string |
||
19 | */ |
||
20 | 2 | public function getDecoratedMessage($message, $type = null, $isDecorated = true) |
|
21 | { |
||
22 | 2 | if ($isDecorated && $type !== null) { |
|
23 | 1 | $message = '<' . $type . '>' . $message . '</' . $type . '>'; |
|
24 | 1 | } |
|
25 | |||
26 | 2 | return $message; |
|
27 | } |
||
28 | |||
29 | /** |
||
30 | * @param IOInterface $io |
||
31 | * @param string $question |
||
32 | * @param mixed $default |
||
33 | * @return string |
||
34 | */ |
||
35 | public function ask(IOInterface $io, $question, $default = null) |
||
36 | { |
||
37 | $question = $this->getDecoratedMessage($question, 'question', $io->isDecorated()) . ' '; |
||
38 | if ($default !== null) { |
||
39 | $question .= '(' . $this->getDecoratedMessage($default, 'comment', $io->isDecorated()) . ')'; |
||
40 | } |
||
41 | $question .= ': '; |
||
42 | |||
43 | return $io->ask($question, $default); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param IOInterface $io |
||
48 | * @param string $question |
||
49 | * @param bool $default |
||
50 | * @return bool |
||
51 | */ |
||
52 | public function askConfirmation(IOInterface $io, $question, $default = true) |
||
59 | |||
60 | /** |
||
61 | * Extract the client and project from a given path |
||
62 | * As everyone should use the same setup we can get the client en project from the path. |
||
63 | * |
||
64 | * @param string $path |
||
65 | * @return array |
||
66 | */ |
||
67 | 4 | public function extractInformationFromPath($path) |
|
88 | |||
89 | /** |
||
90 | * Update the Capfile with information provided in the config |
||
91 | * |
||
92 | * @param string $path |
||
93 | * @param array $config |
||
94 | */ |
||
95 | 1 | public function updateCapfile($path, array $config = null) |
|
107 | |||
108 | /** |
||
109 | * Replace a variable set in Ruby with the actual value |
||
110 | * |
||
111 | * @param string $variableName |
||
112 | * @param string $value |
||
113 | * @param string $content |
||
114 | * @return string |
||
115 | */ |
||
116 | 1 | protected function replaceSetRubyVar($variableName, $value, $content) |
|
124 | |||
125 | /** |
||
126 | * Update a YAML-file with information provided in the config |
||
127 | * |
||
128 | * @param string $path |
||
129 | * @param array $config |
||
130 | */ |
||
131 | 1 | public function updateYmlFile($path, array $config = null) |
|
149 | |||
150 | /** |
||
151 | * Replaces elements from passed arrays into the first array recursively but only when the key exists. |
||
152 | * |
||
153 | * @param array $content |
||
154 | * @param array $config |
||
155 | * @return array mixed |
||
156 | */ |
||
157 | 1 | public function arrayReplaceExisting(array $content, array $config = null) |
|
182 | } |
||
183 |
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.