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 |
||
13 | class ResolverFactory extends AbstractFactory |
||
14 | { |
||
15 | /** |
||
16 | * @var string $resolverPrefix |
||
17 | */ |
||
18 | private $resolverPrefix = ''; |
||
19 | |||
20 | /** |
||
21 | * @var string $resolverSuffix |
||
22 | */ |
||
23 | private $resolverSuffix = ''; |
||
24 | |||
25 | /** |
||
26 | * @var array $resolverCapitals |
||
27 | */ |
||
28 | private $resolverCapitals; |
||
29 | |||
30 | /** |
||
31 | * @var array $resolverReplacements |
||
32 | */ |
||
33 | private $resolverReplacements; |
||
34 | |||
35 | /** |
||
36 | * @param array $data Factory arguments. |
||
37 | */ |
||
38 | public function __construct(array $data = null) |
||
69 | |||
70 | /** |
||
71 | * @param string $prefix The resolver prefix string. |
||
72 | * @throws InvalidArgumentException If the prefix argument is not a string. |
||
73 | * @return ResolverFactory Chainable |
||
74 | */ |
||
75 | public function setResolverPrefix($prefix) |
||
85 | |||
86 | /** |
||
87 | * @return string |
||
88 | */ |
||
89 | public function resolverPrefix() |
||
93 | |||
94 | /** |
||
95 | * @param string $suffix The resolver suffix string. |
||
96 | * @throws InvalidArgumentException If the suffix argument is not a string. |
||
97 | * @return ResolverFactory Chainable |
||
98 | */ |
||
99 | public function setResolverSuffix($suffix) |
||
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | public function resolverSuffix() |
||
117 | |||
118 | /** |
||
119 | * @param array $capitals The array of letter to "calitalize-next" (uppercase next letter in the string). |
||
120 | * @return ResolverFactory Chainable |
||
121 | */ |
||
122 | public function setResolverCapitals(array $capitals) |
||
127 | |||
128 | /** |
||
129 | * @return array |
||
130 | */ |
||
131 | public function resolverCapitals() |
||
135 | |||
136 | /** |
||
137 | * @param array $replacements The array (key=>value) of replacements. |
||
138 | * @return ResolverFactory Chainable |
||
139 | */ |
||
140 | public function setResolverReplacements(array $replacements) |
||
145 | |||
146 | /** |
||
147 | * @return array |
||
148 | */ |
||
149 | public function resolverReplacements() |
||
153 | |||
154 | /** |
||
155 | * Resolve the class name from the requested type. |
||
156 | * |
||
157 | * @param string $type The "type" of object to resolve (the object ident). |
||
158 | * @throws InvalidArgumentException If the type parameter is not a string. |
||
159 | * @return string The resolved class name (FQN). |
||
160 | */ |
||
161 | public function resolve($type) |
||
192 | |||
193 | /** |
||
194 | * @param string $type The "type" of object to resolve (the object ident). |
||
195 | * @throws InvalidArgumentException If the type parameter is not a string. |
||
196 | * @return boolean |
||
197 | */ |
||
198 | public function isResolvable($type) |
||
209 | } |
||
210 |
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.