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 |
||
| 17 | class Changelog { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Content before the changelog itself. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $prologue = ''; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Content after the changelog itself. |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $epilogue = ''; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Changelog entries, one per version. |
||
| 35 | * |
||
| 36 | * @var ChangelogEntry[] |
||
| 37 | */ |
||
| 38 | protected $entries = array(); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Get the prologue content. |
||
| 42 | * |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | public function getPrologue() { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Set the prologue content. |
||
| 51 | * |
||
| 52 | * @param string $prologue Prologue content to set. |
||
| 53 | * @return $this |
||
| 54 | */ |
||
| 55 | public function setPrologue( $prologue ) { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get the epilogue content. |
||
| 62 | * |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | public function getEpilogue() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Set the epilogue content. |
||
| 71 | * |
||
| 72 | * @param string $epilogue Epilogue content to set. |
||
| 73 | * @return $this |
||
| 74 | */ |
||
| 75 | public function setEpilogue( $epilogue ) { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Get the list of changelog entries. |
||
| 82 | * |
||
| 83 | * @return ChangelogEntry[] |
||
| 84 | */ |
||
| 85 | public function getEntries() { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Set the list of changelog entries. |
||
| 91 | * |
||
| 92 | * This replaces all existing entries. |
||
| 93 | * |
||
| 94 | * @param ChangelogEntry[] $entries Changelog entries. |
||
| 95 | * @return $this |
||
| 96 | * @throws InvalidArgumentException If an argument is invalid. |
||
| 97 | */ |
||
| 98 | View Code Duplication | public function setEntries( array $entries ) { |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Get the latest changelog entry. |
||
| 111 | * |
||
| 112 | * @return ChangelogEntry|null |
||
| 113 | */ |
||
| 114 | public function getLatestEntry() { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Add a new entry as the latest. |
||
| 120 | * |
||
| 121 | * If no new entry object is provided, one will be created. The version |
||
| 122 | * number on the new entry is selected as follows. |
||
| 123 | * |
||
| 124 | * - If there are no existing entries, the new entry is version "0.0.1-dev". |
||
| 125 | * - If the latest existing entry has a version number ending in something |
||
| 126 | * like "-p1" (see `version_compare()`), the patch number is incremented. |
||
| 127 | * - Otherwise, "-p1" is appended. |
||
| 128 | * |
||
| 129 | * You'll probably want to chain to `ChangelogEntry::setVersion()` to |
||
| 130 | * replace that with something else. |
||
| 131 | * |
||
| 132 | * @param ChangelogEntry|null $entry New entry, or null to create an empty entry. |
||
| 133 | * @return ChangelogEntry The added entry. Always `$entry` if an entry was passed. |
||
| 134 | */ |
||
| 135 | public function addEntry( ChangelogEntry $entry = null ) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get the list of versions in the changelog. |
||
| 156 | * |
||
| 157 | * @return string[] |
||
| 158 | */ |
||
| 159 | public function getVersions() { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Find an entry by version. |
||
| 169 | * |
||
| 170 | * @param string $version Version to search for. |
||
| 171 | * @param string $operator Operator as for `version_compare()`. Note the |
||
| 172 | * passed `$version` is passed to `version_compare()` as the second |
||
| 173 | * argument, and the first entry matching is returned. |
||
| 174 | * @return ChangelogEntry|null |
||
| 175 | */ |
||
| 176 | public function findEntryByVersion( $version, $operator = '==' ) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Fetch all entries by a version check. |
||
| 187 | * |
||
| 188 | * @param array $constraints Version constraints. Keys are operations |
||
| 189 | * recognized by `version_compare()`, values are the version to compare |
||
| 190 | * with as the second argument. |
||
| 191 | * @return ChangelogEntry[] |
||
| 192 | */ |
||
| 193 | public function findEntriesByVersions( $constraints ) { |
||
| 205 | |||
| 206 | } |
||
| 207 |