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 | class USHolTradeDates extends BaseRequest |
||
10 | { |
||
11 | const ENDPOINT = 'ref-data/us/dates/{type}/{direction}/{last}/{startDate}'; |
||
12 | |||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $direction = 'next'; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $type = 'trade'; |
||
22 | |||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | protected $last = 1; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $startDate = ''; |
||
32 | |||
33 | /** |
||
34 | * Create constructor. |
||
35 | * |
||
36 | * @param IEXCloud $api |
||
37 | */ |
||
38 | 4 | public function __construct(IEXCloud $api) |
|
42 | |||
43 | /** |
||
44 | * @param string $type |
||
45 | * |
||
46 | * @return USHolTradeDates |
||
47 | */ |
||
48 | 1 | public function setType(string $type): self |
|
56 | |||
57 | /** |
||
58 | * @param string $direction |
||
59 | * |
||
60 | * @return USHolTradeDates |
||
61 | */ |
||
62 | 1 | public function setDirection(string $direction): self |
|
70 | |||
71 | /** |
||
72 | * @param string $last |
||
73 | * |
||
74 | * @return USHolTradeDates |
||
75 | */ |
||
76 | public function setLast(string $last): self |
||
82 | |||
83 | /** |
||
84 | * @param string $startDate |
||
85 | * |
||
86 | * @return USHolTradeDates |
||
87 | */ |
||
88 | 1 | public function setStartDate(string $startDate): self |
|
100 | |||
101 | /** |
||
102 | * @return string |
||
103 | */ |
||
104 | protected function getFullEndpoint(): string |
||
108 | |||
109 | /** |
||
110 | * @param string $input |
||
111 | * @param string $pattern |
||
112 | * @param string $errorMessage |
||
113 | */ |
||
114 | 3 | View Code Duplication | private function validateInput(string $input, string $pattern, string $errorMessage): void |
122 | } |
||
123 |
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.