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 |
||
| 24 | class Builder extends Command |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * {@inheritdoc} |
||
| 28 | */ |
||
| 29 | protected $signature = 'app:build {name=application : The build name} {--with-dev : Whether the dev dependencies should be included}'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * {@inheritdoc} |
||
| 33 | */ |
||
| 34 | protected $description = 'Perform an application build'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Contains the default app structure. |
||
| 38 | * |
||
| 39 | * @var string[] |
||
| 40 | */ |
||
| 41 | protected $structure = [ |
||
| 42 | 'app'.DIRECTORY_SEPARATOR, |
||
| 43 | 'bootstrap'.DIRECTORY_SEPARATOR, |
||
| 44 | 'vendor'.DIRECTORY_SEPARATOR, |
||
| 45 | 'config'.DIRECTORY_SEPARATOR, |
||
| 46 | 'composer.json', |
||
| 47 | 'builder-stub', |
||
| 48 | '.env', |
||
| 49 | ]; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Holds the stub name. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $stub = 'builder-stub'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Holds the configuration on is original state. |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected static $config; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | 1 | public function handle(): void |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Builds the application into a single file. |
||
| 84 | * |
||
| 85 | * @param string $name The file name. |
||
| 86 | * |
||
| 87 | * @return $this |
||
| 88 | */ |
||
| 89 | 1 | protected function build(string $name): Builder |
|
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $name |
||
| 110 | * |
||
| 111 | * @return $this |
||
| 112 | */ |
||
| 113 | 1 | protected function compile(string $name): Builder |
|
| 114 | { |
||
| 115 | 1 | $compiler = $this->makeBuildsFolder() |
|
| 116 | 1 | ->getCompiler($name); |
|
| 117 | |||
| 118 | 1 | $structure = config('app.structure') ?: $this->structure; |
|
| 119 | |||
| 120 | 1 | $regex = '#'.implode('|', $structure).'#'; |
|
| 121 | |||
| 122 | 1 | if (stristr(PHP_OS, 'WINNT') !== false) { // For windows: |
|
| 123 | $compiler->buildFromDirectory($this->app->basePath(), str_replace('\\', '/', $regex)); |
||
| 124 | } else { // Linux, OS X: |
||
| 125 | 1 | $compiler->buildFromDirectory($this->app->basePath(), $regex); |
|
| 126 | } |
||
| 127 | |||
| 128 | $this->task('Compiling code', function () use ($compiler) { |
||
| 129 | 1 | $compiler->setStub( |
|
| 130 | 1 | "#!/usr/bin/env php \n".$compiler->createDefaultStub( |
|
| 131 | 1 | $this->stub |
|
| 132 | ) |
||
| 133 | ); |
||
| 134 | 1 | }); |
|
| 135 | |||
| 136 | // Unset the compiler to fix a "file in use" error on Windows. |
||
| 137 | 1 | unset($compiler); |
|
| 138 | |||
| 139 | 1 | $file = $this->app->buildsPath($name); |
|
| 140 | |||
| 141 | 1 | File::move("$file.phar", $file); |
|
| 142 | |||
| 143 | 1 | return $this; |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Gets a new instance of the compiler. |
||
| 148 | * |
||
| 149 | * @param string $name |
||
| 150 | * |
||
| 151 | * @return \Phar |
||
| 152 | */ |
||
| 153 | 1 | protected function getCompiler(string $name): \Phar |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @return $this |
||
| 168 | */ |
||
| 169 | 1 | protected function makeBuildsFolder(): Builder |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Sets the executable mode on the single application file. |
||
| 180 | * |
||
| 181 | * @param string $name |
||
| 182 | * |
||
| 183 | * @return $this |
||
| 184 | */ |
||
| 185 | 1 | protected function setPermissions($name): Builder |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @return $this |
||
| 194 | */ |
||
| 195 | 1 | protected function prepare(): Builder |
|
| 196 | { |
||
| 197 | 1 | $file = $this->app->configPath('app.php'); |
|
| 198 | 1 | static::$config = File::get($file); |
|
| 199 | 1 | $config = include $file; |
|
| 200 | |||
| 201 | 1 | $config['production'] = true; |
|
| 202 | |||
| 203 | $this->task('Moving application to production mode', function () use ($file, $config) { |
||
| 204 | 1 | File::put($file, '<?php return '.var_export($config, true).';'.PHP_EOL); |
|
| 205 | 1 | }); |
|
| 206 | |||
| 207 | 1 | View Code Duplication | if ($this->option('with-dev') === false) { |
| 208 | $this->task('Temporarily removing dev dependencies', function () { |
||
| 209 | 1 | $this->app[Composer::class]->install(['--no-dev']); |
|
| 210 | 1 | }); |
|
| 211 | } |
||
| 212 | |||
| 213 | 1 | $stub = str_replace('#!/usr/bin/env php', '', File::get($this->app->basePath(ARTISAN_BINARY))); |
|
| 214 | |||
| 215 | // Remove first line. |
||
| 216 | 1 | $stubAsArray = explode("\n", $stub); |
|
| 217 | 1 | array_shift($stubAsArray); |
|
| 218 | 1 | $stub = implode("\n", $stubAsArray); |
|
| 219 | |||
| 220 | 1 | File::put($this->app->basePath($this->stub), $stub); |
|
| 221 | |||
| 222 | 1 | return $this; |
|
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @return $this |
||
| 227 | */ |
||
| 228 | 1 | protected function clear(): Builder |
|
| 229 | { |
||
| 230 | 1 | File::put($this->app->configPath('app.php'), static::$config); |
|
| 231 | |||
| 232 | 1 | File::delete($this->app->basePath($this->stub)); |
|
| 233 | |||
| 234 | 1 | View Code Duplication | if ($this->option('with-dev') === false) { |
| 235 | $this->task('Reinstalling dev dependencies', function () { |
||
| 236 | 1 | $this->app[Composer::class]->install(); |
|
| 237 | 1 | }); |
|
| 238 | } |
||
| 239 | |||
| 240 | 1 | static::$config = null; |
|
| 241 | |||
| 242 | 1 | return $this; |
|
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Makes sure that the `clear` is performed even |
||
| 247 | * if the command fails. |
||
| 248 | * |
||
| 249 | * @return void |
||
| 250 | */ |
||
| 251 | 18 | public function __destruct() |
|
| 258 | } |
||
| 259 |
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: