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 | 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) |
||
| 87 | |||
| 88 | public function all($db = null) |
||
| 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 | 1 | public function action($action) |
|
| 146 | |||
| 147 | 2 | public function addAction($action) |
|
| 155 | |||
| 156 | 2 | public function addOption($name, $value) |
|
| 164 | |||
| 165 | public function getOption($name) |
||
| 169 | |||
| 170 | 1 | public function options($options) |
|
| 176 | |||
| 177 | public function addOptions($options) |
||
| 185 | |||
| 186 | 1 | public function body($body) |
|
| 192 | |||
| 193 | public function innerJoin($table, $on = '', $params = []) |
||
| 199 | |||
| 200 | View Code Duplication | public function fields($fields) |
|
| 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.