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 ChangeEntry implements JsonSerializable { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Entry significance. |
||
| 23 | * |
||
| 24 | * @var string|null |
||
| 25 | */ |
||
| 26 | protected $significance = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Entry timestamp. |
||
| 30 | * |
||
| 31 | * @var DateTime |
||
| 32 | */ |
||
| 33 | protected $timestamp; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Subheading the entry is under. |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $subheading = ''; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Author of the entry. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $author = ''; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Content of the entry. |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $content = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Constructor. |
||
| 58 | * |
||
| 59 | * @param array $data Data for entry fields. Keys correspond to the setters, e.g. key 'content' calls `setContent()`. |
||
| 60 | * @throws InvalidArgumentException If an argument is invalid. |
||
| 61 | */ |
||
| 62 | View Code Duplication | public function __construct( array $data = array() ) { |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Compare two ChangeEntry objects. |
||
| 76 | * |
||
| 77 | * @param ChangeEntry $a First ChangeEntry. |
||
| 78 | * @param ChangeEntry $b Second ChangeEntry. |
||
| 79 | * @param array $config Comparison configuration. Keys are: |
||
| 80 | * - ordering: (string[]) Order in which to consider the fields. Field |
||
| 81 | * names correspond to getters, e.g. 'significance' => |
||
| 82 | * `getSignificance()`. Default ordering is subheading, content. |
||
| 83 | * - knownSubheadings: (string[]) Change entries with these headings will |
||
| 84 | * compare, in this order, after those with no subheading and before any |
||
| 85 | * other subheadings. |
||
| 86 | * @return int <0 if $a should come before $b, >0 if $b should come before $a, or 0 otherwise. |
||
| 87 | * @throws InvalidArgumentException If an argument is invalid. |
||
| 88 | */ |
||
| 89 | public static function compare( ChangeEntry $a, ChangeEntry $b, array $config = array() ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get the significance. |
||
| 120 | * |
||
| 121 | * @return string|null |
||
| 122 | */ |
||
| 123 | public function getSignificance() { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Set the significance. |
||
| 129 | * |
||
| 130 | * @param string|null $significance 'patch', 'minor', or 'major'. |
||
| 131 | * @returns $this |
||
| 132 | * @throws InvalidArgumentException If an argument is invalid. |
||
| 133 | */ |
||
| 134 | public function setSignificance( $significance ) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Compare significance values. |
||
| 144 | * |
||
| 145 | * @param ChangeEntry $a First entry. |
||
| 146 | * @param ChangeEntry $b Second entry. |
||
| 147 | * @param array $config Unused. |
||
| 148 | * @return int |
||
| 149 | */ |
||
| 150 | protected static function compareSignificance( ChangeEntry $a, ChangeEntry $b, array $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Get the timestamp. |
||
| 159 | * |
||
| 160 | * @return DateTime |
||
| 161 | */ |
||
| 162 | public function getTimestamp() { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Set the timestamp. |
||
| 168 | * |
||
| 169 | * @param DateTime|string $timestamp Timestamp to set. |
||
| 170 | * @returns $this |
||
| 171 | * @throws InvalidArgumentException If an argument is invalid. |
||
| 172 | */ |
||
| 173 | View Code Duplication | public function setTimestamp( $timestamp ) { |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Compare timestamps. |
||
| 187 | * |
||
| 188 | * @param ChangeEntry $a First entry. |
||
| 189 | * @param ChangeEntry $b Second entry. |
||
| 190 | * @param array $config Unused. |
||
| 191 | * @return int |
||
| 192 | */ |
||
| 193 | protected static function compareTimestamp( ChangeEntry $a, ChangeEntry $b, array $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get the subheading. |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function getSubheading() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Set the subheading. |
||
| 210 | * |
||
| 211 | * @param string $subheading Subheading to set. |
||
| 212 | * @returns $this |
||
| 213 | */ |
||
| 214 | public function setSubheading( $subheading ) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Compare subheadings. |
||
| 221 | * |
||
| 222 | * @param ChangeEntry $a First entry. |
||
| 223 | * @param ChangeEntry $b Second entry. |
||
| 224 | * @param array $config Used for 'knownSubheadings'. |
||
| 225 | * @return int |
||
| 226 | */ |
||
| 227 | protected static function compareSubheading( ChangeEntry $a, ChangeEntry $b, array $config ) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get the author. |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | public function getAuthor() { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Set the author. |
||
| 270 | * |
||
| 271 | * @param string $author Author to set. |
||
| 272 | * @returns $this |
||
| 273 | */ |
||
| 274 | public function setAuthor( $author ) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get the content. |
||
| 281 | * |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function getContent() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Set the content. |
||
| 290 | * |
||
| 291 | * @param string $content Content to set. |
||
| 292 | * @returns $this |
||
| 293 | */ |
||
| 294 | public function setContent( $content ) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Return data for serializing to JSON. |
||
| 301 | * |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | public function jsonSerialize() { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Unserialize from JSON. |
||
| 317 | * |
||
| 318 | * @param array $data JSON data as returned by self::jsonSerialize(). |
||
| 319 | * @return static |
||
| 320 | * @throws InvalidArgumentException If the data is invalid. |
||
| 321 | */ |
||
| 322 | public static function jsonUnserialize( $data ) { |
||
| 334 | |||
| 335 | } |
||
| 336 |