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 |
||
| 7 | trait AutoTreeTrait |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var \yii\base\Behavior[] |
||
| 11 | */ |
||
| 12 | private $_autoTreeMap; |
||
| 13 | |||
| 14 | private $_autoTreeReturnBehavior = false; |
||
| 15 | |||
| 16 | |||
| 17 | /** |
||
| 18 | * @return array |
||
| 19 | */ |
||
| 20 | 61 | private static function autoTreeAliases() |
|
| 33 | |||
| 34 | /** |
||
| 35 | * |
||
| 36 | */ |
||
| 37 | 61 | private function autoTreeInit() |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @param string $method |
||
| 51 | * @param array $list |
||
| 52 | * @param array $arguments |
||
| 53 | * @param bool $firstOnly |
||
| 54 | * @return mixed |
||
| 55 | */ |
||
| 56 | 61 | private function autoTreeCall($method, $list, $arguments = [], $firstOnly = true) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @inheritdoc |
||
| 89 | */ |
||
| 90 | 51 | public function __get($name) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @param int|null $depth |
||
| 115 | * @return \yii\db\ActiveQuery |
||
| 116 | */ |
||
| 117 | 3 | public function getParents($depth = null) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @return \yii\db\ActiveQuery |
||
| 124 | */ |
||
| 125 | 3 | public function getParent() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @return \yii\db\ActiveQuery |
||
| 132 | */ |
||
| 133 | 3 | public function getRoot() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @param int|null $depth |
||
| 140 | * @param bool $andSelf |
||
| 141 | * @return \yii\db\ActiveQuery |
||
| 142 | */ |
||
| 143 | 5 | public function getDescendants($depth = null, $andSelf = false) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @return \yii\db\ActiveQuery |
||
| 150 | */ |
||
| 151 | 8 | public function getChildren() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @param int|null $depth |
||
| 158 | * @return \yii\db\ActiveQuery |
||
| 159 | */ |
||
| 160 | 3 | public function getLeaves($depth = null) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @return \yii\db\ActiveQuery |
||
| 167 | * @throws \yii\base\NotSupportedException |
||
| 168 | */ |
||
| 169 | 3 | public function getPrev() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @return \yii\db\ActiveQuery |
||
| 176 | * @throws \yii\base\NotSupportedException |
||
| 177 | */ |
||
| 178 | 3 | public function getNext() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Populate children relations for self and all descendants |
||
| 185 | * @param int $depth = null |
||
| 186 | * @return self |
||
| 187 | */ |
||
| 188 | public function populateTree($depth = null) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | 12 | public function isRoot() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @param \yii\db\BaseActiveRecord $node |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | 17 | public function isChildOf($node) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | 3 | public function isLeaf() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @return $this |
||
| 220 | */ |
||
| 221 | 6 | public function makeRoot() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @param \yii\db\BaseActiveRecord $node |
||
| 228 | * @return $this |
||
| 229 | */ |
||
| 230 | 6 | public function prependTo($node) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * @param \yii\db\BaseActiveRecord $node |
||
| 237 | * @return $this |
||
| 238 | */ |
||
| 239 | 3 | public function appendTo($node) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @param \yii\db\BaseActiveRecord $node |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | 3 | public function insertBefore($node) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * @param \yii\db\BaseActiveRecord $node |
||
| 255 | * @return $this |
||
| 256 | */ |
||
| 257 | 6 | public function insertAfter($node) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @return bool|int |
||
| 264 | * @throws \Exception |
||
| 265 | * @throws \yii\db\Exception |
||
| 266 | */ |
||
| 267 | 3 | public function deleteWithChildren() |
|
| 271 | } |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.