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 |
||
38 | class RoboFile extends AbstractRoboFile |
||
|
|||
39 | { |
||
40 | |||
41 | /** |
||
42 | * Configuration key for the directories. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | const DIRS = 'dirs'; |
||
47 | |||
48 | /** |
||
49 | * Configuration key for the source directory. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | const SRC = 'src'; |
||
54 | |||
55 | /** |
||
56 | * Configuration key for the destination directory. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | const DEST = 'dest'; |
||
61 | |||
62 | /** |
||
63 | * Configuration key for the deploy directory. |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | const DEPLOY = 'deploy'; |
||
68 | |||
69 | /** |
||
70 | * Configuration key for the docker configuration. |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | const DOCKER = 'docker'; |
||
75 | |||
76 | /** |
||
77 | * Configuration key for the docker target container name. |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | const TARGET_CONTAINER = 'target-container'; |
||
82 | |||
83 | /** |
||
84 | * Returns the deploy directory. |
||
85 | * |
||
86 | * @return string The directory to deploy the sources to |
||
87 | */ |
||
88 | protected function getDeployDir() |
||
92 | |||
93 | /** |
||
94 | * Returns the name of the docker target container. |
||
95 | * |
||
96 | * @return string The docker target container |
||
97 | */ |
||
98 | protected function getTargetContainer() |
||
102 | |||
103 | /** |
||
104 | * Returns the Magento 2 root directory inside the docker container. |
||
105 | * |
||
106 | * @return string The Magento 2 root directory |
||
107 | */ |
||
108 | protected function getDockerMagentoRootDir() |
||
112 | |||
113 | /** |
||
114 | * Returns the synchronization source directory inside the docker container. |
||
115 | * |
||
116 | * @return string The synchronization source directory |
||
117 | */ |
||
118 | protected function getDockerSyncSrcDir() |
||
122 | |||
123 | /** |
||
124 | * Returns the synchronization destination directory inside the docker container. |
||
125 | * |
||
126 | * @return string The synchronization destination directory |
||
127 | */ |
||
128 | protected function getDockerSyncDestDir() |
||
132 | |||
133 | /** |
||
134 | * Run's the composer install command. |
||
135 | * |
||
136 | * @return void |
||
137 | */ |
||
138 | public function composerInstall() |
||
146 | |||
147 | /** |
||
148 | * Run's the composer update command. |
||
149 | * |
||
150 | * @return void |
||
151 | */ |
||
152 | public function composerUpdate() |
||
160 | |||
161 | /** |
||
162 | * Clean up the environment for a new build. |
||
163 | * |
||
164 | * @return void |
||
165 | */ |
||
166 | public function clean() |
||
170 | |||
171 | /** |
||
172 | * Prepare's the environment for a new build. |
||
173 | * |
||
174 | * @return void |
||
175 | */ |
||
176 | public function prepare() |
||
183 | |||
184 | /** |
||
185 | * Run's the PHPMD. |
||
186 | * |
||
187 | * @return void |
||
188 | */ |
||
189 | public function runMd() |
||
202 | |||
203 | /** |
||
204 | * Run's the PHPCPD. |
||
205 | * |
||
206 | * @return void |
||
207 | */ |
||
208 | public function runCpd() |
||
221 | |||
222 | /** |
||
223 | * Run's the PHPCodeSniffer. |
||
224 | * |
||
225 | * @return void |
||
226 | */ |
||
227 | public function runCs() |
||
240 | |||
241 | /** |
||
242 | * Run's the PHPUnit tests. |
||
243 | * |
||
244 | * @return void |
||
245 | */ |
||
246 | public function runTests() |
||
247 | { |
||
248 | |||
249 | // run PHPUnit |
||
250 | $this->taskPHPUnit(sprintf('%s/bin/phpunit', $this->getVendorDir())) |
||
251 | ->configFile('phpunit.xml') |
||
252 | ->run(); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Deploy's the extension to it's target directory. |
||
257 | * |
||
258 | * @return void |
||
259 | */ |
||
260 | public function deploy() |
||
264 | |||
265 | /** |
||
266 | * Deploy's the extension to it's target directory in the specified docker container. |
||
267 | * |
||
268 | * @return void |
||
269 | */ |
||
270 | public function dockerDeploy() |
||
280 | |||
281 | /** |
||
282 | * Start's the synchronization between the local sources and the Magento 2 instance |
||
283 | * inside the container. |
||
284 | * |
||
285 | * @return void |
||
286 | */ |
||
287 | public function dockerSync() |
||
309 | |||
310 | /** |
||
311 | * Invokes the Magento 2 setup:upgrade command inside the docker container. |
||
312 | * |
||
313 | * @params array $args The arguments to pass to the bin/magento script inside the docker container |
||
314 | * |
||
315 | * @return void |
||
316 | */ |
||
317 | View Code Duplication | public function dockerMagento(array $args) |
|
334 | |||
335 | /** |
||
336 | * Invokes the passed Composer command inside the Magento root directory of the docker container. |
||
337 | * |
||
338 | * @params array $args The arguments to pass to the composer script inside the docker container |
||
339 | * |
||
340 | * @return void |
||
341 | */ |
||
342 | View Code Duplication | public function dockerComposer(array $args) |
|
359 | |||
360 | /** |
||
361 | * The complete build process. |
||
362 | * |
||
363 | * @return void |
||
364 | */ |
||
365 | public function build() |
||
374 | } |
||
375 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.