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 |
||
| 23 | class Builder extends Command |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Contains the default app structure. |
||
| 27 | * |
||
| 28 | * @var string[] |
||
| 29 | */ |
||
| 30 | protected $structure = [ |
||
| 31 | 'app' . DIRECTORY_SEPARATOR, |
||
| 32 | 'bootstrap' . DIRECTORY_SEPARATOR, |
||
| 33 | 'vendor' . DIRECTORY_SEPARATOR, |
||
| 34 | 'config' . DIRECTORY_SEPARATOR, |
||
| 35 | 'composer.json', |
||
| 36 | 'builder-stub', |
||
| 37 | '.env' |
||
| 38 | ]; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Holds the stub name. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $stub = 'builder-stub'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritdoc} |
||
| 49 | */ |
||
| 50 | protected $signature = 'app:build {name=application : The build name}'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritdoc} |
||
| 54 | */ |
||
| 55 | protected $description = 'Perform an application build'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Holds the configuration on is original state. |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected static $config; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * {@inheritdoc} |
||
| 66 | */ |
||
| 67 | 1 | public function handle(): void |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Builds the application into a single file. |
||
| 83 | * |
||
| 84 | * @param string $name The file name. |
||
| 85 | * |
||
| 86 | * @return $this |
||
| 87 | */ |
||
| 88 | 1 | protected function build(string $name): Builder |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $name |
||
| 109 | * |
||
| 110 | * @return $this |
||
| 111 | */ |
||
| 112 | 1 | protected function compile(string $name): Builder |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Gets a new instance of the compiler. |
||
| 144 | * |
||
| 145 | * @param string $name |
||
| 146 | * |
||
| 147 | * @return \Phar |
||
| 148 | */ |
||
| 149 | 1 | protected function getCompiler(string $name): \Phar |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @return $this |
||
| 164 | */ |
||
| 165 | 1 | protected function makeBuildsFolder(): Builder |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Sets the executable mode on the single application file. |
||
| 176 | * |
||
| 177 | * @param string $name |
||
| 178 | * |
||
| 179 | * @return $this |
||
| 180 | */ |
||
| 181 | 1 | protected function setPermissions($name): Builder |
|
| 187 | |||
| 188 | /** |
||
| 189 | * @return $this |
||
| 190 | */ |
||
| 191 | 1 | protected function prepare(): Builder |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | 1 | View Code Duplication | protected function clear(): Builder |
| 223 | |||
| 224 | /** |
||
| 225 | * Makes sure that the `clear` is performed even |
||
| 226 | * if the command fails. |
||
| 227 | * |
||
| 228 | * @return void |
||
| 229 | */ |
||
| 230 | 20 | View Code Duplication | public function __destruct() |
| 237 | } |
||
| 238 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: