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 | * OutputInterface. |
||
64 | * |
||
65 | * @var OutputInterface|null |
||
66 | */ |
||
67 | private static $out; |
||
68 | |||
69 | /** |
||
70 | * Set the OutputInterface. |
||
71 | * |
||
72 | * @param OutputInterface $out OutputInterface. |
||
73 | */ |
||
74 | public static function setOutput( OutputInterface $out ) { |
||
77 | |||
78 | /** |
||
79 | * Load the configuration. |
||
80 | * |
||
81 | * @throws \LogicException If called before `setOutput()`. |
||
82 | * @throws \DomainException If the path to composer.json exists but can't be `realpath`-ed. |
||
83 | */ |
||
84 | private static function load() { |
||
122 | |||
123 | /** |
||
124 | * Get the base directory. |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | public static function base() { |
||
132 | |||
133 | /** |
||
134 | * Add the base directory to a path, if necessary. |
||
135 | * |
||
136 | * @param string $path Path. |
||
137 | * @return string |
||
138 | */ |
||
139 | private static function addBase( $path ) { |
||
146 | |||
147 | /** |
||
148 | * Get the changelog filename. |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | View Code Duplication | public static function changelogFile() { |
|
159 | |||
160 | /** |
||
161 | * Get the changes directory. |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | View Code Duplication | public static function changesDir() { |
|
172 | |||
173 | /** |
||
174 | * Get the link. |
||
175 | * |
||
176 | * @param string $old Old version number. |
||
177 | * @param string $new New version number. |
||
178 | * @return string|null |
||
179 | */ |
||
180 | public static function link( $old, $new ) { |
||
193 | |||
194 | /** |
||
195 | * Get change entry ordering. |
||
196 | * |
||
197 | * @return string[] |
||
198 | */ |
||
199 | public static function ordering() { |
||
206 | |||
207 | /** |
||
208 | * Get change types. |
||
209 | * |
||
210 | * @return array |
||
211 | */ |
||
212 | public static function types() { |
||
222 | |||
223 | /** |
||
224 | * Get a plugin. |
||
225 | * |
||
226 | * @param string|array $config Plugin name or configuration array. |
||
227 | * @param string $suffix Plugin class suffix. |
||
228 | * @param string $interface Expected interface name. |
||
229 | * @return object|null Object, or null if the plugin was not found. |
||
230 | */ |
||
231 | private static function getPlugin( $config, $suffix, $interface ) { |
||
261 | |||
262 | /** |
||
263 | * Get formatting plugin. |
||
264 | * |
||
265 | * @return Formatter |
||
266 | * @throws \RuntimeException If the configured formatter is unknown. |
||
267 | */ |
||
268 | View Code Duplication | public static function formatterPlugin() { |
|
280 | |||
281 | /** |
||
282 | * Get verisoning plugin. |
||
283 | * |
||
284 | * @return Versioning |
||
285 | * @throws \RuntimeException If the configured versioning plugin is unknown. |
||
286 | */ |
||
287 | View Code Duplication | public static function versioningPlugin() { |
|
299 | |||
300 | } |
||
301 |