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 |
||
| 48 | class PublicKeyToken extends Entity implements IToken { |
||
| 49 | |||
| 50 | const VERSION = 2; |
||
| 51 | |||
| 52 | /** @var string user UID */ |
||
| 53 | protected $uid; |
||
| 54 | |||
| 55 | /** @var string login name used for generating the token */ |
||
| 56 | protected $loginName; |
||
| 57 | |||
| 58 | /** @var string encrypted user password */ |
||
| 59 | protected $password; |
||
| 60 | |||
| 61 | /** @var string token name (e.g. browser/OS) */ |
||
| 62 | protected $name; |
||
| 63 | |||
| 64 | /** @var string */ |
||
| 65 | protected $token; |
||
| 66 | |||
| 67 | /** @var int */ |
||
| 68 | protected $type; |
||
| 69 | |||
| 70 | /** @var int */ |
||
| 71 | protected $remember; |
||
| 72 | |||
| 73 | /** @var int */ |
||
| 74 | protected $lastActivity; |
||
| 75 | |||
| 76 | /** @var int */ |
||
| 77 | protected $lastCheck; |
||
| 78 | |||
| 79 | /** @var string */ |
||
| 80 | protected $scope; |
||
| 81 | |||
| 82 | /** @var int */ |
||
| 83 | protected $expires; |
||
| 84 | |||
| 85 | /** @var string */ |
||
| 86 | protected $privateKey; |
||
| 87 | |||
| 88 | /** @var string */ |
||
| 89 | protected $publicKey; |
||
| 90 | |||
| 91 | /** @var int */ |
||
| 92 | protected $version; |
||
| 93 | |||
| 94 | /** @var bool */ |
||
| 95 | protected $passwordInvalid; |
||
| 96 | |||
| 97 | public function __construct() { |
||
| 114 | |||
| 115 | public function getId(): int { |
||
| 118 | |||
| 119 | public function getUID(): string { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get the login name used when generating the token |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | public function getLoginName(): string { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get the (encrypted) login password |
||
| 134 | * |
||
| 135 | * @return string|null |
||
| 136 | */ |
||
| 137 | public function getPassword() { |
||
| 140 | |||
| 141 | View Code Duplication | public function jsonSerialize() { |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Get the timestamp of the last password check |
||
| 153 | * |
||
| 154 | * @return int |
||
| 155 | */ |
||
| 156 | public function getLastCheck(): int { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the timestamp of the last password check |
||
| 162 | * |
||
| 163 | * @param int $time |
||
| 164 | */ |
||
| 165 | public function setLastCheck(int $time) { |
||
| 168 | |||
| 169 | public function getScope(): string { |
||
| 177 | |||
| 178 | View Code Duplication | public function getScopeAsArray(): array { |
|
| 187 | |||
| 188 | public function setScope($scope) { |
||
| 195 | |||
| 196 | public function getName(): string { |
||
| 199 | |||
| 200 | public function getRemember(): int { |
||
| 203 | |||
| 204 | public function setToken(string $token) { |
||
| 207 | |||
| 208 | public function setPassword(string $password = null) { |
||
| 211 | |||
| 212 | public function setExpires($expires) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return int|null |
||
| 218 | */ |
||
| 219 | public function getExpires() { |
||
| 222 | |||
| 223 | public function setPasswordInvalid(bool $invalid) { |
||
| 226 | } |
||
| 227 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: