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 |
||
| 10 | trait SortModels |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Sort records by their id list. |
||
| 14 | * Note: you must manually call refresh() on needed models |
||
| 15 | * @param array $ids array of records id in needed order(ie. [4, 3, 1, 2]) |
||
| 16 | * @param string $field Field that stores sort order |
||
| 17 | * @return bool |
||
| 18 | * @throws yii\db\Exception |
||
| 19 | */ |
||
| 20 | View Code Duplication | public static function sortModels($ids, $field = 'sort_order') |
|
| 39 | |||
| 40 | View Code Duplication | public static function generateCase($priorities) |
|
| 48 | |||
| 49 | public static function moveIdBefore($id, $id_before, $field = 'sort_order') |
||
| 56 | |||
| 57 | public static function moveIdAfter($id, $id_after, $field = 'sort_order') |
||
| 64 | } |
||
| 65 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.