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 declare(strict_types=1); |
||
| 29 | trait MALTrait { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The request builder for the MAL API |
||
| 33 | * @var MALRequestBuilder |
||
| 34 | */ |
||
| 35 | protected $requestBuilder; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The base url for api requests |
||
| 39 | * @var string $base_url |
||
| 40 | */ |
||
| 41 | protected $baseUrl = M::BASE_URL; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * HTTP headers to send with every request |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $defaultHeaders = [ |
||
| 49 | 'Accept' => 'text/xml', |
||
| 50 | 'Accept-Encoding' => 'gzip', |
||
| 51 | 'Content-type' => 'application/x-www-form-urlencoded', |
||
| 52 | 'User-Agent' => "Tim's Anime Client/4.0" |
||
| 53 | ]; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Set the request builder object |
||
| 57 | * |
||
| 58 | * @param MALRequestBuilder $requestBuilder |
||
| 59 | * @return self |
||
| 60 | */ |
||
| 61 | public function setRequestBuilder($requestBuilder): self |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Unencode the dual-encoded ampersands in the body |
||
| 69 | * |
||
| 70 | * This is a dirty hack until I can fully track down where |
||
| 71 | * the dual-encoding happens |
||
| 72 | * |
||
| 73 | * @param FormBody $formBody The form builder object to fix |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | private function fixBody(FormBody $formBody): string |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Create a request object |
||
| 84 | * |
||
| 85 | * @param string $type |
||
| 86 | * @param string $url |
||
| 87 | * @param array $options |
||
| 88 | * @return \Amp\Promise |
||
| 89 | */ |
||
| 90 | public function setUpRequest(string $type, string $url, array $options = []) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Make a request |
||
| 137 | * |
||
| 138 | * @param string $type |
||
| 139 | * @param string $url |
||
| 140 | * @param array $options |
||
| 141 | * @return \Amp\Artax\Response |
||
| 142 | */ |
||
| 143 | private function getResponse(string $type, string $url, array $options = []) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Make a request |
||
| 170 | * |
||
| 171 | * @param string $type |
||
| 172 | * @param string $url |
||
| 173 | * @param array $options |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | View Code Duplication | private function request(string $type, string $url, array $options = []): array |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Remove some boilerplate for get requests |
||
| 199 | * |
||
| 200 | * @param array $args |
||
| 201 | * @return array |
||
| 202 | */ |
||
| 203 | protected function getRequest(...$args): array |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Remove some boilerplate for post requests |
||
| 210 | * |
||
| 211 | * @param array $args |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | View Code Duplication | protected function postRequest(...$args): array |
|
| 235 | } |
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: