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 // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase |
||
| 18 | class ChangelogEntry { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Entry version. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $version; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Entry link. |
||
| 29 | * |
||
| 30 | * @var string|null |
||
| 31 | */ |
||
| 32 | protected $link = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Entry timestamp. |
||
| 36 | * |
||
| 37 | * @var DateTime |
||
| 38 | */ |
||
| 39 | protected $timestamp; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Content before the changes themselves. |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $prologue = ''; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Content after the changes themselves. |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $epilogue = ''; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Changes. |
||
| 57 | * |
||
| 58 | * @var ChangeEntry[] |
||
| 59 | */ |
||
| 60 | protected $changes = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Constructor. |
||
| 64 | * |
||
| 65 | * @param string $version Version for the new entry. |
||
| 66 | * @param array $data Data for other entry fields. Keys correspond to the setters, e.g. key 'link' calls `setLink()`. |
||
| 67 | * @throws InvalidArgumentException If an argument is invalid. |
||
| 68 | */ |
||
| 69 | View Code Duplication | public function __construct( $version, array $data = array() ) { |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Get the version. |
||
| 83 | * |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | public function getVersion() { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Set the version. |
||
| 92 | * |
||
| 93 | * @param string $version Version to set. |
||
| 94 | * @returns $this |
||
| 95 | * @throws InvalidArgumentException If an argument is invalid. |
||
| 96 | */ |
||
| 97 | public function setVersion( $version ) { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get the link. |
||
| 108 | * |
||
| 109 | * @return string|null |
||
| 110 | */ |
||
| 111 | public function getLink() { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Set the link. |
||
| 117 | * |
||
| 118 | * @param string|null $link Link to set. |
||
| 119 | * @returns $this |
||
| 120 | * @throws InvalidArgumentException If an argument is invalid. |
||
| 121 | */ |
||
| 122 | public function setLink( $link ) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get the timestamp. |
||
| 137 | * |
||
| 138 | * @return DateTime |
||
| 139 | */ |
||
| 140 | public function getTimestamp() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Set the timestamp. |
||
| 146 | * |
||
| 147 | * @param DateTime|string $timestamp Timestamp to set. |
||
| 148 | * @returns $this |
||
| 149 | * @throws InvalidArgumentException If an argument is invalid. |
||
| 150 | */ |
||
| 151 | View Code Duplication | public function setTimestamp( $timestamp ) { |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Get the prologue content. |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function getPrologue() { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Set the prologue content. |
||
| 174 | * |
||
| 175 | * @param string $prologue Prologue content to set. |
||
| 176 | * @return $this |
||
| 177 | */ |
||
| 178 | public function setPrologue( $prologue ) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get the epilogue content. |
||
| 185 | * |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | public function getEpilogue() { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Set the epilogue content. |
||
| 194 | * |
||
| 195 | * @param string $epilogue Epilogue content to set. |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | public function setEpilogue( $epilogue ) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get the list of changes. |
||
| 205 | * |
||
| 206 | * @return ChangeEntry[] |
||
| 207 | */ |
||
| 208 | public function getChanges() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Set the list of changes. |
||
| 214 | * |
||
| 215 | * This replaces all existing changes. The caller is responsible |
||
| 216 | * for making sure the changes are sorted properly. |
||
| 217 | * |
||
| 218 | * @param ChangeEntry[] $changes Changes. |
||
| 219 | * @return $this |
||
| 220 | * @throws InvalidArgumentException If an argument is invalid. |
||
| 221 | */ |
||
| 222 | View Code Duplication | public function setChanges( array $changes ) { |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Add a new change. |
||
| 235 | * |
||
| 236 | * The new change is inserted before the first existing change where |
||
| 237 | * `ChangeEntry::compare()` says the existing change should come after. |
||
| 238 | * |
||
| 239 | * @param ChangeEntry $change New change. |
||
| 240 | * @param array $compareconfig Comparison config, see `ChangeEntry::compare()`. |
||
| 241 | * @return $this |
||
| 242 | */ |
||
| 243 | public function insertChange( ChangeEntry $change, $compareconfig = array() ) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Append a new change. |
||
| 256 | * |
||
| 257 | * @param ChangeEntry $change New change. |
||
| 258 | * @return $this |
||
| 259 | */ |
||
| 260 | public function appendChange( ChangeEntry $change ) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get the changes grouped by subheading. |
||
| 267 | * |
||
| 268 | * @param string|null $subheading Subheading to retrieve. |
||
| 269 | * @return ChangeEntry[]|ChangeEntry[][] An array of changes with the |
||
| 270 | * heading if `$subheading` was passed, or an array keyed by subheading of |
||
| 271 | * arrays of changes if it was null. |
||
| 272 | */ |
||
| 273 | public function getChangesBySubheading( $subheading = null ) { |
||
| 283 | |||
| 284 | } |
||
| 285 |