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 |
||
31 | View Code Duplication | trait OrderTrait |
|
|
|||
32 | { |
||
33 | use AbstractTrait; |
||
34 | |||
35 | /** |
||
36 | * {@inheritDoc} |
||
37 | */ |
||
38 | public function order($col) |
||
39 | { |
||
40 | if (func_num_args() > 1) { |
||
41 | $col = func_get_args(); |
||
42 | } |
||
43 | return $this->realOrder($col, 'ASC'); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * {@inheritDoc} |
||
48 | */ |
||
49 | public function orderDesc($col) |
||
50 | { |
||
51 | if (func_num_args() > 1) { |
||
52 | $col = func_get_args(); |
||
53 | } |
||
54 | return $this->realOrder($col, 'DESC'); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * {@inheritDoc} |
||
59 | */ |
||
60 | public function orderTpl(/*# string */ $template, $col, array $params = []) |
||
61 | { |
||
62 | $template = $this->positionedParam($template, $params); |
||
63 | return $this->realOrder(new Template($template, $col), '', true); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * {@inheritDoc} |
||
68 | */ |
||
69 | public function orderRaw(/*# string */ $rawString, array $params = []) |
||
70 | { |
||
71 | $rawString = $this->positionedParam($rawString, $params); |
||
72 | return $this->realOrder($rawString, '', true); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Real orderby |
||
77 | * |
||
78 | * @param string|string[]|Template $col |
||
79 | * @param string $suffix 'ASC' or 'DESC' |
||
80 | * @param bool $rawMode |
||
81 | * @return $this |
||
82 | * @access protected |
||
83 | */ |
||
84 | protected function realOrder( |
||
101 | |||
102 | /** |
||
103 | * Multitple orderbys |
||
104 | * |
||
105 | * @param array $cols |
||
106 | * @param string $suffix 'ASC' or 'DESC' |
||
107 | * @access protected |
||
108 | */ |
||
109 | protected function multipleOrder(array $cols, /*# sting */ $suffix) |
||
115 | |||
116 | /** |
||
117 | * Build ORDER BY |
||
118 | * |
||
119 | * @param string $prefix |
||
120 | * @param array $settings |
||
121 | * @return string |
||
122 | * @access protected |
||
123 | */ |
||
124 | protected function buildOrder( |
||
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.