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 Changelog implements JsonSerializable { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Content before the changelog itself. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $prologue = ''; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Content after the changelog itself. |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $epilogue = ''; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Changelog entries, one per version. |
||
| 36 | * |
||
| 37 | * @var ChangelogEntry[] |
||
| 38 | */ |
||
| 39 | protected $entries = array(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Get the prologue content. |
||
| 43 | * |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | public function getPrologue() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Set the prologue content. |
||
| 52 | * |
||
| 53 | * @param string $prologue Prologue content to set. |
||
| 54 | * @return $this |
||
| 55 | */ |
||
| 56 | public function setPrologue( $prologue ) { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get the epilogue content. |
||
| 63 | * |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public function getEpilogue() { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Set the epilogue content. |
||
| 72 | * |
||
| 73 | * @param string $epilogue Epilogue content to set. |
||
| 74 | * @return $this |
||
| 75 | */ |
||
| 76 | public function setEpilogue( $epilogue ) { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get the list of changelog entries. |
||
| 83 | * |
||
| 84 | * @return ChangelogEntry[] |
||
| 85 | */ |
||
| 86 | public function getEntries() { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Set the list of changelog entries. |
||
| 92 | * |
||
| 93 | * This replaces all existing entries. |
||
| 94 | * |
||
| 95 | * @param ChangelogEntry[] $entries Changelog entries. |
||
| 96 | * @return $this |
||
| 97 | * @throws InvalidArgumentException If an argument is invalid. |
||
| 98 | */ |
||
| 99 | View Code Duplication | public function setEntries( array $entries ) { |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Get the latest changelog entry. |
||
| 112 | * |
||
| 113 | * @return ChangelogEntry|null |
||
| 114 | */ |
||
| 115 | public function getLatestEntry() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Add a new entry as the latest. |
||
| 121 | * |
||
| 122 | * @param ChangelogEntry $entry New entry. |
||
| 123 | * @return $this |
||
| 124 | */ |
||
| 125 | public function addEntry( ChangelogEntry $entry ) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get the list of versions in the changelog. |
||
| 132 | * |
||
| 133 | * @return string[] |
||
| 134 | */ |
||
| 135 | public function getVersions() { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Return data for serializing to JSON. |
||
| 145 | * |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | public function jsonSerialize() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Unserialize from JSON. |
||
| 159 | * |
||
| 160 | * @param array $data JSON data as returned by self::jsonSerialize(). |
||
| 161 | * @return static |
||
| 162 | * @throws InvalidArgumentException If the data is invalid. |
||
| 163 | */ |
||
| 164 | public static function jsonUnserialize( $data ) { |
||
| 186 | |||
| 187 | } |
||
| 188 |