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 |
||
9 | abstract class Base extends BaseTask implements CommandInterface |
||
10 | { |
||
11 | use \Robo\Common\ExecOneCommand; |
||
12 | |||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $command = ''; |
||
17 | |||
18 | /** |
||
19 | * @var bool |
||
20 | */ |
||
21 | protected $built = false; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $prefer; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $dev; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $ansi; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $nointeraction; |
||
42 | |||
43 | /** |
||
44 | * Action to use |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $action = ''; |
||
49 | |||
50 | /** |
||
51 | * @param null|string $pathToComposer |
||
52 | * |
||
53 | * @throws \Robo\Exception\TaskException |
||
54 | */ |
||
55 | View Code Duplication | public function __construct($pathToComposer = null) |
|
|
|||
56 | { |
||
57 | $this->command = $pathToComposer; |
||
58 | if (!$this->command) { |
||
59 | $this->command = $this->findExecutablePhar('composer'); |
||
60 | } |
||
61 | if (!$this->command) { |
||
62 | throw new TaskException(__CLASS__, "Neither local composer.phar nor global composer installation could be found."); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * adds `prefer-dist` option to composer |
||
68 | * |
||
69 | * @param bool $preferDist |
||
70 | * |
||
71 | * @return $this |
||
72 | */ |
||
73 | public function preferDist($preferDist = true) |
||
74 | { |
||
75 | if (!$preferDist) { |
||
76 | return $this->preferSource(); |
||
77 | } |
||
78 | $this->prefer = '--prefer-dist'; |
||
79 | return $this; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * adds `prefer-source` option to composer |
||
84 | * |
||
85 | * @return $this |
||
86 | */ |
||
87 | public function preferSource() |
||
88 | { |
||
89 | $this->prefer = '--prefer-source'; |
||
90 | return $this; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * adds `dev` option to composer |
||
95 | * |
||
96 | * @param bool $dev |
||
97 | * |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function dev($dev = true) |
||
108 | |||
109 | /** |
||
110 | * adds `no-dev` option to composer |
||
111 | * |
||
112 | * @return $this |
||
113 | */ |
||
114 | public function noDev() |
||
115 | { |
||
116 | $this->dev = '--no-dev'; |
||
117 | return $this; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * adds `ansi` option to composer |
||
122 | * |
||
123 | * @param bool $ansi |
||
124 | * |
||
125 | * @return $this |
||
126 | */ |
||
127 | public function ansi($ansi = true) |
||
128 | { |
||
129 | if (!$ansi) { |
||
130 | return $this->noAnsi(); |
||
131 | } |
||
132 | $this->ansi = '--ansi'; |
||
133 | return $this; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * adds `no-ansi` option to composer |
||
138 | * |
||
139 | * @return $this |
||
140 | */ |
||
141 | public function noAnsi() |
||
142 | { |
||
143 | $this->ansi = '--no-ansi'; |
||
144 | return $this; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param bool $interaction |
||
149 | * |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function interaction($interaction = true) |
||
159 | |||
160 | /** |
||
161 | * adds `no-interaction` option to composer |
||
162 | * |
||
163 | * @return $this |
||
164 | */ |
||
165 | public function noInteraction() |
||
170 | |||
171 | /** |
||
172 | * adds `optimize-autoloader` option to composer |
||
173 | * |
||
174 | * @param bool $optimize |
||
175 | * |
||
176 | * @return $this |
||
177 | */ |
||
178 | public function optimizeAutoloader($optimize = true) |
||
179 | { |
||
180 | if ($optimize) { |
||
181 | $this->option('--optimize-autoloader'); |
||
182 | } |
||
183 | return $this; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * adds `ignore-platform-reqs` option to composer |
||
188 | * |
||
189 | * @param bool $ignore |
||
190 | * |
||
191 | * @return $this |
||
192 | */ |
||
193 | public function ignorePlatformRequirements($ignore = true) |
||
198 | |||
199 | /** |
||
200 | * disable plugins |
||
201 | * |
||
202 | * @param bool $disable |
||
203 | * |
||
204 | * @return $this |
||
205 | */ |
||
206 | public function disablePlugins($disable = true) |
||
213 | |||
214 | /** |
||
215 | * skip scripts |
||
216 | * |
||
217 | * @param bool $disable |
||
218 | * |
||
219 | * @return $this |
||
220 | */ |
||
221 | public function noScripts($disable = true) |
||
228 | |||
229 | /** |
||
230 | * adds `--working-dir $dir` option to composer |
||
231 | * |
||
232 | * @param string $dir |
||
233 | * |
||
234 | * @return $this |
||
235 | */ |
||
236 | public function workingDir($dir) |
||
241 | |||
242 | /** |
||
243 | * Copy class fields into command options as directed. |
||
244 | */ |
||
245 | public function buildCommand() |
||
258 | |||
259 | /** |
||
260 | * {@inheritdoc} |
||
261 | */ |
||
262 | public function getCommand() |
||
263 | { |
||
264 | if (!$this->built) { |
||
265 | $this->buildCommand(); |
||
270 | } |
||
271 |
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.