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 |
||
| 20 | class ConsoleController extends yii\console\Controller |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Confirm action |
||
| 24 | * @param string $message message for confirm |
||
| 25 | * @param bool $default default selection |
||
| 26 | * @throws yii\base\ExitException |
||
| 27 | */ |
||
| 28 | public function confirmAction($message, $default = false) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Show action usage |
||
| 38 | * @param string $required required params separated by comma |
||
| 39 | * @param string $optional optional params separated by comma |
||
| 40 | * @param array $errors errors to output |
||
| 41 | * @throws yii\base\ExitException |
||
| 42 | */ |
||
| 43 | protected function showUsage($required = '', $optional = '', $errors = []) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Show errors |
||
| 76 | * @param array $errors array of errors string |
||
| 77 | * @throws yii\base\ExitException |
||
| 78 | */ |
||
| 79 | protected function showErrors($errors) |
||
| 86 | |||
| 87 | } |
||
| 88 |
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.