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 |
||
| 47 | class PublicKeyToken extends Entity implements IToken { |
||
| 48 | |||
| 49 | const VERSION = 2; |
||
| 50 | |||
| 51 | /** @var string user UID */ |
||
| 52 | protected $uid; |
||
| 53 | |||
| 54 | /** @var string login name used for generating the token */ |
||
| 55 | protected $loginName; |
||
| 56 | |||
| 57 | /** @var string encrypted user password */ |
||
| 58 | protected $password; |
||
| 59 | |||
| 60 | /** @var string token name (e.g. browser/OS) */ |
||
| 61 | protected $name; |
||
| 62 | |||
| 63 | /** @var string */ |
||
| 64 | protected $token; |
||
| 65 | |||
| 66 | /** @var int */ |
||
| 67 | protected $type; |
||
| 68 | |||
| 69 | /** @var int */ |
||
| 70 | protected $remember; |
||
| 71 | |||
| 72 | /** @var int */ |
||
| 73 | protected $lastActivity; |
||
| 74 | |||
| 75 | /** @var int */ |
||
| 76 | protected $lastCheck; |
||
| 77 | |||
| 78 | /** @var string */ |
||
| 79 | protected $scope; |
||
| 80 | |||
| 81 | /** @var int */ |
||
| 82 | protected $expires; |
||
| 83 | |||
| 84 | /** @var string */ |
||
| 85 | protected $privateKey; |
||
| 86 | |||
| 87 | /** @var string */ |
||
| 88 | protected $publicKey; |
||
| 89 | |||
| 90 | /** @var int */ |
||
| 91 | protected $version; |
||
| 92 | |||
| 93 | public function __construct() { |
||
| 109 | |||
| 110 | public function getId(): int { |
||
| 113 | |||
| 114 | public function getUID(): string { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get the login name used when generating the token |
||
| 120 | * |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | public function getLoginName(): string { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get the (encrypted) login password |
||
| 129 | * |
||
| 130 | * @return string|null |
||
| 131 | */ |
||
| 132 | public function getPassword() { |
||
| 135 | |||
| 136 | View Code Duplication | public function jsonSerialize() { |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Get the timestamp of the last password check |
||
| 148 | * |
||
| 149 | * @return int |
||
| 150 | */ |
||
| 151 | public function getLastCheck(): int { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get the timestamp of the last password check |
||
| 157 | * |
||
| 158 | * @param int $time |
||
| 159 | */ |
||
| 160 | public function setLastCheck(int $time) { |
||
| 163 | |||
| 164 | public function getScope(): string { |
||
| 172 | |||
| 173 | View Code Duplication | public function getScopeAsArray(): array { |
|
| 182 | |||
| 183 | public function setScope($scope) { |
||
| 190 | |||
| 191 | public function getName(): string { |
||
| 194 | |||
| 195 | public function getRemember(): int { |
||
| 198 | |||
| 199 | public function setToken(string $token) { |
||
| 202 | |||
| 203 | public function setPassword(string $password = null) { |
||
| 206 | |||
| 207 | public function setExpires($expires) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return int|null |
||
| 213 | */ |
||
| 214 | public function getExpires() { |
||
| 217 | } |
||
| 218 |
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: