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 | * The component name. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $name; |
||
18 | |||
19 | /** |
||
20 | * The version of component being installed. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $version; |
||
25 | |||
26 | /** |
||
27 | * The component repository instance. |
||
28 | * |
||
29 | * @var \Consigliere\Components\Repository |
||
30 | */ |
||
31 | protected $repository; |
||
32 | |||
33 | /** |
||
34 | * The console command instance. |
||
35 | * |
||
36 | * @var \Illuminate\Console\Command |
||
37 | */ |
||
38 | protected $console; |
||
39 | |||
40 | /** |
||
41 | * The destionation path. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $path; |
||
46 | |||
47 | /** |
||
48 | * The process timeout. |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | protected $timeout = 3360; |
||
53 | |||
54 | /** |
||
55 | * The constructor. |
||
56 | * |
||
57 | * @param string $name |
||
58 | * @param string $version |
||
59 | * @param string $type |
||
60 | * @param bool $tree |
||
61 | */ |
||
62 | public function __construct($name, $version = null, $type = null, $tree = false) |
||
69 | |||
70 | /** |
||
71 | * Set destination path. |
||
72 | * |
||
73 | * @param string $path |
||
74 | * |
||
75 | * @return $this |
||
76 | */ |
||
77 | public function setPath($path) |
||
83 | |||
84 | /** |
||
85 | * Set the component repository instance. |
||
86 | * |
||
87 | * @param \Consigliere\Components\Repository $repository |
||
88 | * |
||
89 | * @return $this |
||
90 | */ |
||
91 | public function setRepository(Repository $repository) |
||
97 | |||
98 | /** |
||
99 | * Set console command instance. |
||
100 | * |
||
101 | * @param \Consigliere\Components\Process\Command $console |
||
102 | * |
||
103 | * @return $this |
||
104 | */ |
||
105 | public function setConsole(Command $console) |
||
111 | |||
112 | /** |
||
113 | * Set process timeout. |
||
114 | * |
||
115 | * @param int $timeout |
||
116 | * |
||
117 | * @return $this |
||
118 | */ |
||
119 | public function setTimeout($timeout) |
||
125 | |||
126 | /** |
||
127 | * Run the installation process. |
||
128 | * |
||
129 | * @return \Symfony\Component\Process\Process |
||
130 | */ |
||
131 | public function run() |
||
145 | |||
146 | /** |
||
147 | * Get process instance. |
||
148 | * |
||
149 | * @return \Symfony\Component\Process\Process |
||
150 | */ |
||
151 | public function getProcess() |
||
171 | |||
172 | /** |
||
173 | * Get destination path. |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | public function getDestinationPath() |
||
185 | |||
186 | /** |
||
187 | * Get git repo url. |
||
188 | * |
||
189 | * @return string|null |
||
190 | */ |
||
191 | public function getRepoUrl() |
||
211 | |||
212 | /** |
||
213 | * Get branch name. |
||
214 | * |
||
215 | * @return string |
||
216 | */ |
||
217 | public function getBranch() |
||
221 | |||
222 | /** |
||
223 | * Get component name. |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | public function getComponentName() |
||
233 | |||
234 | /** |
||
235 | * Get composer package name. |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | public function getPackageName() |
||
247 | |||
248 | /** |
||
249 | * Install the component via git. |
||
250 | * |
||
251 | * @return \Symfony\Component\Process\Process |
||
252 | */ |
||
253 | View Code Duplication | public function installViaGit() |
|
264 | |||
265 | /** |
||
266 | * Install the component via git subtree. |
||
267 | * |
||
268 | * @return \Symfony\Component\Process\Process |
||
269 | */ |
||
270 | View Code Duplication | public function installViaSubtree() |
|
282 | |||
283 | /** |
||
284 | * Install the component via composer. |
||
285 | * |
||
286 | * @return \Symfony\Component\Process\Process |
||
287 | */ |
||
288 | public function installViaComposer() |
||
296 | } |
||
297 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: