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 |
||
9 | View Code Duplication | class KeyStats extends BaseRequest |
|
|
|||
10 | { |
||
11 | const ENDPOINT = 'stock/{symbol}/stats'; |
||
12 | |||
13 | /** |
||
14 | * IEX Cloud Documentation provides for the optional field to be added to |
||
15 | * the end of the endpoint uri in order to retrieve a specific field. |
||
16 | * This property allows that functionality to be used in this SDK. |
||
17 | */ |
||
18 | public $field; |
||
19 | |||
20 | /** |
||
21 | * Create constructor. |
||
22 | * |
||
23 | * @param IEXCloud $api |
||
24 | */ |
||
25 | 4 | public function __construct(IEXCloud $api) |
|
29 | |||
30 | /** |
||
31 | * If the field property is set, add it to the end of the endpoint string. |
||
32 | * |
||
33 | * @return string |
||
34 | */ |
||
35 | 2 | protected function getFullEndpoint(): string |
|
41 | |||
42 | /** |
||
43 | * @return bool|void |
||
44 | * @throws WrongData |
||
45 | */ |
||
46 | 3 | protected function validateParams(): void |
|
52 | |||
53 | /** |
||
54 | * Setter for field property |
||
55 | * |
||
56 | * @param string $field |
||
57 | * |
||
58 | * @return KeyStats |
||
59 | */ |
||
60 | 1 | public function only(string $field): self |
|
66 | } |
||
67 |
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.