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 | 'link-template' => null, |
||
| 28 | 'ordering' => array( 'subheading', 'content' ), |
||
| 29 | 'formatter' => 'keepachangelog', |
||
| 30 | 'types' => array( |
||
| 31 | 'security' => 'Security', |
||
| 32 | 'added' => 'Added', |
||
| 33 | 'changed' => 'Changed', |
||
| 34 | 'deprecated' => 'Deprecated', |
||
| 35 | 'removed' => 'Removed', |
||
| 36 | 'fixed' => 'Fixed', |
||
| 37 | ), |
||
| 38 | 'versioning' => 'semver', |
||
| 39 | ); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Active config settings. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private static $config = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Cached config settings. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private static $cache = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Whether `load()` was called already. |
||
| 57 | * |
||
| 58 | * @var bool |
||
| 59 | */ |
||
| 60 | private static $loaded = false; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Location of composer.json, overriding any COMPOSER environment variable. |
||
| 64 | * |
||
| 65 | * @var string|null |
||
| 66 | */ |
||
| 67 | private static $composerJsonPath; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Set the location of composer.json, overriding auto-detection. |
||
| 71 | * |
||
| 72 | * @since 1.2.0 |
||
| 73 | * @param string|null $path Path to composer.json, or null to re-enable auto-detection. |
||
| 74 | */ |
||
| 75 | public static function setComposerJsonPath( $path ) { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Former method used to set an OutputInterface. No longer used. |
||
| 81 | * |
||
| 82 | * @deprecated since 1.2.0, no longer needed. |
||
| 83 | * @param OutputInterface $out Ignored. |
||
| 84 | */ |
||
| 85 | public static function setOutput( OutputInterface $out ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Load the configuration. |
||
| 90 | * |
||
| 91 | * @throws \DomainException If the path to composer.json exists but can't be `realpath`-ed. |
||
| 92 | * @throws ConfigException If composer.json is not found or is invalid (since 1.2.0). |
||
| 93 | */ |
||
| 94 | private static function load() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get the base directory. |
||
| 134 | * |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | public static function base() { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Add the base directory to a path, if necessary. |
||
| 144 | * |
||
| 145 | * @param string $path Path. |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | private static function addBase( $path ) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Get the changelog filename. |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | View Code Duplication | public static function changelogFile() { |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Get the changes directory. |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | View Code Duplication | public static function changesDir() { |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Get the link. |
||
| 184 | * |
||
| 185 | * @param string $old Old version number. |
||
| 186 | * @param string $new New version number. |
||
| 187 | * @return string|null |
||
| 188 | */ |
||
| 189 | public static function link( $old, $new ) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get change entry ordering. |
||
| 205 | * |
||
| 206 | * @return string[] |
||
| 207 | */ |
||
| 208 | public static function ordering() { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get change types. |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public static function types() { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get a plugin. |
||
| 234 | * |
||
| 235 | * @param string|array $config Plugin name or configuration array. |
||
| 236 | * @param string $suffix Plugin class suffix. |
||
| 237 | * @param string $interface Expected interface name. |
||
| 238 | * @return object|null Object, or null if the plugin was not found. |
||
| 239 | */ |
||
| 240 | private static function getPlugin( $config, $suffix, $interface ) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get formatting plugin. |
||
| 273 | * |
||
| 274 | * @return Formatter |
||
| 275 | * @throws \RuntimeException If the configured formatter is unknown. |
||
| 276 | */ |
||
| 277 | View Code Duplication | public static function formatterPlugin() { |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Get verisoning plugin. |
||
| 292 | * |
||
| 293 | * @return Versioning |
||
| 294 | * @throws \RuntimeException If the configured versioning plugin is unknown. |
||
| 295 | */ |
||
| 296 | View Code Duplication | public static function versioningPlugin() { |
|
| 308 | |||
| 309 | } |
||
| 310 |