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 | ||
| 22 | class MediaController extends Controller | ||
| 23 | { | ||
| 24 | /** | ||
| 25 | * @param integer|null $id Media ID | ||
| 26 | * @param string|null $slug Media slug | ||
| 27 | * @return mixed | ||
| 28 | * @throws \yii\web\NotFoundHttpException | ||
| 29 | */ | ||
| 30 | public function actionView($id = null, $slug = null) | ||
| 69 | |||
| 70 | /** | ||
| 71 | * Finds the Media model based on its primary key value. | ||
| 72 | * If the model is not found, a 404 HTTP exception will be thrown. | ||
| 73 | * | ||
| 74 | * @param integer $id Media ID | ||
| 75 | * @return Media the loaded model | ||
| 76 | * @throws NotFoundHttpException if the model cannot be found | ||
| 77 | */ | ||
| 78 | protected function findModel($id) | ||
| 86 | |||
| 87 | /** | ||
| 88 | * Finds the Media model based on its primary key value. | ||
| 89 | * If the model is not found, a 404 HTTP exception will be thrown. | ||
| 90 | * | ||
| 91 | * @param string $slug Media slug | ||
| 92 | * @return Media the loaded model | ||
| 93 | * @throws NotFoundHttpException if the model cannot be found | ||
| 94 | */ | ||
| 95 | View Code Duplication | protected function findModelBySlug($slug) | |
| 103 | } | ||
| 104 | 
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: