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 |
||
| 25 | class UserData extends AbstractModel implements |
||
| 26 | UserDataInterface |
||
| 27 | { |
||
| 28 | use TranslatorAwareTrait; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Client IP address of the end-user. |
||
| 32 | * |
||
| 33 | * @var integer|null |
||
| 34 | */ |
||
| 35 | private $ip; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Language of the end-user or source URI. |
||
| 39 | * |
||
| 40 | * @var string|null |
||
| 41 | */ |
||
| 42 | private $lang; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Source URL or identifier of end-user submission. |
||
| 46 | * |
||
| 47 | * @var string|null |
||
| 48 | */ |
||
| 49 | private $origin; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Creation timestamp of submission. |
||
| 53 | * |
||
| 54 | * @var DateTimeInterface|null |
||
| 55 | */ |
||
| 56 | private $ts; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Dependencies |
||
| 60 | * @param Container $container DI Container. |
||
| 61 | * @return void |
||
| 62 | */ |
||
| 63 | public function setDependencies(Container $container) |
||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * Set the client IP address. |
||
| 73 | * |
||
| 74 | * @param integer|null $ip The remote IP at object creation. |
||
| 75 | * @return UserDataInterface Chainable |
||
| 76 | */ |
||
| 77 | public function setIp($ip) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Retrieve the client IP address. |
||
| 99 | * |
||
| 100 | * @return integer|null |
||
| 101 | */ |
||
| 102 | public function ip() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Set the origin language. |
||
| 109 | * |
||
| 110 | * @param string $lang The language code. |
||
| 111 | * @throws InvalidArgumentException If the argument is not a string. |
||
| 112 | * @return UserDataInterface Chainable |
||
| 113 | */ |
||
| 114 | View Code Duplication | public function setLang($lang) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Retrieve the language. |
||
| 131 | * |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | public function lang() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Set the origin of the object submission. |
||
| 141 | * |
||
| 142 | * @param string $origin The source URL or identifier of the submission. |
||
| 143 | * @throws InvalidArgumentException If the argument is not a string. |
||
| 144 | * @return UserDataInterface Chainable |
||
| 145 | */ |
||
| 146 | View Code Duplication | public function setOrigin($origin) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Resolve the origin of the user data. |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function resolveOrigin() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Retrieve the origin of the object submission. |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function origin() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Set when the object was created. |
||
| 196 | * |
||
| 197 | * @param DateTime|string|null $timestamp The timestamp at object's creation. |
||
| 198 | * NULL is accepted and instances of DateTimeInterface are recommended; |
||
| 199 | * any other value will be converted (if possible) into one. |
||
| 200 | * @throws InvalidArgumentException If the timestamp is invalid. |
||
| 201 | * @return UserDataInterface Chainable |
||
| 202 | */ |
||
| 203 | View Code Duplication | public function setTs($timestamp) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Retrieve the creation timestamp. |
||
| 234 | * |
||
| 235 | * @return DateTime|null |
||
| 236 | */ |
||
| 237 | public function ts() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Event called before _creating_ the object. |
||
| 244 | * |
||
| 245 | * @see Charcoal\Source\StorableTrait::preSave() For the "create" Event. |
||
| 246 | * @return boolean |
||
| 247 | */ |
||
| 248 | public function preSave() |
||
| 264 | } |
||
| 265 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.