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 RequestParser |
||
19 | { |
||
20 | /** |
||
21 | * @var Interceptor |
||
22 | */ |
||
23 | private $preParse; |
||
24 | |||
25 | /** |
||
26 | * RequestParser constructor. |
||
27 | */ |
||
28 | public function __construct() |
||
32 | |||
33 | /** |
||
34 | * Parse request data |
||
35 | * |
||
36 | * @param string $data |
||
37 | * |
||
38 | * @return InvokeSpec |
||
39 | */ |
||
40 | public function parse(string $data): InvokeSpec |
||
64 | |||
65 | /** |
||
66 | * Get pre-parse chain |
||
67 | * |
||
68 | * @return Interceptor |
||
69 | */ |
||
70 | public function onPreParse(): Interceptor |
||
74 | |||
75 | /** |
||
76 | * @param $record |
||
77 | * |
||
78 | * @return AbstractInvoke |
||
79 | */ |
||
80 | private function decodeCall($record): AbstractInvoke |
||
94 | |||
95 | /** |
||
96 | * @param array $payload |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | private function isSingleRequest(array $payload): bool |
||
104 | |||
105 | /** |
||
106 | * @param array $payload |
||
107 | * |
||
108 | * @return bool |
||
109 | */ |
||
110 | View Code Duplication | private function isValidCall($payload): bool |
|
128 | |||
129 | /** |
||
130 | * @param array $payload |
||
131 | * |
||
132 | * @return bool |
||
133 | */ |
||
134 | View Code Duplication | private function isValidNotification($payload): bool |
|
152 | |||
153 | /** |
||
154 | * @param mixed $record |
||
155 | * |
||
156 | * @return mixed |
||
157 | */ |
||
158 | private function preParse($record) |
||
168 | } |
||
169 |
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.