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 Swift_DependencyContainer 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 Swift_DependencyContainer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Swift_DependencyContainer |
||
|
|||
17 | { |
||
18 | /** |
||
19 | * Constant for literal value types |
||
20 | */ |
||
21 | const TYPE_VALUE = 0x0001; |
||
22 | |||
23 | /** |
||
24 | * Constant for new instance types |
||
25 | */ |
||
26 | const TYPE_INSTANCE = 0x0010; |
||
27 | |||
28 | /** |
||
29 | * Constant for shared instance types |
||
30 | */ |
||
31 | const TYPE_SHARED = 0x0100; |
||
32 | |||
33 | /** |
||
34 | * Constant for aliases |
||
35 | */ |
||
36 | const TYPE_ALIAS = 0x1000; |
||
37 | |||
38 | /** |
||
39 | * Singleton instance |
||
40 | * |
||
41 | * @var null|self |
||
42 | */ |
||
43 | private static $_instance = null; |
||
44 | |||
45 | /** |
||
46 | * The data container |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | private $_store = array(); |
||
51 | |||
52 | /** |
||
53 | * The current endpoint in the data container |
||
54 | */ |
||
55 | private $_endPoint; |
||
56 | |||
57 | /** |
||
58 | * Constructor should not be used. |
||
59 | * |
||
60 | * Use {@link getInstance()} instead. |
||
61 | */ |
||
62 | 18 | public function __construct() |
|
65 | |||
66 | /** |
||
67 | * Returns a singleton of the DependencyContainer. |
||
68 | * |
||
69 | * @return self |
||
70 | */ |
||
71 | 175 | public static function getInstance() |
|
79 | |||
80 | /** |
||
81 | * List the names of all items stored in the Container. |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | 1 | public function listItems() |
|
89 | |||
90 | /** |
||
91 | * Test if an item is registered in this container with the given name. |
||
92 | * |
||
93 | * @see register() |
||
94 | * |
||
95 | * @param string $itemName |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | 192 | public function has($itemName) |
|
103 | |||
104 | /** |
||
105 | * Lookup the item with the given $itemName. |
||
106 | * |
||
107 | * @see register() |
||
108 | * |
||
109 | * @param string $itemName |
||
110 | * |
||
111 | * @throws Swift_DependencyException If the dependency is not found |
||
112 | * |
||
113 | * @return mixed |
||
114 | */ |
||
115 | 188 | public function lookup($itemName) |
|
134 | |||
135 | /** |
||
136 | * Create an array of arguments passed to the constructor of $itemName. |
||
137 | * |
||
138 | * @param string $itemName |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | 184 | public function createDependenciesFor($itemName) |
|
151 | |||
152 | /** |
||
153 | * Register a new dependency with $itemName. |
||
154 | * |
||
155 | * This method returns the current DependencyContainer instance because it |
||
156 | * requires the use of the fluid interface to set the specific details for the |
||
157 | * dependency. |
||
158 | * |
||
159 | * @see asNewInstanceOf(), asSharedInstanceOf(), asValue() |
||
160 | * |
||
161 | * @param string $itemName |
||
162 | * |
||
163 | * @return $this |
||
164 | */ |
||
165 | 145 | public function register($itemName) |
|
172 | |||
173 | /** |
||
174 | * Specify the previously registered item as a literal value. |
||
175 | * |
||
176 | * {@link register()} must be called before this will work. |
||
177 | * |
||
178 | * @param mixed $value |
||
179 | * |
||
180 | * @return $this |
||
181 | */ |
||
182 | 130 | public function asValue($value) |
|
190 | |||
191 | /** |
||
192 | * Specify the previously registered item as an alias of another item. |
||
193 | * |
||
194 | * @param string $lookup |
||
195 | * |
||
196 | * @return $this |
||
197 | */ |
||
198 | 2 | public function asAliasOf($lookup) |
|
206 | |||
207 | /** |
||
208 | * Specify the previously registered item as a new instance of $className. |
||
209 | * |
||
210 | * {@link register()} must be called before this will work. |
||
211 | * Any arguments can be set with {@link withDependencies()}, |
||
212 | * {@link addConstructorValue()} or {@link addConstructorLookup()}. |
||
213 | * |
||
214 | * @see withDependencies(), addConstructorValue(), addConstructorLookup() |
||
215 | * |
||
216 | * @param string $className |
||
217 | * |
||
218 | * @return $this |
||
219 | */ |
||
220 | 18 | View Code Duplication | public function asNewInstanceOf($className) |
228 | |||
229 | /** |
||
230 | * Specify the previously registered item as a shared instance of $className. |
||
231 | * |
||
232 | * {@link register()} must be called before this will work. |
||
233 | * |
||
234 | * @param string $className |
||
235 | * |
||
236 | * @return $this |
||
237 | */ |
||
238 | 3 | View Code Duplication | public function asSharedInstanceOf($className) |
246 | |||
247 | /** |
||
248 | * Specify a list of injected dependencies for the previously registered item. |
||
249 | * |
||
250 | * This method takes an array of lookup names. |
||
251 | * |
||
252 | * @see addConstructorValue(), addConstructorLookup() |
||
253 | * |
||
254 | * @param array $lookups |
||
255 | * |
||
256 | * @return $this |
||
257 | */ |
||
258 | 13 | public function withDependencies(array $lookups) |
|
268 | |||
269 | /** |
||
270 | * Specify a literal (non looked up) value for the constructor of the |
||
271 | * previously registered item. |
||
272 | * |
||
273 | * @see withDependencies(), addConstructorLookup() |
||
274 | * |
||
275 | * @param mixed $value |
||
276 | * |
||
277 | * @return $this |
||
278 | */ |
||
279 | 9 | View Code Duplication | public function addConstructorValue($value) |
289 | |||
290 | /** |
||
291 | * Specify a dependency lookup for the constructor of the previously |
||
292 | * registered item. |
||
293 | * |
||
294 | * @see withDependencies(), addConstructorValue() |
||
295 | * |
||
296 | * @param string $lookup |
||
297 | * |
||
298 | * @return $this |
||
299 | */ |
||
300 | 14 | View Code Duplication | public function addConstructorLookup($lookup) |
310 | |||
311 | /** |
||
312 | * Get the literal value with $itemName |
||
313 | * |
||
314 | * @param $itemName |
||
315 | * |
||
316 | * @return mixed |
||
317 | */ |
||
318 | 172 | private function _getValue($itemName) |
|
322 | |||
323 | /** |
||
324 | * Resolve an alias to another item |
||
325 | * |
||
326 | * @param $itemName |
||
327 | * |
||
328 | * @return mixed |
||
329 | * @throws Swift_DependencyException |
||
330 | */ |
||
331 | 164 | private function _createAlias($itemName) |
|
335 | |||
336 | /** Create a fresh instance of $itemName */ |
||
337 | 184 | private function _createNewInstance($itemName) |
|
348 | |||
349 | /** |
||
350 | * Create and register a shared instance of $itemName |
||
351 | * |
||
352 | * @param $itemName |
||
353 | * |
||
354 | * @return mixed |
||
355 | */ |
||
356 | 173 | private function _createSharedInstance($itemName) |
|
364 | |||
365 | /** |
||
366 | * Get the current endpoint in the store |
||
367 | * |
||
368 | * @return mixed |
||
369 | */ |
||
370 | 145 | private function &_getEndPoint() |
|
380 | |||
381 | /** |
||
382 | * Get an argument list with dependencies resolved |
||
383 | * |
||
384 | * @param array $args |
||
385 | * |
||
386 | * @return array |
||
387 | */ |
||
388 | 180 | private function _resolveArgs(array $args) |
|
404 | |||
405 | /** Resolve a single dependency with an collections */ |
||
406 | 177 | private function _lookupRecursive($item) |
|
419 | } |
||
420 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.