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 |
||
| 11 | class Attachment extends DataObject |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Creates a new message attachment. |
||
| 15 | * |
||
| 16 | * @param string $title The attachment title. |
||
| 17 | * @param string $text The attachment body text. |
||
| 18 | * @param string $fallback A plain-text summary of the attachment. |
||
| 19 | */ |
||
| 20 | 4 | public function __construct($title, $text, $fallback = null, $color = null, $pretext = null, array $fields = []) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Gets a plain-text summary of the attachment. |
||
| 32 | * |
||
| 33 | * @return string A plain-text summary of the attachment. |
||
| 34 | */ |
||
| 35 | 1 | public function getFallbackText() |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Gets the attachment border color. |
||
| 42 | * |
||
| 43 | * @return string The attachment border color. Can be "good", "warning", "danger", or a hex color code. |
||
| 44 | */ |
||
| 45 | 1 | public function getColor() |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Gets the attachment pretext. |
||
| 52 | * |
||
| 53 | * @return string Optional text that appears above the message attachment block. |
||
| 54 | */ |
||
| 55 | 1 | public function getPretext() |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Gets the author name. |
||
| 62 | * |
||
| 63 | * @return string The attachment author's name. |
||
| 64 | */ |
||
| 65 | 1 | public function getAuthorName() |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Gets the author link. |
||
| 72 | * |
||
| 73 | * @return string A link URL for the author. |
||
| 74 | */ |
||
| 75 | 1 | public function getAuthorLink() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Gets the author icon. |
||
| 82 | * |
||
| 83 | * @return string An icon URL to show next to the author name. |
||
| 84 | */ |
||
| 85 | 1 | public function getAuthorIcon() |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Gets the title. |
||
| 92 | * |
||
| 93 | * @return string The attachment title. |
||
| 94 | */ |
||
| 95 | 1 | public function getTitle() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Gets the title link. |
||
| 102 | * |
||
| 103 | * @return string A link URL the title should link to. |
||
| 104 | */ |
||
| 105 | 1 | public function getTitleLink() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Gets the attachment body text. |
||
| 112 | * |
||
| 113 | * @return string The attachment body text. |
||
| 114 | */ |
||
| 115 | 2 | public function getText() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Gets the image URL. |
||
| 122 | * |
||
| 123 | * @return string A URL to an image to display in the attachment body. |
||
| 124 | */ |
||
| 125 | 1 | public function getImageUrl() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Gets the thumbnail URL. |
||
| 132 | * |
||
| 133 | * @return string A URL to an image to display as a thumbnail. |
||
| 134 | */ |
||
| 135 | 1 | public function getThumbUrl() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Gets the footer text. |
||
| 142 | * |
||
| 143 | * @return string The footer text. |
||
| 144 | */ |
||
| 145 | public function getFooterText() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Gets a URL to an image to show to the left of the footer text. |
||
| 152 | * |
||
| 153 | * @return string The footer icon URL. |
||
| 154 | */ |
||
| 155 | public function getFooterIcon() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Gets an extra timestamp value in the footer. |
||
| 162 | * |
||
| 163 | * @return \DateTime The time of the timestamp. |
||
| 164 | */ |
||
| 165 | 1 | public function getTimestamp() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Checks if the attachment has fields. |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | 1 | public function hasFields() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Gets all the attachment's fields. |
||
| 188 | * |
||
| 189 | * @return AttachmentField[] |
||
| 190 | */ |
||
| 191 | 1 | public function getFields() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritDoc} |
||
| 198 | */ |
||
| 199 | 15 | View Code Duplication | public function jsonUnserialize(array $data) |
| 209 | } |
||
| 210 |
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: