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 RemoteInstance extends NC21Signatory implements INC21QueryRow, JsonSerializable { |
||
| 48 | |||
| 49 | |||
| 50 | use TArrayTools; |
||
| 51 | |||
| 52 | const TYPE_UNKNOWN = 'Unknown'; // not trusted |
||
| 53 | const TYPE_PASSIVE = 'Passive'; // Minimum information about Federated Circles are broadcasted if a member belongs to the circle. |
||
| 54 | const TYPE_EXTERNAL = 'External'; // info about Federated Circles and their members are broadcasted if a member belongs to the circle. |
||
| 55 | const TYPE_TRUSTED = 'Trusted'; // everything about Federated Circles are broadcasted. |
||
| 56 | const TYPE_GLOBAL_SCALE = 'GlobalScale'; // every Circle is broadcasted, |
||
| 57 | |||
| 58 | public static $LIST_TYPE = [ |
||
| 59 | self::TYPE_UNKNOWN, |
||
| 60 | self::TYPE_PASSIVE, |
||
| 61 | self::TYPE_EXTERNAL, |
||
| 62 | self::TYPE_TRUSTED, |
||
| 63 | self::TYPE_GLOBAL_SCALE |
||
| 64 | ]; |
||
| 65 | |||
| 66 | const TEST = 'test'; |
||
| 67 | const INCOMING = 'incoming'; |
||
| 68 | const EVENT = 'event'; |
||
| 69 | const CIRCLES = 'circles'; |
||
| 70 | const CIRCLE = 'circle'; |
||
| 71 | const MEMBERS = 'members'; |
||
| 72 | const MEMBER = 'member'; |
||
| 73 | |||
| 74 | |||
| 75 | /** @var int */ |
||
| 76 | private $dbId = 0; |
||
| 77 | |||
| 78 | /** @var string */ |
||
| 79 | private $type = self::TYPE_UNKNOWN; |
||
| 80 | |||
| 81 | /** @var string */ |
||
| 82 | private $test = ''; |
||
| 83 | |||
| 84 | /** @var string */ |
||
| 85 | private $incoming = ''; |
||
| 86 | |||
| 87 | /** @var string */ |
||
| 88 | private $event = ''; |
||
| 89 | |||
| 90 | /** @var string */ |
||
| 91 | private $circles = ''; |
||
| 92 | |||
| 93 | /** @var string */ |
||
| 94 | private $circle = ''; |
||
| 95 | |||
| 96 | /** @var string */ |
||
| 97 | private $members = ''; |
||
| 98 | |||
| 99 | /** @var string */ |
||
| 100 | private $member = ''; |
||
| 101 | |||
| 102 | /** @var string */ |
||
| 103 | private $uid = ''; |
||
| 104 | |||
| 105 | /** @var string */ |
||
| 106 | private $authSigned = ''; |
||
| 107 | |||
| 108 | /** @var bool */ |
||
| 109 | private $identityAuthed = false; |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * @param int $dbId |
||
| 114 | * |
||
| 115 | * @return self |
||
| 116 | */ |
||
| 117 | public function setDbId(int $dbId): self { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return int |
||
| 125 | */ |
||
| 126 | public function getDbId(): int { |
||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $type |
||
| 133 | * |
||
| 134 | * @return $this |
||
| 135 | */ |
||
| 136 | public function setType(string $type): self { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public function getType(): string { |
||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function getIncoming(): string { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $incoming |
||
| 159 | * |
||
| 160 | * @return self |
||
| 161 | */ |
||
| 162 | public function setIncoming(string $incoming): self { |
||
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public function getEvent(): string { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param string $event |
||
| 178 | * |
||
| 179 | * @return self |
||
| 180 | */ |
||
| 181 | public function setEvent(string $event): self { |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * @param string $test |
||
| 190 | * |
||
| 191 | * @return RemoteInstance |
||
| 192 | */ |
||
| 193 | public function setTest(string $test): self { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | public function getTest(): string { |
||
| 205 | |||
| 206 | |||
| 207 | /** |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public function getCircles(): string { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string $circles |
||
| 216 | * |
||
| 217 | * @return self |
||
| 218 | */ |
||
| 219 | public function setCircles(string $circles): self { |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function getCircle(): string { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $circle |
||
| 235 | * |
||
| 236 | * @return self |
||
| 237 | */ |
||
| 238 | public function setCircle(string $circle): self { |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | public function getMembers(): string { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $members |
||
| 254 | * |
||
| 255 | * @return self |
||
| 256 | */ |
||
| 257 | public function setMembers(string $members): self { |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function getMember(): string { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param string $member |
||
| 273 | * |
||
| 274 | * @return self |
||
| 275 | */ |
||
| 276 | public function setMember(string $member): self { |
||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * @return $this |
||
| 285 | */ |
||
| 286 | public function setUidFromKey(): self { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $uid |
||
| 294 | * |
||
| 295 | * @return RemoteInstance |
||
| 296 | */ |
||
| 297 | public function setUid(string $uid): self { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param bool $shorten |
||
| 305 | * |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | public function getUid(bool $shorten = false): string { |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * @param string $authSigned |
||
| 319 | * |
||
| 320 | * @return RemoteInstance |
||
| 321 | */ |
||
| 322 | public function setAuthSigned(string $authSigned): self { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | public function getAuthSigned(): string { |
||
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * @param bool $identityAuthed |
||
| 338 | * |
||
| 339 | * @return RemoteInstance |
||
| 340 | */ |
||
| 341 | public function setIdentityAuthed(bool $identityAuthed): self { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @return bool |
||
| 349 | */ |
||
| 350 | public function isIdentityAuthed(): bool { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @throws RemoteUidException |
||
| 356 | */ |
||
| 357 | public function mustBeIdentityAuthed(): void { |
||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * @param array $data |
||
| 366 | * |
||
| 367 | * @return NC21Signatory |
||
|
|
|||
| 368 | */ |
||
| 369 | public function import(array $data): NC21Signatory { |
||
| 392 | |||
| 393 | |||
| 394 | /** |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | public function jsonSerialize(): array { |
||
| 415 | |||
| 416 | |||
| 417 | /** |
||
| 418 | * @param array $data |
||
| 419 | * |
||
| 420 | * @return self |
||
| 421 | */ |
||
| 422 | View Code Duplication | public function importFromDatabase(array $data): INC21QueryRow { |
|
| 432 | |||
| 433 | } |
||
| 434 | |||
| 435 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.