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 |
||
18 | class Client |
||
19 | { |
||
20 | const DEFAULT_OPTIONS = [ |
||
21 | 'schema' => 'https', |
||
22 | 'path' => '/', |
||
23 | 'user_agent' => 'WyriHaximus/php-api-client', |
||
24 | 'headers' => [], |
||
25 | ]; |
||
26 | |||
27 | /** |
||
28 | * @var GuzzleClient |
||
29 | */ |
||
30 | protected $handler; |
||
31 | |||
32 | /** |
||
33 | * @var LoopInterface |
||
34 | */ |
||
35 | protected $loop; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $options = []; |
||
41 | |||
42 | /** |
||
43 | * @var Hydrator |
||
44 | */ |
||
45 | protected $hydrator; |
||
46 | |||
47 | /** |
||
48 | * @var CacheInterface |
||
49 | */ |
||
50 | protected $cache; |
||
51 | |||
52 | 9 | public function __construct(LoopInterface $loop, GuzzleClient $handler, array $options = []) |
|
62 | |||
63 | 5 | public function request(string $path, bool $refresh = false): PromiseInterface |
|
73 | |||
74 | 3 | View Code Duplication | protected function checkCache(string $path) |
84 | |||
85 | 4 | protected function sendRequest(string $path, string $method = 'GET'): PromiseInterface |
|
104 | |||
105 | 4 | protected function createRequest(string $method, string $path) |
|
114 | |||
115 | protected function jsonDecode(string $json): PromiseInterface |
||
121 | |||
122 | 1 | public function getHydrator(): Hydrator |
|
126 | |||
127 | 3 | public function getLoop(): LoopInterface |
|
131 | } |
||
132 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.