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 Config { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Default config settings. |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | private static $defaultConfig = array( |
||
| 25 | 'changelog' => 'CHANGELOG.md', |
||
| 26 | 'changes-dir' => 'changelog', |
||
| 27 | 'formatter' => 'keepachangelog', |
||
| 28 | 'types' => array( |
||
| 29 | 'security' => 'Security', |
||
| 30 | 'added' => 'Added', |
||
| 31 | 'changed' => 'Changed', |
||
| 32 | 'deprecated' => 'Deprecated', |
||
| 33 | 'removed' => 'Removed', |
||
| 34 | 'fixed' => 'Fixed', |
||
| 35 | ), |
||
| 36 | 'versioning' => 'semver', |
||
| 37 | ); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Active config settings. |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private static $config = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Cached config settings. |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | private static $cache = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Whether `load()` was called already. |
||
| 55 | * |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | private static $loaded = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * OutputInterface. |
||
| 62 | * |
||
| 63 | * @var OutputInterface|null |
||
| 64 | */ |
||
| 65 | private static $out; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Set the OutputInterface. |
||
| 69 | * |
||
| 70 | * @param OutputInterface $out OutputInterface. |
||
| 71 | */ |
||
| 72 | public static function setOutput( OutputInterface $out ) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Load the configuration. |
||
| 78 | * |
||
| 79 | * @throws \LogicException If called before `setOutput()`. |
||
| 80 | * @throws \DomainException If the path to composer.json exists but can't be `realpath`-ed. |
||
| 81 | */ |
||
| 82 | private static function load() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get the base directory. |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public static function base() { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Add the base directory to a path, if necessary. |
||
| 133 | * |
||
| 134 | * @param string $path Path. |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | private static function addBase( $path ) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Get the changelog filename. |
||
| 147 | * |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | View Code Duplication | public static function changelogFile() { |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Get the changes directory. |
||
| 160 | * |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | View Code Duplication | public static function changesDir() { |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Get change types. |
||
| 173 | * |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | public static function types() { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get a plugin. |
||
| 189 | * |
||
| 190 | * @param string|array $config Plugin name or configuration array. |
||
| 191 | * @param string $suffix Plugin class suffix. |
||
| 192 | * @return object|null Object, or null if the plugin was not found. |
||
| 193 | */ |
||
| 194 | private static function getPlugin( $config, $suffix ) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get formatting plugin. |
||
| 222 | * |
||
| 223 | * @return Formatter |
||
| 224 | * @throws \RuntimeException If the configured formatter is unknown. |
||
| 225 | */ |
||
| 226 | View Code Duplication | public static function formatterPlugin() { |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Get verisoning plugin. |
||
| 241 | * |
||
| 242 | * @return Versioning |
||
| 243 | * @throws \RuntimeException If the configured versioning plugin is unknown. |
||
| 244 | */ |
||
| 245 | View Code Duplication | public static function versioningPlugin() { |
|
| 257 | |||
| 258 | } |
||
| 259 |