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 |
||
| 36 | class RoboFile extends \Robo\Tasks |
||
| 37 | { |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The build properties. |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $properties = array( |
||
| 45 | 'base.dir' => __DIR__, |
||
| 46 | 'src.dir' => __DIR__ . '/src', |
||
| 47 | 'dist.dir' => __DIR__ . '/dist', |
||
| 48 | 'vendor.dir' => __DIR__ . '/vendor', |
||
| 49 | 'target.dir' => __DIR__ . '/target' |
||
| 50 | ); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Run's the composer install command. |
||
| 54 | * |
||
| 55 | * @return \Robo\Result The result |
||
| 56 | */ |
||
| 57 | public function composerInstall() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Run's the composer update command. |
||
| 68 | * |
||
| 69 | * @return \Robo\Result The result |
||
| 70 | */ |
||
| 71 | public function composerUpdate() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Clean up the environment for a new build. |
||
| 82 | * |
||
| 83 | * @return \Robo\Result The result |
||
| 84 | */ |
||
| 85 | public function clean() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Prepare's the environment for a new build. |
||
| 92 | * |
||
| 93 | * @return \Robo\Result The result |
||
| 94 | */ |
||
| 95 | public function prepare() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Run's the PHPMD. |
||
| 106 | * |
||
| 107 | * @return \Robo\Result The result |
||
| 108 | */ |
||
| 109 | View Code Duplication | public function runMd() |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Run's the PHPCPD. |
||
| 125 | * |
||
| 126 | * @return \Robo\Result The result |
||
| 127 | */ |
||
| 128 | public function runCpd() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Run's the PHPCodeSniffer. |
||
| 151 | * |
||
| 152 | * @return \Robo\Result The result |
||
| 153 | */ |
||
| 154 | View Code Duplication | public function runCs() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Run's the PHPUnit tests. |
||
| 170 | * |
||
| 171 | * @return \Robo\Result The result |
||
| 172 | */ |
||
| 173 | public function runTests() |
||
| 174 | { |
||
| 175 | |||
| 176 | // run PHPUnit |
||
| 177 | return $this->taskPHPUnit(sprintf('%s/bin/phpunit', $this->properties['vendor.dir'])) |
||
| 178 | ->configFile('phpunit.xml') |
||
| 179 | ->run(); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * The complete build process. |
||
| 184 | * |
||
| 185 | * @return void |
||
| 186 | */ |
||
| 187 | public function build() |
||
| 201 | } |
||
| 202 |
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: