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:
Complex classes like Config often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Config, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
81 | class Config |
||
82 | { |
||
83 | const PULI_DIR = 'puli-dir'; |
||
84 | |||
85 | const BOOTSTRAP_FILE = 'bootstrap-file'; |
||
86 | |||
87 | const FACTORY = 'factory'; |
||
88 | |||
89 | const FACTORY_AUTO_GENERATE = 'factory.auto-generate'; |
||
90 | |||
91 | const FACTORY_IN = 'factory.in'; |
||
92 | |||
93 | const FACTORY_IN_CLASS = 'factory.in.class'; |
||
94 | |||
95 | const FACTORY_IN_FILE = 'factory.in.file'; |
||
96 | |||
97 | const FACTORY_OUT = 'factory.out'; |
||
98 | |||
99 | const FACTORY_OUT_CLASS = 'factory.out.class'; |
||
100 | |||
101 | const FACTORY_OUT_FILE = 'factory.out.file'; |
||
102 | |||
103 | const REPOSITORY = 'repository'; |
||
104 | |||
105 | const REPOSITORY_TYPE = 'repository.type'; |
||
106 | |||
107 | const REPOSITORY_PATH = 'repository.path'; |
||
108 | |||
109 | const REPOSITORY_SYMLINK = 'repository.symlink'; |
||
110 | |||
111 | const REPOSITORY_OPTIMIZE = 'repository.optimize'; |
||
112 | |||
113 | const REPOSITORY_STORE = 'repository.store'; |
||
114 | |||
115 | const REPOSITORY_STORE_TYPE = 'repository.store.type'; |
||
116 | |||
117 | const REPOSITORY_STORE_PATH = 'repository.store.path'; |
||
118 | |||
119 | const REPOSITORY_STORE_HOST = 'repository.store.host'; |
||
120 | |||
121 | const REPOSITORY_STORE_PORT = 'repository.store.port'; |
||
122 | |||
123 | const REPOSITORY_STORE_BUCKET = 'repository.store.bucket'; |
||
124 | |||
125 | const REPOSITORY_STORE_CACHE = 'repository.store.cache'; |
||
126 | |||
127 | const CHANGE_STREAM = 'change-stream'; |
||
128 | |||
129 | const CHANGE_STREAM_TYPE = 'change-stream.type'; |
||
130 | |||
131 | const CHANGE_STREAM_PATH = 'change-stream.path'; |
||
132 | |||
133 | const CHANGE_STREAM_STORE = 'change-stream.store'; |
||
134 | |||
135 | const CHANGE_STREAM_STORE_TYPE = 'change-stream.store.type'; |
||
136 | |||
137 | const CHANGE_STREAM_STORE_PATH = 'change-stream.store.path'; |
||
138 | |||
139 | const CHANGE_STREAM_STORE_HOST = 'change-stream.store.host'; |
||
140 | |||
141 | const CHANGE_STREAM_STORE_PORT = 'change-stream.store.port'; |
||
142 | |||
143 | const CHANGE_STREAM_STORE_BUCKET = 'change-stream.store.bucket'; |
||
144 | |||
145 | const CHANGE_STREAM_STORE_CACHE = 'change-stream.store.cache'; |
||
146 | |||
147 | const DISCOVERY = 'discovery'; |
||
148 | |||
149 | const DISCOVERY_TYPE = 'discovery.type'; |
||
150 | |||
151 | const DISCOVERY_PATH = 'discovery.path'; |
||
152 | |||
153 | const DISCOVERY_STORE = 'discovery.store'; |
||
154 | |||
155 | const DISCOVERY_STORE_TYPE = 'discovery.store.type'; |
||
156 | |||
157 | const DISCOVERY_STORE_PATH = 'discovery.store.path'; |
||
158 | |||
159 | const DISCOVERY_STORE_HOST = 'discovery.store.host'; |
||
160 | |||
161 | const DISCOVERY_STORE_PORT = 'discovery.store.port'; |
||
162 | |||
163 | const DISCOVERY_STORE_BUCKET = 'discovery.store.bucket'; |
||
164 | |||
165 | const DISCOVERY_STORE_CACHE = 'discovery.store.cache'; |
||
166 | |||
167 | /** |
||
168 | * The accepted config keys. |
||
169 | * |
||
170 | * @var bool[] |
||
171 | */ |
||
172 | private static $keys = array( |
||
173 | self::PULI_DIR => true, |
||
174 | self::BOOTSTRAP_FILE => true, |
||
175 | self::FACTORY_AUTO_GENERATE => true, |
||
176 | self::FACTORY_IN_CLASS => true, |
||
177 | self::FACTORY_IN_FILE => true, |
||
178 | self::FACTORY_OUT_CLASS => true, |
||
179 | self::FACTORY_OUT_FILE => true, |
||
180 | self::REPOSITORY_TYPE => true, |
||
181 | self::REPOSITORY_PATH => true, |
||
182 | self::REPOSITORY_SYMLINK => true, |
||
183 | self::REPOSITORY_OPTIMIZE => true, |
||
184 | self::REPOSITORY_STORE_TYPE => true, |
||
185 | self::REPOSITORY_STORE_PATH => true, |
||
186 | self::REPOSITORY_STORE_HOST => true, |
||
187 | self::REPOSITORY_STORE_PORT => true, |
||
188 | self::REPOSITORY_STORE_BUCKET => true, |
||
189 | self::REPOSITORY_STORE_CACHE => true, |
||
190 | self::CHANGE_STREAM_TYPE => true, |
||
191 | self::CHANGE_STREAM_PATH => true, |
||
192 | self::CHANGE_STREAM_STORE_TYPE => true, |
||
193 | self::CHANGE_STREAM_STORE_PATH => true, |
||
194 | self::CHANGE_STREAM_STORE_HOST => true, |
||
195 | self::CHANGE_STREAM_STORE_PORT => true, |
||
196 | self::CHANGE_STREAM_STORE_BUCKET => true, |
||
197 | self::CHANGE_STREAM_STORE_CACHE => true, |
||
198 | self::DISCOVERY_TYPE => true, |
||
199 | self::DISCOVERY_PATH => true, |
||
200 | self::DISCOVERY_STORE_TYPE => true, |
||
201 | self::DISCOVERY_STORE_PATH => true, |
||
202 | self::DISCOVERY_STORE_HOST => true, |
||
203 | self::DISCOVERY_STORE_PORT => true, |
||
204 | self::DISCOVERY_STORE_BUCKET => true, |
||
205 | self::DISCOVERY_STORE_CACHE => true, |
||
206 | ); |
||
207 | |||
208 | private static $compositeKeys = array( |
||
209 | self::FACTORY => true, |
||
210 | self::FACTORY_IN => true, |
||
211 | self::FACTORY_OUT => true, |
||
212 | self::REPOSITORY => true, |
||
213 | self::REPOSITORY_STORE => true, |
||
214 | self::CHANGE_STREAM => true, |
||
215 | self::CHANGE_STREAM_STORE => true, |
||
216 | self::DISCOVERY => true, |
||
217 | self::DISCOVERY_STORE => true, |
||
218 | ); |
||
219 | |||
220 | /** |
||
221 | * The configuration values. |
||
222 | * |
||
223 | * @var array |
||
224 | */ |
||
225 | private $values = array(); |
||
226 | |||
227 | /** |
||
228 | * The configuration to fall back to. |
||
229 | * |
||
230 | * @var Config |
||
231 | */ |
||
232 | private $baseConfig; |
||
233 | |||
234 | /** |
||
235 | * Returns all valid configuration keys. |
||
236 | * |
||
237 | * @return string[] The config keys. |
||
|
|||
238 | */ |
||
239 | 20 | public static function getKeys() |
|
243 | |||
244 | /** |
||
245 | * Creates a new configuration. |
||
246 | * |
||
247 | * @param Config|null $baseConfig The configuration to fall back to if a value is |
||
248 | * not set in here. |
||
249 | * @param array $values The values to initially set in the configuration. |
||
250 | */ |
||
251 | 726 | public function __construct(Config $baseConfig = null, array $values = array()) |
|
257 | |||
258 | /** |
||
259 | * Returns the base configuration. |
||
260 | * |
||
261 | * @return Config The base configuration or `null` if none is set. |
||
262 | */ |
||
263 | public function getBaseConfig() |
||
267 | |||
268 | /** |
||
269 | * Returns the value of a configuration key. |
||
270 | * |
||
271 | * If fallback is enabled, the value of the base configuration is returned |
||
272 | * if the key was not set. |
||
273 | * |
||
274 | * You can also pass a default value in the second parameter. This default |
||
275 | * value is returned if the configuration key was neither found in this nor |
||
276 | * in its fallback configuration. |
||
277 | * |
||
278 | * @param string $key The configuration key. |
||
279 | * @param mixed $default The value to return if the key was not set. |
||
280 | * @param bool $fallback Whether to return the value of the base |
||
281 | * configuration if the key was not set. |
||
282 | * |
||
283 | * @return mixed The value of the configuration key. |
||
284 | * |
||
285 | * @throws NoSuchConfigKeyException If the configuration key is invalid. |
||
286 | */ |
||
287 | 134 | public function get($key, $default = null, $fallback = true) |
|
291 | |||
292 | /** |
||
293 | * Returns the raw value of a configuration key. |
||
294 | * |
||
295 | * Unlike {@link get()}, this method does not resolve placeholders: |
||
296 | * |
||
297 | * ```php |
||
298 | * $config = new Config(); |
||
299 | * $config->set(Config::PULI_DIR, '.puli'); |
||
300 | * $config->set(Config::INSTALL_FILE, '{$puli-dir}/install-file.json'); |
||
301 | * |
||
302 | * echo $config->get(Config::PULI_DIR); |
||
303 | * // => .puli/install-file.json |
||
304 | * |
||
305 | * echo $config->getRaw(Config::PULI_DIR); |
||
306 | * // => {$puli-dir}/install-file.json |
||
307 | * ``` |
||
308 | * |
||
309 | * @param string $key The configuration key. |
||
310 | * @param mixed $default The value to return if the key was not set. |
||
311 | * @param bool $fallback Whether to return the value of the base |
||
312 | * configuration if the key was not set. |
||
313 | * |
||
314 | * @return mixed The value of the configuration key. |
||
315 | * |
||
316 | * @throws NoSuchConfigKeyException If the configuration key is invalid. |
||
317 | */ |
||
318 | 160 | public function getRaw($key, $default = null, $fallback = true) |
|
338 | |||
339 | /** |
||
340 | * Returns whether a configuration key is set. |
||
341 | * |
||
342 | * @param string $key The configuration key to search. |
||
343 | * @param bool $fallback Whether to check the base configuration if the |
||
344 | * key is not found. |
||
345 | * |
||
346 | * @return bool Returns `true` if the configuration key is set. |
||
347 | * |
||
348 | * @throws NoSuchConfigKeyException If the configuration key is invalid. |
||
349 | */ |
||
350 | 22 | public function contains($key, $fallback = true) |
|
370 | |||
371 | /** |
||
372 | * Sets the value of a configuration key. |
||
373 | * |
||
374 | * @param string $key The configuration key. |
||
375 | * @param mixed $value The value to set. |
||
376 | * |
||
377 | * @throws NoSuchConfigKeyException If the configuration key is invalid. |
||
378 | * @throws InvalidConfigException If the value is invalid. |
||
379 | */ |
||
380 | 448 | public function set($key, $value) |
|
401 | |||
402 | /** |
||
403 | * Sets a list of configuration values. |
||
404 | * |
||
405 | * @param array $values The values to set. |
||
406 | * |
||
407 | * @throws NoSuchConfigKeyException If a configuration key is invalid. |
||
408 | * @throws InvalidConfigException If a value is invalid. |
||
409 | */ |
||
410 | 726 | public function merge(array $values) |
|
416 | |||
417 | /** |
||
418 | * Replaces the configuration with a list of configuration values. |
||
419 | * |
||
420 | * @param array $values The values to set. |
||
421 | * |
||
422 | * @throws NoSuchConfigKeyException If a configuration key is invalid. |
||
423 | * @throws InvalidConfigException If a value is invalid. |
||
424 | */ |
||
425 | 4 | public function replace(array $values) |
|
430 | |||
431 | /** |
||
432 | * Removes a configuration key. |
||
433 | * |
||
434 | * If the configuration has a base configuration, the default value will |
||
435 | * be returned by {@link get()} after removing the key. |
||
436 | * |
||
437 | * @param string $key The configuration key to remove. |
||
438 | * |
||
439 | * @throws NoSuchConfigKeyException If the configuration key is invalid. |
||
440 | */ |
||
441 | 13 | public function remove($key) |
|
455 | |||
456 | /** |
||
457 | * Removes all configuration keys. |
||
458 | * |
||
459 | * If the configuration has a base configuration, the default values will |
||
460 | * be returned by {@link get()} after removing the keys. |
||
461 | */ |
||
462 | 5 | public function clear() |
|
466 | |||
467 | /** |
||
468 | * Returns all configuration values as flat array. |
||
469 | * |
||
470 | * @param bool $includeFallback Whether to include values set in the base |
||
471 | * configuration passed to {@link __construct()}. |
||
472 | * |
||
473 | * @return array The configuration values. |
||
474 | */ |
||
475 | 5 | public function toFlatArray($includeFallback = true) |
|
479 | |||
480 | /** |
||
481 | * Returns all raw configuration values as flat array. |
||
482 | * |
||
483 | * Unlike {@link toFlatArray()}, this method does not resolve placeholders: |
||
484 | * |
||
485 | * ```php |
||
486 | * $config = new Config(); |
||
487 | * $config->set(Config::PULI_DIR, '.puli'); |
||
488 | * $config->set(Config::REGISTRY_FILE, '{$puli-dir}/ServiceRegistry.php'); |
||
489 | * |
||
490 | * print_r($config->toFlatArray()); |
||
491 | * // Array( |
||
492 | * // 'puli-dir' => '.puli', |
||
493 | * // 'registry-file' => '.puli/ServiceRegistry.php', |
||
494 | * // ) |
||
495 | * |
||
496 | * print_r($config->toFlatRawArray()); |
||
497 | * // Array( |
||
498 | * // 'puli-dir' => '.puli', |
||
499 | * // 'registry-file' => '{$puli-dir}/ServiceRegistry.php', |
||
500 | * // ) |
||
501 | * ``` |
||
502 | * |
||
503 | * @param bool $includeFallback Whether to include values set in the base |
||
504 | * configuration passed to {@link __construct()}. |
||
505 | * |
||
506 | * @return array The raw configuration values. |
||
507 | */ |
||
508 | 34 | public function toFlatRawArray($includeFallback = true) |
|
514 | |||
515 | /** |
||
516 | * Returns all configuration values as nested array. |
||
517 | * |
||
518 | * @param bool $includeFallback Whether to include values set in the base |
||
519 | * configuration passed to {@link __construct()}. |
||
520 | * |
||
521 | * @return array The configuration values. |
||
522 | */ |
||
523 | 3 | public function toArray($includeFallback = true) |
|
527 | |||
528 | /** |
||
529 | * Returns all raw configuration values as nested array. |
||
530 | * |
||
531 | * Unlike {@link toArray()}, this method does not resolve placeholders: |
||
532 | * |
||
533 | * ```php |
||
534 | * $config = new Config(); |
||
535 | * $config->set(Config::PULI_DIR, '.puli'); |
||
536 | * $config->set(Config::REPO_STORAGE_DIR, '{$puli-dir}/repository'); |
||
537 | * |
||
538 | * print_r($config->toArray()); |
||
539 | * // Array( |
||
540 | * // 'puli-dir' => '.puli', |
||
541 | * // 'repository. => array( |
||
542 | * // 'storage-dir' => '.puli/repository', |
||
543 | * // ), |
||
544 | * // ) |
||
545 | * |
||
546 | * print_r($config->toRawArray()); |
||
547 | * // Array( |
||
548 | * // 'puli-dir' => '.puli', |
||
549 | * // 'repository. => array( |
||
550 | * // 'storage-dir' => '{$puli-dir}/repository', |
||
551 | * // ), |
||
552 | * // ) |
||
553 | * ``` |
||
554 | * |
||
555 | * @param bool $includeFallback Whether to include values set in the base |
||
556 | * configuration passed to {@link __construct()}. |
||
557 | * |
||
558 | * @return array The raw configuration values. |
||
559 | */ |
||
560 | 15 | public function toRawArray($includeFallback = true) |
|
572 | |||
573 | /** |
||
574 | * Returns whether the configuration is empty. |
||
575 | * |
||
576 | * @param bool $includeFallback Whether to include values set in the base |
||
577 | * configuration passed to {@link __construct()}. |
||
578 | * |
||
579 | * @return bool Returns `true` if no key is set and `false` otherwise. |
||
580 | */ |
||
581 | 5 | public function isEmpty($includeFallback = true) |
|
591 | |||
592 | /** |
||
593 | * @param string $key |
||
594 | * @param mixed $value |
||
595 | */ |
||
596 | 447 | private function validate($key, $value) |
|
635 | |||
636 | /** |
||
637 | * @param string $key |
||
638 | * @param mixed $value |
||
639 | * |
||
640 | * @throws InvalidConfigException If the config value isn't an array. |
||
641 | */ |
||
642 | 9 | View Code Duplication | private function assertArray($key, $value) |
652 | |||
653 | /** |
||
654 | * @param string $key |
||
655 | * @param mixed $value |
||
656 | * |
||
657 | * @throws InvalidConfigException If the config value is null. |
||
658 | */ |
||
659 | 432 | private function assertNotNull($key, $value) |
|
669 | |||
670 | /** |
||
671 | * @param string $key |
||
672 | * @param mixed $value |
||
673 | * |
||
674 | * @throws InvalidConfigException If the config value isn't a string. |
||
675 | */ |
||
676 | 418 | View Code Duplication | private function assertString($key, $value) |
686 | |||
687 | /** |
||
688 | * @param string $key |
||
689 | * @param mixed $value |
||
690 | * |
||
691 | * @throws InvalidConfigException If the config value isn't an integer. |
||
692 | */ |
||
693 | View Code Duplication | private function assertInteger($key, $value) |
|
703 | |||
704 | /** |
||
705 | * @param string $key |
||
706 | * @param mixed $value |
||
707 | * |
||
708 | * @throws InvalidConfigException If the config value isn't a boolean. |
||
709 | */ |
||
710 | 310 | View Code Duplication | private function assertBoolean($key, $value) |
720 | |||
721 | /** |
||
722 | * @param string $key |
||
723 | * @param mixed $value |
||
724 | * |
||
725 | * @throws InvalidConfigException If the config value is an empty string. |
||
726 | */ |
||
727 | 421 | private function assertNonEmpty($key, $value) |
|
736 | |||
737 | /** |
||
738 | * @param mixed $raw |
||
739 | * @param bool $fallback |
||
740 | * |
||
741 | * @return mixed |
||
742 | */ |
||
743 | 133 | private function replacePlaceholders($raw, $fallback = true) |
|
763 | |||
764 | /** |
||
765 | * @param string $keyPrefix |
||
766 | * |
||
767 | * @return array |
||
768 | */ |
||
769 | 47 | private function filterByKeyPrefix($keyPrefix) |
|
784 | |||
785 | /** |
||
786 | * @param string $keyPrefix |
||
787 | * |
||
788 | * @return bool |
||
789 | */ |
||
790 | 3 | private function containsKeyPrefix($keyPrefix) |
|
800 | |||
801 | /** |
||
802 | * @param string $keyPrefix |
||
803 | */ |
||
804 | 10 | private function removeByKeyPrefix($keyPrefix) |
|
814 | |||
815 | /** |
||
816 | * @param string $key |
||
817 | * @param mixed $value |
||
818 | * @param array $values |
||
819 | */ |
||
820 | 52 | private function addKeyValue($key, $value, array &$values) |
|
835 | } |
||
836 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.