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 | abstract class AbstractStatisticsLoader extends AbstractLoader |
||
9 | { |
||
10 | /** |
||
11 | * Flags set for workarounds |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $flags; |
||
16 | |||
17 | /** |
||
18 | * Allows setting of workaround flags |
||
19 | * |
||
20 | * @param string $flag |
||
21 | */ |
||
22 | public function setFlags($flag) |
||
26 | |||
27 | /** |
||
28 | * Retrieves workaround flags |
||
29 | * |
||
30 | * @return string |
||
31 | */ |
||
32 | public function getFlags() |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $type; |
||
41 | |||
42 | /** |
||
43 | * Returns the top X of a particular statistic |
||
44 | * |
||
45 | * @param array $post POST variables from the request |
||
46 | * |
||
47 | * @return array |
||
48 | */ |
||
49 | View Code Duplication | public function readStatistics($post) |
|
67 | |||
68 | /** |
||
69 | * Build a redis key based off inputs provided by the POST request |
||
70 | * |
||
71 | * @param array $post |
||
72 | * @param string $redisKey Redis Key to append to |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | public function appendRedisKey($post, $redisKey) |
||
104 | |||
105 | /** |
||
106 | * De-encode the POST from application/json vars for use |
||
107 | * |
||
108 | * @param array $post |
||
109 | * |
||
110 | * @return array |
||
111 | */ |
||
112 | public function processPost() |
||
130 | |||
131 | /** |
||
132 | * Takes common requests and appends them to the query object. Any other |
||
133 | * special requirements will be handled after |
||
134 | * |
||
135 | * @param Ps2alerts\Api\QueryObjects\QueryObject $queryObject |
||
136 | * @param array $post |
||
137 | * |
||
138 | * @return Ps2alerts\Api\QueryObjects\QueryObject |
||
139 | */ |
||
140 | public function setupQueryObject(QueryObject $queryObject, array $post) |
||
211 | } |
||
212 |
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.