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 | public static function instantiate($action, $from, array $options = []) |
||
| 51 | { |
||
| 52 | $query = new static(); |
||
| 53 | |||
| 54 | return $query->action($action)->from($from)->options($options); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param null $db |
||
| 59 | * @throws \Exception |
||
| 60 | * @return Command |
||
| 61 | */ |
||
| 62 | 2 | public function createCommand($db = null) |
|
| 63 | { |
||
| 64 | 2 | if ($db === null) { |
|
| 65 | throw new \Exception('no db given to Query::createCommand'); |
||
| 66 | } |
||
| 67 | |||
| 68 | 2 | $commandConfig = $db->getQueryBuilder()->build($this); |
|
|
|
|||
| 69 | |||
| 70 | 2 | return $db->createCommand($commandConfig); |
|
| 71 | } |
||
| 72 | |||
| 73 | public function one($db = null) |
||
| 77 | |||
| 78 | public function searchOne($db = null) |
||
| 82 | |||
| 83 | public function searchSingle($db = null) |
||
| 84 | { |
||
| 87 | |||
| 88 | public function all($db = null) |
||
| 89 | { |
||
| 90 | $rows = $this->searchAll($db); |
||
| 91 | |||
| 92 | if (!empty($rows) && $this->indexBy !== null) { |
||
| 93 | $result = []; |
||
| 94 | foreach ($rows as $row) { |
||
| 95 | View Code Duplication | if ($this->indexBy instanceof \Closure) { |
|
| 96 | $key = call_user_func($this->indexBy, $row); |
||
| 97 | } else { |
||
| 98 | $key = $row[$this->indexBy]; |
||
| 99 | } |
||
| 100 | $result[$key] = $row; |
||
| 101 | } |
||
| 102 | $rows = $result; |
||
| 103 | } |
||
| 104 | |||
| 105 | return $rows; |
||
| 106 | } |
||
| 107 | |||
| 108 | 2 | public function searchAll($db = null) |
|
| 112 | |||
| 113 | 2 | public function searchBatch($db = null) |
|
| 117 | |||
| 118 | 2 | public function search($db = null) |
|
| 122 | |||
| 123 | public function delete($db = null, $options = []) |
||
| 127 | |||
| 128 | public function count($q = '*', $db = null) |
||
| 134 | |||
| 135 | public function exists($db = null) |
||
| 139 | |||
| 140 | public function action($action) |
||
| 141 | { |
||
| 142 | $this->action = $action; |
||
| 143 | |||
| 144 | return $this; |
||
| 145 | } |
||
| 146 | |||
| 147 | 2 | public function addAction($action) |
|
| 148 | { |
||
| 149 | 2 | if (empty($this->action)) { |
|
| 150 | 2 | $this->action = $action; |
|
| 151 | } |
||
| 152 | |||
| 153 | 2 | return $this; |
|
| 154 | } |
||
| 155 | |||
| 156 | 2 | public function addOption($name, $value) |
|
| 157 | { |
||
| 158 | 2 | if (!isset($this->options[$name])) { |
|
| 159 | 2 | $this->options[$name] = $value; |
|
| 160 | } |
||
| 161 | |||
| 162 | 2 | return $this; |
|
| 163 | } |
||
| 164 | |||
| 165 | public function getOption($name) |
||
| 169 | |||
| 170 | public function options($options) |
||
| 171 | { |
||
| 172 | $this->options = $options; |
||
| 173 | |||
| 174 | return $this; |
||
| 175 | } |
||
| 176 | |||
| 177 | public function addOptions($options) |
||
| 178 | { |
||
| 179 | if (!empty($options)) { |
||
| 180 | $this->options = array_merge($this->options, $options); |
||
| 181 | } |
||
| 182 | |||
| 183 | return $this; |
||
| 184 | } |
||
| 185 | |||
| 186 | public function body($body) |
||
| 187 | { |
||
| 188 | $this->body = $body; |
||
| 189 | |||
| 190 | return $this; |
||
| 191 | } |
||
| 192 | |||
| 193 | public function innerJoin($table, $on = '', $params = []) |
||
| 199 | |||
| 200 | View Code Duplication | public function fields($fields) |
|
| 201 | { |
||
| 202 | if (is_array($fields) || $fields === null) { |
||
| 203 | $this->fields = $fields; |
||
| 204 | } else { |
||
| 205 | $this->fields = func_get_args(); |
||
| 206 | } |
||
| 210 | |||
| 211 | View Code Duplication | public function source($source) |
|
| 221 | } |
||
| 222 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.