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 | View Code Duplication | public function runCpd() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Run's the PHPCodeSniffer. |
||
| 144 | * |
||
| 145 | * @return \Robo\Result The result |
||
| 146 | */ |
||
| 147 | View Code Duplication | public function runCs() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Run's the PHPUnit tests. |
||
| 163 | * |
||
| 164 | * @return \Robo\Result The result |
||
| 165 | */ |
||
| 166 | public function runTests() |
||
| 167 | { |
||
| 168 | |||
| 169 | // run PHPUnit |
||
| 170 | return $this->taskPHPUnit(sprintf('%s/bin/phpunit', $this->properties['vendor.dir'])) |
||
| 171 | ->configFile('phpunit.xml') |
||
| 172 | ->run(); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * The complete build process. |
||
| 177 | * |
||
| 178 | * @return void |
||
| 179 | */ |
||
| 180 | public function build() |
||
| 194 | } |
||
| 195 |
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: