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 /** MicroQuery */ |
||
19 | class Query implements IQuery |
||
20 | { |
||
21 | /** @var IConnection $db Connection */ |
||
22 | public $db; |
||
23 | |||
24 | /** @var string $select selectable columns */ |
||
25 | public $select = '*'; |
||
26 | /** @var boolean $distinct unique rows */ |
||
27 | public $distinct = false; |
||
28 | /** @var string $where condition */ |
||
29 | public $where = ''; |
||
30 | /** @var string $join joins tables */ |
||
31 | public $join = ''; |
||
32 | /** @var string $order sorting result rows */ |
||
33 | public $order = ''; |
||
34 | /** @var string $group grouping result rows */ |
||
35 | public $group = ''; |
||
36 | /** @var string $having condition for result rows */ |
||
37 | public $having = ''; |
||
38 | /** @var integer $limit count result rows */ |
||
39 | public $limit = -1; |
||
40 | /** @var integer $offset offset on start result rows */ |
||
41 | public $offset = -1; |
||
42 | /** @var array $params masks for where */ |
||
43 | public $params = []; |
||
44 | /** @var string $table table for query */ |
||
45 | public $table = ''; |
||
46 | /** @var string $objectName class name form fetching */ |
||
47 | public $objectName = ''; |
||
48 | /** @var boolean $single is one result? */ |
||
49 | public $single = false; |
||
50 | |||
51 | /** |
||
52 | * Construct class |
||
53 | * |
||
54 | * @access public |
||
55 | * |
||
56 | * @param IConnection $db |
||
57 | * |
||
58 | * @result void |
||
59 | */ |
||
60 | public function __construct(IConnection $db) |
||
61 | { |
||
62 | $this->db = $db; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @inheritdoc |
||
67 | */ |
||
68 | public function addSearch($column, $keyword, $escaped = false, $operand = 'AND') |
||
73 | |||
74 | /** |
||
75 | * @inheritdoc |
||
76 | */ |
||
77 | public function addWhere($sql, $operand = 'AND') |
||
78 | { |
||
79 | $this->where .= $this->where ? ' '.$operand.' ('.$sql.')' : ' '.$this->where.' ('.$sql.')'; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @inheritdoc |
||
84 | */ |
||
85 | public function addNotSearch($column, $keyword, $escaped, $operand = 'AND') |
||
90 | |||
91 | /** |
||
92 | * @inheritdoc |
||
93 | */ |
||
94 | View Code Duplication | public function addIn($column, $params, $operand = 'AND') |
|
102 | |||
103 | /** |
||
104 | * @inheritdoc |
||
105 | */ |
||
106 | View Code Duplication | public function addNotIn($column, $params, $operand = 'AND') |
|
114 | |||
115 | /** |
||
116 | * @inheritdoc |
||
117 | */ |
||
118 | public function addBetween($column, $start, $stop, $operand = 'AND') |
||
122 | |||
123 | /** |
||
124 | * @inheritdoc |
||
125 | */ |
||
126 | public function addNotBetween($column, $start, $stop, $operand = 'AND') |
||
130 | |||
131 | /** |
||
132 | * @inheritdoc |
||
133 | */ |
||
134 | public function addJoin($table, $condition, $type = 'LEFT') |
||
138 | |||
139 | /** |
||
140 | * @inheritdoc |
||
141 | */ |
||
142 | public function run($as = \PDO::FETCH_CLASS) |
||
151 | |||
152 | /** |
||
153 | * @inheritdoc |
||
154 | */ |
||
155 | public function getQuery() |
||
187 | } |
||
188 |
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.