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 |
||
| 9 | class Message extends ClientObject |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Gets the message text. |
||
| 13 | * |
||
| 14 | * @return string |
||
| 15 | */ |
||
| 16 | 2 | public function getText() |
|
| 20 | |||
| 21 | /** |
||
| 22 | * Checks if Markdown is enabled for the message text. |
||
| 23 | * |
||
| 24 | * @return bool |
||
| 25 | */ |
||
| 26 | public function isMarkdownEnabled() |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Checks if the message has attachments. |
||
| 33 | * |
||
| 34 | * @return bool True if the message has attachments, otherwise false. |
||
| 35 | */ |
||
| 36 | 3 | public function hasAttachments() |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Gets all message attachments. |
||
| 43 | * |
||
| 44 | * @return Attachment[] |
||
| 45 | */ |
||
| 46 | 2 | public function getAttachments() |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Gets the channel the message is in. |
||
| 53 | * |
||
| 54 | * @return \React\Promise\PromiseInterface |
||
| 55 | */ |
||
| 56 | 2 | public function getChannel() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Gets the user that sent the message. |
||
| 63 | * |
||
| 64 | * @return \React\Promise\PromiseInterface |
||
| 65 | */ |
||
| 66 | 2 | public function getUser() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Send message as user or not. |
||
| 73 | * By default sends as user. |
||
| 74 | * |
||
| 75 | * @return bool |
||
| 76 | */ |
||
| 77 | public function isAsUser() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get message's user display name |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public function getUsername() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritDoc} |
||
| 94 | */ |
||
| 95 | View Code Duplication | public function jsonUnserialize(array $data) |
|
| 105 | } |
||
| 106 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: