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 | class Field |
||
| 11 | { |
||
| 12 | protected $table; |
||
| 13 | protected $name; |
||
| 14 | protected $config = []; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @param Table $table |
||
| 18 | * @param string $name |
||
| 19 | */ |
||
| 20 | public function __construct(Table $table, $name) |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Converts the data to save in the database |
||
| 28 | * |
||
| 29 | * @param mixed $data |
||
| 30 | * |
||
| 31 | * @return mixed |
||
| 32 | */ |
||
| 33 | public function dataToDatabase($data) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Converts the data to be used |
||
| 44 | * |
||
| 45 | * @param mixed $data |
||
| 46 | * |
||
| 47 | * @return mixed |
||
| 48 | */ |
||
| 49 | public function dataFromDatabase($data) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Returns the field scheme |
||
| 56 | * |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | public function getScheme() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Returns the expression used to get the value from the database |
||
| 66 | * |
||
| 67 | * @param string|null $as |
||
| 68 | * |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | View Code Duplication | public function getSelectExpression($as = null) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Returns the expression used to save the value in the database |
||
| 85 | * |
||
| 86 | * @param string $mark |
||
| 87 | * |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public function getValueExpression($mark) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns a config value |
||
| 97 | * |
||
| 98 | * @return mixed |
||
| 99 | */ |
||
| 100 | public function getConfig($name) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Edit a config value |
||
| 107 | * |
||
| 108 | * @param string $name |
||
| 109 | * @param mixed $value |
||
| 110 | * |
||
| 111 | * @return self |
||
| 112 | */ |
||
| 113 | public function setConfig($name, $value) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Return the default value |
||
| 122 | */ |
||
| 123 | public function getDefaultValue() |
||
| 127 | } |
||
| 128 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: