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 |
||
11 | final class Router implements MethodMatcher, WarmableInterface |
||
12 | { |
||
13 | /** @var MethodCollectionLoader */ |
||
14 | private $loader; |
||
15 | /** @var MethodCollection */ |
||
16 | private $collection; |
||
17 | /** @var MethodMatcher */ |
||
18 | private $matcher; |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $name; |
||
23 | /** @var ConfigCacheFactoryInterface */ |
||
24 | private $configCacheFactory; |
||
25 | private $options = []; |
||
26 | |||
27 | /** |
||
28 | * Router constructor. |
||
29 | * |
||
30 | * @param MethodCollectionLoader $loader |
||
31 | * @param string $name |
||
32 | * @param array $options |
||
33 | */ |
||
34 | 11 | public function __construct(MethodCollectionLoader $loader, $name, array $options = []) |
|
40 | |||
41 | /** |
||
42 | * @return MethodCollection |
||
43 | 11 | */ |
|
44 | 2 | public function getMethodCollection() |
|
52 | |||
53 | /** {@inheritdoc} */ |
||
54 | 9 | public function match($method) |
|
58 | 1 | ||
59 | 1 | /** |
|
60 | 1 | * Warms up the cache. |
|
61 | 1 | * |
|
62 | * @param string $cacheDir The cache directory |
||
63 | 1 | */ |
|
64 | public function warmUp($cacheDir) |
||
74 | |||
75 | /** |
||
76 | * Sets an option. |
||
77 | * |
||
78 | * @param string $key The key |
||
79 | * @param mixed $value The value |
||
80 | * |
||
81 | * @throws \InvalidArgumentException |
||
82 | */ |
||
83 | View Code Duplication | public function setOption($key, $value) |
|
91 | |||
92 | 1 | /** |
|
93 | * Gets an option value. |
||
94 | 1 | * |
|
95 | 1 | * @param string $key The key |
|
96 | 1 | * |
|
97 | 1 | * @return mixed The value |
|
98 | * |
||
99 | 1 | * @throws \InvalidArgumentException |
|
100 | */ |
||
101 | 1 | View Code Duplication | public function getOption($key) |
109 | 1 | ||
110 | 1 | /** |
|
111 | * Sets options. |
||
112 | 1 | * |
|
113 | 1 | * Available options: |
|
114 | 1 | * |
|
115 | 1 | * * cache_dir: The cache directory (or null to disable caching) |
|
116 | 1 | * * debug: Whether to enable debugging or not (false by default) |
|
117 | 1 | * |
|
118 | 1 | * @param array $options An array of options |
|
119 | 1 | * |
|
120 | * @throws \InvalidArgumentException When unsupported option is provided |
||
121 | */ |
||
122 | 11 | public function setOptions(array $options) |
|
146 | 1 | ||
147 | /** |
||
148 | 1 | * @return MethodMatcher |
|
149 | */ |
||
150 | 9 | private function getMatcher() |
|
185 | |||
186 | /** |
||
187 | * Provides the ConfigCache factory implementation, falling back to a |
||
188 | * default implementation if necessary. |
||
189 | * |
||
190 | * @return ConfigCacheFactoryInterface $configCacheFactory |
||
191 | */ |
||
192 | 9 | private function getConfigCacheFactory() |
|
200 | } |
||
201 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.