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:
Complex classes like ChangelogEntry often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ChangelogEntry, and based on these observations, apply Extract Interface, too.
| 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|null |
||
| 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 | * The timestamp may be null, which should be interpreted as "unreleased". |
||
| 140 | * |
||
| 141 | * @return DateTime|null |
||
| 142 | */ |
||
| 143 | public function getTimestamp() { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Set the timestamp. |
||
| 149 | * |
||
| 150 | * The timestamp may be null, which should be interpreted as "unreleased". |
||
| 151 | * |
||
| 152 | * @param DateTime|string|null $timestamp Timestamp to set. |
||
| 153 | * @returns $this |
||
| 154 | * @throws InvalidArgumentException If an argument is invalid. |
||
| 155 | */ |
||
| 156 | View Code Duplication | public function setTimestamp( $timestamp ) { |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Get the prologue content. |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public function getPrologue() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Set the prologue content. |
||
| 179 | * |
||
| 180 | * @param string $prologue Prologue content to set. |
||
| 181 | * @return $this |
||
| 182 | */ |
||
| 183 | public function setPrologue( $prologue ) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get the epilogue content. |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function getEpilogue() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Set the epilogue content. |
||
| 199 | * |
||
| 200 | * @param string $epilogue Epilogue content to set. |
||
| 201 | * @return $this |
||
| 202 | */ |
||
| 203 | public function setEpilogue( $epilogue ) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get the list of changes. |
||
| 210 | * |
||
| 211 | * @return ChangeEntry[] |
||
| 212 | */ |
||
| 213 | public function getChanges() { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Set the list of changes. |
||
| 219 | * |
||
| 220 | * This replaces all existing changes. The caller is responsible |
||
| 221 | * for making sure the changes are sorted properly. |
||
| 222 | * |
||
| 223 | * @param ChangeEntry[] $changes Changes. |
||
| 224 | * @return $this |
||
| 225 | * @throws InvalidArgumentException If an argument is invalid. |
||
| 226 | */ |
||
| 227 | View Code Duplication | public function setChanges( array $changes ) { |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Add a new change. |
||
| 240 | * |
||
| 241 | * The new change is inserted before the first existing change where |
||
| 242 | * `ChangeEntry::compare()` says the existing change should come after. |
||
| 243 | * |
||
| 244 | * @param ChangeEntry $change New change. |
||
| 245 | * @param array $compareconfig Comparison config, see `ChangeEntry::compare()`. |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | public function insertChange( ChangeEntry $change, $compareconfig = array() ) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Append a new change. |
||
| 261 | * |
||
| 262 | * @param ChangeEntry $change New change. |
||
| 263 | * @return $this |
||
| 264 | */ |
||
| 265 | public function appendChange( ChangeEntry $change ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Get the changes grouped by subheading. |
||
| 272 | * |
||
| 273 | * @param string|null $subheading Subheading to retrieve. |
||
| 274 | * @return ChangeEntry[]|ChangeEntry[][] An array of changes with the |
||
| 275 | * heading if `$subheading` was passed, or an array keyed by subheading of |
||
| 276 | * arrays of changes if it was null. |
||
| 277 | */ |
||
| 278 | public function getChangesBySubheading( $subheading = null ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Return data for serializing to JSON. |
||
| 291 | * |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | public function jsonSerialize() { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Unserialize from JSON. |
||
| 308 | * |
||
| 309 | * @param array $data JSON data as returned by self::jsonSerialize(). |
||
| 310 | * @return static |
||
| 311 | * @throws InvalidArgumentException If the data is invalid. |
||
| 312 | */ |
||
| 313 | public static function jsonUnserialize( $data ) { |
||
| 329 | |||
| 330 | } |
||
| 331 |