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 |
||
| 12 | class Generic |
||
| 13 | { |
||
| 14 | use \PHPDaemon\Traits\ClassWatchdog; |
||
| 15 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
| 16 | |||
| 17 | /** @var */ |
||
| 18 | public $defaultValue; |
||
| 19 | /** @var */ |
||
| 20 | public $value; |
||
| 21 | /** @var */ |
||
| 22 | public $valueType; |
||
| 23 | /** @var */ |
||
| 24 | public $humanValue; |
||
| 25 | /** @var */ |
||
| 26 | public $source; |
||
| 27 | /** @var */ |
||
| 28 | public $revision; |
||
| 29 | /** @var bool */ |
||
| 30 | public $hasDefaultValue = false; |
||
| 31 | /** @var bool */ |
||
| 32 | protected $stackable = false; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Constructor |
||
| 36 | * @return void |
||
|
|
|||
| 37 | */ |
||
| 38 | public function __construct() |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Set default value |
||
| 48 | * @param mixed |
||
| 49 | * @return void |
||
| 50 | */ |
||
| 51 | public function setDefaultValue($value) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Converts plain value to human-readable |
||
| 59 | * @param mixed |
||
| 60 | * @return mixed |
||
| 61 | */ |
||
| 62 | public static function plainToHuman($value) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get human value |
||
| 69 | * @return mixed |
||
| 70 | */ |
||
| 71 | public function getHumanValue() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Set human-readable value |
||
| 78 | * @param mixed |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | View Code Duplication | public function setHumanValue($value) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Get value |
||
| 91 | * @return mixed |
||
| 92 | */ |
||
| 93 | public function getValue() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Set value |
||
| 100 | * @param mixed |
||
| 101 | * @return void |
||
| 102 | */ |
||
| 103 | View Code Duplication | public function setValue($value) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | public function isStackable() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param bool $b |
||
| 121 | */ |
||
| 122 | public function setStackable($b = true) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Push human-readable value |
||
| 129 | * @param $value |
||
| 130 | * @return void |
||
| 131 | */ |
||
| 132 | public function pushHumanValue($value) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Push plain value |
||
| 139 | * @param $value |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | public function pushValue($value) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Called when |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | public function onUpdate($old) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Converts human-readable value to plain |
||
| 178 | * @param mixed |
||
| 179 | * @return mixed |
||
| 180 | */ |
||
| 181 | public static function humanToPlain($value) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Set value type |
||
| 188 | * @param mixed |
||
| 189 | * @return void |
||
| 190 | */ |
||
| 191 | public function setValueType($type) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Reset to default |
||
| 198 | * @return boolean Success |
||
| 199 | */ |
||
| 200 | public function resetToDefault() |
||
| 208 | } |
||
| 209 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.