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 |
||
| 27 | class UserData extends AbstractModel implements |
||
|
|
|||
| 28 | UserDataInterface |
||
| 29 | { |
||
| 30 | use TranslatorAwareTrait; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Client IP address of the end-user. |
||
| 34 | * |
||
| 35 | * @var integer|null |
||
| 36 | */ |
||
| 37 | private $ip; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Language of the end-user or source URI. |
||
| 41 | * |
||
| 42 | * @var string|null |
||
| 43 | */ |
||
| 44 | private $lang; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Source URL or identifier of end-user submission. |
||
| 48 | * |
||
| 49 | * @var string|null |
||
| 50 | */ |
||
| 51 | private $origin; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Creation timestamp of submission. |
||
| 55 | * |
||
| 56 | * @var DateTimeInterface|null |
||
| 57 | */ |
||
| 58 | private $ts; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Dependencies |
||
| 62 | * @param Container $container DI Container. |
||
| 63 | * @return void |
||
| 64 | */ |
||
| 65 | protected function setDependencies(Container $container) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Set the client IP address. |
||
| 74 | * |
||
| 75 | * @param integer|null $ip The remote IP at object creation. |
||
| 76 | * @return self |
||
| 77 | */ |
||
| 78 | public function setIp($ip) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Retrieve the client IP address. |
||
| 100 | * |
||
| 101 | * @return integer|null |
||
| 102 | */ |
||
| 103 | public function getIp() |
||
| 104 | { |
||
| 105 | return $this->ip; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Set the origin language. |
||
| 110 | * |
||
| 111 | * @param string $lang The language code. |
||
| 112 | * @throws InvalidArgumentException If the argument is not a string. |
||
| 113 | * @return self |
||
| 114 | */ |
||
| 115 | View Code Duplication | public function setLang($lang) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Retrieve the language. |
||
| 132 | * |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function getLang() |
||
| 136 | { |
||
| 137 | return $this->lang; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Set the origin of the object submission. |
||
| 142 | * |
||
| 143 | * @param string $origin The source URL or identifier of the submission. |
||
| 144 | * @throws InvalidArgumentException If the argument is not a string. |
||
| 145 | * @return self |
||
| 146 | */ |
||
| 147 | View Code Duplication | public function setOrigin($origin) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Resolve the origin of the user data. |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public function resolveOrigin() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Retrieve the origin of the object submission. |
||
| 187 | * |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function getOrigin() |
||
| 191 | { |
||
| 192 | return $this->origin; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Set when the object was created. |
||
| 197 | * |
||
| 198 | * @param DateTimeInterface|string|null $timestamp The timestamp at object's creation. |
||
| 199 | * NULL is accepted and instances of DateTimeInterface are recommended; |
||
| 200 | * any other value will be converted (if possible) into one. |
||
| 201 | * @throws InvalidArgumentException If the timestamp is invalid. |
||
| 202 | * @return self |
||
| 203 | */ |
||
| 204 | View Code Duplication | public function setTs($timestamp) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Retrieve the creation timestamp. |
||
| 235 | * |
||
| 236 | * @return DateTimeInterface|null |
||
| 237 | */ |
||
| 238 | public function getTs() |
||
| 239 | { |
||
| 240 | return $this->ts; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Event called before _creating_ the object. |
||
| 245 | * |
||
| 246 | * @see Charcoal\Source\StorableTrait::preSave() For the "create" Event. |
||
| 247 | * @return boolean |
||
| 248 | */ |
||
| 249 | protected function preSave() |
||
| 265 | } |
||
| 266 |