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 |
||
| 32 | class ExampleCommands extends \Robo\Tasks |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * Watch a file. |
||
| 36 | * |
||
| 37 | * Demonstrates the 'watch' command. Runs 'composer update' any time |
||
| 38 | * composer.json changes. |
||
| 39 | */ |
||
| 40 | public function tryWatch() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Demonstrates Robo input APIs. |
||
| 49 | */ |
||
| 50 | public function tryInput() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Demonstrate Robo configuration. |
||
| 68 | * |
||
| 69 | * Config values are loaded from the followig locations: |
||
| 70 | * |
||
| 71 | * - [Robo Project]/robo.yml |
||
| 72 | * - $HOME/.robo/robo.yml |
||
| 73 | * - $CWD/robo.yml |
||
| 74 | * - Environment variables ROBO_CONFIG_KEY (e.g. ROBO_OPTIONS_PROGRESS_DELAY) |
||
| 75 | * - Overridden on the commandline via -Doptions.progress-delay=value |
||
| 76 | * |
||
| 77 | * @param string $key Name of the option to read (e.g. options.progress-delay) |
||
| 78 | * @option opt An option whose value is printed. Can be overridden in |
||
| 79 | * configuration via the configuration key command.try.config.options.opt. |
||
| 80 | * @option show-all Also print out the value of all configuration options |
||
| 81 | */ |
||
| 82 | public function tryConfig($key = 'options.progress-delay', $options = ['opt' => '0', 'show-all' => false]) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Demonstrates serial execution. |
||
| 96 | * |
||
| 97 | * @option $printed Print the output of each process. |
||
| 98 | * @option $error Include an extra process that fails. |
||
| 99 | */ |
||
| 100 | public function tryExec($options = ['printed' => true, 'error' => false]) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Demonstrates parallel execution. |
||
| 120 | * |
||
| 121 | * @option $printed Print the output of each process. |
||
| 122 | * @option $error Include an extra process that fails. |
||
| 123 | */ |
||
| 124 | public function tryPara($options = ['printed' => true, 'error' => false]) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * try:opt-required |
||
| 141 | */ |
||
| 142 | public function tryOptRequired($options = ['foo' => InputOption::VALUE_REQUIRED]) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Demonstrates Robo argument passing. |
||
| 149 | * |
||
| 150 | * @param string $a The first parameter. Required. |
||
| 151 | * @param string $b The second parameter. Optional. |
||
| 152 | */ |
||
| 153 | public function tryArgs($a, $b = 'default') |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Demonstrate Robo variable argument passing. |
||
| 160 | * |
||
| 161 | * @param array $a A list of commandline parameters. |
||
| 162 | * @param array $options |
||
| 163 | */ |
||
| 164 | public function tryArrayArgs(array $a, array $options = ['foo' => []]) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Demonstrate use of Symfony $input object in Robo in place of |
||
| 174 | * the usual "parameter arguments". |
||
| 175 | * |
||
| 176 | * @arg array $a A list of commandline parameters. |
||
| 177 | * @option foo |
||
| 178 | * @default a [] |
||
| 179 | * @default foo [] |
||
| 180 | */ |
||
| 181 | View Code Duplication | public function trySymfony(InputInterface $input) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Demonstrate Robo boolean options. |
||
| 193 | * |
||
| 194 | * @param array $opts The options. |
||
| 195 | * @option boolean $silent Supress output. |
||
| 196 | */ |
||
| 197 | public function tryOptbool($opts = ['silent|s' => false]) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Demonstrate the use of the PHP built-in webserver. |
||
| 206 | */ |
||
| 207 | public function tryServer() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Demonstrate the use of the Robo open-browser task. |
||
| 217 | */ |
||
| 218 | public function tryOpenBrowser() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Demonstrate Robo error output and command failure. |
||
| 228 | */ |
||
| 229 | public function tryError() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Demonstrate Robo standard output and command success. |
||
| 236 | */ |
||
| 237 | public function trySuccess() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @field-labels |
||
| 244 | * name: Name |
||
| 245 | * species: Species |
||
| 246 | * legs: Legs |
||
| 247 | * food: Favorite Food |
||
| 248 | * id: Id |
||
| 249 | * @return PropertyList |
||
| 250 | */ |
||
| 251 | public function tryInfo() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Demonstrate Robo formatters. Default format is 'table'. |
||
| 282 | * |
||
| 283 | * @field-labels |
||
| 284 | * first: I |
||
| 285 | * second: II |
||
| 286 | * third: III |
||
| 287 | * @default-string-field second |
||
| 288 | * @usage try:formatters --format=yaml |
||
| 289 | * @usage try:formatters --format=csv |
||
| 290 | * @usage try:formatters --fields=first,third |
||
| 291 | * @usage try:formatters --fields=III,II |
||
| 292 | * @aliases tf |
||
| 293 | * |
||
| 294 | * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields |
||
| 295 | */ |
||
| 296 | public function tryFormatters($somthing = 'default', $options = ['format' => 'table', 'fields' => '']) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Try word wrapping |
||
| 309 | * |
||
| 310 | * @field-labels |
||
| 311 | * first: First |
||
| 312 | * second: Second |
||
| 313 | * |
||
| 314 | * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields |
||
| 315 | */ |
||
| 316 | public function tryWrap() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Demonstrate an alter hook with an option |
||
| 329 | * |
||
| 330 | * @hook alter try:formatters |
||
| 331 | * @option $french Add a row with French numbers. |
||
| 332 | * @usage try:formatters --french |
||
| 333 | */ |
||
| 334 | public function alterFormatters($result, CommandData $commandData) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Demonstrate what happens when a command or a task |
||
| 345 | * throws an exception. Note that typically, Robo commands |
||
| 346 | * should return Result objects rather than throw exceptions. |
||
| 347 | */ |
||
| 348 | public function tryException($options = ['task' => false]) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Demonstrate deprecated task behavior. |
||
| 358 | * |
||
| 359 | * Demonstrate what happens when using a task that is created via |
||
| 360 | * direct instantiation, which omits initialization done by the |
||
| 361 | * container. Emits a warning message. |
||
| 362 | */ |
||
| 363 | public function tryDeprecated() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Demonstrate the use of a collection builder to chain multiple tasks |
||
| 373 | * together into a collection, which is executed once constructed. |
||
| 374 | * |
||
| 375 | * For demonstration purposes only; this could, of course, be done |
||
| 376 | * with a single FilesystemStack. |
||
| 377 | */ |
||
| 378 | public function tryBuilder() |
||
| 392 | |||
| 393 | public function tryState() |
||
| 403 | |||
| 404 | public function tryBuilderRollback() |
||
| 431 | |||
| 432 | public function tryWorkdir() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Demonstrates Robo temporary directory usage. |
||
| 462 | */ |
||
| 463 | public function tryTmpDir() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Description |
||
| 488 | * @param $options |
||
| 489 | * @option delay Miliseconds delay |
||
| 490 | * @return type |
||
| 491 | */ |
||
| 492 | public function tryProgress($options = ['delay' => 500]) |
||
| 512 | |||
| 513 | public function tryIter() |
||
| 533 | } |
||
| 534 | |||
| 549 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: