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 |
||
34 | class Query extends \yii\db\Query implements QueryInterface |
||
35 | { |
||
36 | /** |
||
37 | * @var string action that this query performs |
||
38 | */ |
||
39 | public $action; |
||
40 | |||
41 | /** |
||
42 | * @var array query options e.g. raw, batch |
||
43 | */ |
||
44 | public $options = []; |
||
45 | |||
46 | public $count; |
||
47 | |||
48 | public $body = []; |
||
49 | |||
50 | 1 | public static function instantiate($action, $from, array $options = []) |
|
56 | |||
57 | public function createCommand($db = null) |
||
67 | |||
68 | public function one($db = null) |
||
72 | |||
73 | public function all($db = null) |
||
74 | { |
||
75 | $rows = $this->searchAll(); |
||
76 | |||
77 | if (!empty($rows) && $this->indexBy !== null) { |
||
78 | $result = []; |
||
79 | foreach ($rows as $row) { |
||
80 | View Code Duplication | if ($this->indexBy instanceof \Closure) { |
|
|
|||
81 | $key = call_user_func($this->indexBy, $row); |
||
82 | } else { |
||
83 | $key = $row[$this->indexBy]; |
||
84 | } |
||
85 | $result[$key] = $row; |
||
86 | } |
||
87 | $rows = $result; |
||
88 | } |
||
89 | |||
90 | return $rows; |
||
91 | } |
||
92 | |||
93 | 2 | public function searchAll($db = null) |
|
97 | |||
98 | 2 | public function search($db = null) |
|
102 | |||
103 | public function delete($db = null, $options = []) |
||
107 | |||
108 | public function count($q = '*', $db = null) |
||
114 | |||
115 | public function exists($db = null) |
||
119 | |||
120 | 1 | public function action($action) |
|
126 | |||
127 | 2 | public function addAction($action) |
|
128 | { |
||
129 | 2 | if (empty($this->action)) { |
|
130 | 2 | $this->action = $action; |
|
131 | } |
||
132 | |||
133 | 2 | return $this; |
|
134 | } |
||
135 | |||
136 | 2 | public function addOption($name, $value) |
|
137 | { |
||
138 | 2 | if (!isset($this->options[$name])) { |
|
139 | 2 | $this->options[$name] = $value; |
|
140 | } |
||
141 | |||
142 | 2 | return $this; |
|
143 | } |
||
144 | |||
145 | public function getOption($name) |
||
149 | |||
150 | 1 | public function options($options) |
|
156 | |||
157 | public function addOptions($options) |
||
158 | { |
||
159 | if (!empty($options)) { |
||
160 | $this->options = array_merge($this->options, $options); |
||
161 | } |
||
162 | |||
163 | return $this; |
||
164 | } |
||
165 | |||
166 | 1 | public function body($body) |
|
172 | |||
173 | public function innerJoin($table, $on = '', $params = []) |
||
179 | |||
180 | View Code Duplication | public function fields($fields) |
|
181 | { |
||
182 | if (is_array($fields) || $fields === null) { |
||
183 | $this->fields = $fields; |
||
190 | |||
191 | View Code Duplication | public function source($source) |
|
201 | } |
||
202 |
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.