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 Client |
||
| 24 | { |
||
| 25 | const DEFAULT_OPTIONS = [ |
||
| 26 | Options::SCHEMA => 'https', |
||
| 27 | Options::PATH => '/', |
||
| 28 | Options::USER_AGENT => 'WyriHaximus/php-api-client', |
||
| 29 | Options::HEADERS => [], |
||
| 30 | ]; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var GuzzleClient |
||
| 34 | */ |
||
| 35 | protected $handler; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var LoopInterface |
||
| 39 | */ |
||
| 40 | protected $loop; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $options = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var Hydrator |
||
| 49 | */ |
||
| 50 | protected $hydrator; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var CacheInterface |
||
| 54 | */ |
||
| 55 | protected $cache; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param LoopInterface $loop |
||
| 59 | * @param GuzzleClient $handler |
||
| 60 | * @param array $options |
||
| 61 | */ |
||
| 62 | 12 | public function __construct(LoopInterface $loop, GuzzleClient $handler, array $options = []) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @return Hydrator |
||
| 75 | */ |
||
| 76 | 12 | protected function determineHydrator(): Hydrator |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param string $path |
||
| 87 | * @param bool $refresh |
||
| 88 | * @return PromiseInterface |
||
| 89 | */ |
||
| 90 | 5 | public function request(string $path, bool $refresh = false): PromiseInterface |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $path |
||
| 99 | * @param bool $refresh |
||
| 100 | * @return PromiseInterface |
||
| 101 | */ |
||
| 102 | 5 | public function requestRaw(string $path, bool $refresh = false): PromiseInterface |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @param UriInterface $uri |
||
| 114 | * @return PromiseInterface |
||
| 115 | */ |
||
| 116 | 3 | protected function checkCache(UriInterface $uri): PromiseInterface |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param RequestInterface $request |
||
| 139 | * @param ResponseInterface $response |
||
| 140 | */ |
||
| 141 | 2 | protected function storeCache(RequestInterface $request, ResponseInterface $response) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @param UriInterface $uri |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | 3 | protected function determineCacheKey(UriInterface $uri): string |
|
| 179 | |||
| 180 | /** |
||
| 181 | * @param string $string |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | 3 | protected function stripExtraSlashes(string $string): string |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @param RequestInterface $request |
||
| 191 | * @param bool $refresh |
||
| 192 | * @return PromiseInterface |
||
| 193 | */ |
||
| 194 | 5 | public function requestPsr7(RequestInterface $request, bool $refresh = false): PromiseInterface |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $path |
||
| 238 | * @return RequestInterface |
||
| 239 | */ |
||
| 240 | 5 | protected function createRequest(string $path): RequestInterface |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | 5 | public function getHeaders(): array |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @param string $json |
||
| 261 | * @return PromiseInterface |
||
| 262 | */ |
||
| 263 | public function jsonDecode(string $json): PromiseInterface |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @return Hydrator |
||
| 272 | */ |
||
| 273 | 1 | public function getHydrator(): Hydrator |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @return LoopInterface |
||
| 280 | */ |
||
| 281 | 3 | public function getLoop(): LoopInterface |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | 8 | public function getBaseURL(): string |
|
| 293 | } |
||
| 294 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: