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 |
||
| 15 | trait FindModelTrait |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Finds model |
||
| 19 | * |
||
| 20 | * @param mixed $modelClass |
||
| 21 | * @param mixed $condition primary key value or a set of column values |
||
| 22 | * @param string $notFoundMessage |
||
| 23 | * |
||
| 24 | * @throws NotFoundHttpException |
||
| 25 | * |
||
| 26 | * @return ActiveRecord |
||
| 27 | */ |
||
| 28 | View Code Duplication | protected function findModel($modelClass, $condition, string $notFoundMessage = 'The requested page does not exist.') |
|
| 36 | |||
| 37 | /** |
||
| 38 | * @param mixed $modelClass |
||
| 39 | * @param mixed $condition primary key value or a set of column values |
||
| 40 | * |
||
| 41 | * @return ActiveRecord |
||
| 42 | */ |
||
| 43 | View Code Duplication | protected function findModelOrCreate($modelClass, $condition) |
|
| 51 | } |
||
| 52 |