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 |
||
8 | trait ResponseRecordStorage |
||
9 | { |
||
10 | /** |
||
11 | * @var ResponseRecord[] |
||
12 | */ |
||
13 | protected $reps = []; |
||
14 | |||
15 | /** |
||
16 | * @var ResponseRecord[] |
||
17 | */ |
||
18 | protected $handledReps = []; |
||
19 | |||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | protected $handledRepsTimeout = 0; |
||
24 | |||
25 | /** |
||
26 | * Create ResponseRecord. |
||
27 | * |
||
28 | * @param string $pid |
||
29 | * @param string $alias |
||
30 | * @param float $timeout |
||
31 | * @param float $timeoutIncrease |
||
32 | * @return ResponseRecord |
||
33 | */ |
||
34 | 1 | protected function createResponse($pid, $alias, $timeout = 0.0, $timeoutIncrease = 1.0) |
|
38 | |||
39 | /** |
||
40 | * Check if ResponseRecord with given protocol ID exists. |
||
41 | * |
||
42 | * @param $pid |
||
43 | * @return bool |
||
44 | */ |
||
45 | 3 | protected function existsResponse($pid) |
|
49 | |||
50 | /** |
||
51 | * Add new ResponseRecord to storage. |
||
52 | * |
||
53 | * @param string $pid |
||
54 | * @param ResponseRecord $response |
||
55 | */ |
||
56 | 5 | protected function addResponse($pid, ResponseRecord $response) |
|
60 | |||
61 | /** |
||
62 | * Return ResponseRecord if it exists or null if it does not exist. |
||
63 | * |
||
64 | * @param string $pid |
||
65 | * @return ResponseRecord |
||
66 | */ |
||
67 | 1 | protected function getResponse($pid) |
|
71 | |||
72 | /** |
||
73 | * Mark ResponseRecord as handled if it exists that and has protocol ID equal to $pid. |
||
74 | * |
||
75 | * @param string $pid |
||
76 | * @param $exception |
||
77 | */ |
||
78 | 2 | protected function resolveOrRejectResponse($pid, $exception) |
|
86 | |||
87 | /** |
||
88 | * Return all unhandled ResponseRecords in array form. |
||
89 | * |
||
90 | * @return ResponseRecord[] |
||
91 | */ |
||
92 | 1 | View Code Duplication | protected function unfinishedResponses() |
108 | |||
109 | /** |
||
110 | * Expire unhandled ResponseRecords. |
||
111 | */ |
||
112 | 1 | View Code Duplication | protected function expireResponses() |
130 | } |
||
131 |
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.