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