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 | /** |
||
| 58 | * @param null $db |
||
| 59 | * @throws \Exception |
||
| 60 | * @return Command |
||
| 61 | */ |
||
| 62 | public function createCommand($db = null) |
||
| 72 | |||
| 73 | public function one($db = null) |
||
| 77 | |||
| 78 | public function searchOne($db = null) |
||
| 82 | |||
| 83 | public function all($db = null) |
||
| 84 | { |
||
| 85 | $rows = $this->searchAll(); |
||
| 86 | |||
| 87 | if (!empty($rows) && $this->indexBy !== null) { |
||
| 88 | $result = []; |
||
| 89 | foreach ($rows as $row) { |
||
| 90 | View Code Duplication | if ($this->indexBy instanceof \Closure) { |
|
| 91 | $key = call_user_func($this->indexBy, $row); |
||
| 92 | } else { |
||
| 93 | $key = $row[$this->indexBy]; |
||
| 94 | } |
||
| 95 | $result[$key] = $row; |
||
| 96 | } |
||
| 97 | $rows = $result; |
||
| 98 | } |
||
| 99 | |||
| 100 | return $rows; |
||
| 101 | } |
||
| 102 | |||
| 103 | 2 | public function searchAll($db = null) |
|
| 107 | |||
| 108 | 2 | public function search($db = null) |
|
| 112 | |||
| 113 | public function delete($db = null, $options = []) |
||
| 117 | |||
| 118 | public function count($q = '*', $db = null) |
||
| 124 | |||
| 125 | public function exists($db = null) |
||
| 129 | |||
| 130 | 1 | public function action($action) |
|
| 136 | |||
| 137 | 2 | public function addAction($action) |
|
| 138 | { |
||
| 139 | 2 | if (empty($this->action)) { |
|
| 140 | 2 | $this->action = $action; |
|
| 141 | } |
||
| 142 | |||
| 143 | 2 | return $this; |
|
| 144 | } |
||
| 145 | |||
| 146 | 2 | public function addOption($name, $value) |
|
| 147 | { |
||
| 148 | 2 | if (!isset($this->options[$name])) { |
|
| 149 | 2 | $this->options[$name] = $value; |
|
| 150 | } |
||
| 151 | |||
| 152 | 2 | return $this; |
|
| 153 | } |
||
| 154 | |||
| 155 | public function getOption($name) |
||
| 159 | |||
| 160 | 1 | public function options($options) |
|
| 166 | |||
| 167 | public function addOptions($options) |
||
| 168 | { |
||
| 169 | if (!empty($options)) { |
||
| 170 | $this->options = array_merge($this->options, $options); |
||
| 171 | } |
||
| 172 | |||
| 173 | return $this; |
||
| 174 | } |
||
| 175 | |||
| 176 | 1 | public function body($body) |
|
| 182 | |||
| 183 | public function innerJoin($table, $on = '', $params = []) |
||
| 189 | |||
| 190 | View Code Duplication | public function fields($fields) |
|
| 191 | { |
||
| 192 | if (is_array($fields) || $fields === null) { |
||
| 193 | $this->fields = $fields; |
||
| 200 | |||
| 201 | View Code Duplication | public function source($source) |
|
| 211 | } |
||
| 212 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.