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 Container implements \ArrayAccess |
||
14 | { |
||
15 | private $values = []; |
||
16 | private $factories; |
||
17 | private $protected; |
||
18 | private $frozen = []; |
||
19 | private $raw = []; |
||
20 | private $keys = []; |
||
21 | |||
22 | /** |
||
23 | * Instantiate the container. |
||
24 | * |
||
25 | * Objects and parameters can be passed as argument to the constructor. |
||
26 | * |
||
27 | * @param array $values The parameters or objects. |
||
28 | */ |
||
29 | public function __construct(array $values = []) |
||
38 | |||
39 | /** |
||
40 | * Sets a parameter or an object. |
||
41 | * |
||
42 | * Objects must be defined as Closures. |
||
43 | * |
||
44 | * Allowing any PHP callable leads to difficult to debug problems |
||
45 | * as function names (strings) are callable (creating a function with |
||
46 | * the same name as an existing parameter would break your container). |
||
47 | * |
||
48 | * @param string $id The unique identifier for the parameter or object |
||
49 | * @param mixed $value The value of the parameter or a closure to define an object |
||
50 | * |
||
51 | * @throws \RuntimeException Prevent override of a frozen service |
||
52 | */ |
||
53 | public function offsetSet($id, $value) |
||
62 | |||
63 | /** |
||
64 | * Gets a parameter or an object. |
||
65 | * |
||
66 | * @param string $id The unique identifier for the parameter or object |
||
67 | * |
||
68 | * @return mixed The value of the parameter or an object |
||
69 | * |
||
70 | * @throws \InvalidArgumentException if the identifier is not defined |
||
71 | */ |
||
72 | public function offsetGet($id) |
||
99 | |||
100 | /** |
||
101 | * Checks if a parameter or an object is set. |
||
102 | * |
||
103 | * @param string $id The unique identifier for the parameter or object |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function offsetExists($id) |
||
111 | |||
112 | /** |
||
113 | * Unsets a parameter or an object. |
||
114 | * |
||
115 | * @param string $id The unique identifier for the parameter or object |
||
116 | */ |
||
117 | public function offsetUnset($id) |
||
127 | |||
128 | /** |
||
129 | * Marks a callable as being a factory service. |
||
130 | * |
||
131 | * @param callable $callable A service definition to be used as a factory |
||
132 | * |
||
133 | * @return callable The passed callable |
||
134 | * |
||
135 | * @throws \InvalidArgumentException Service definition has to be a closure of an invokable object |
||
136 | */ |
||
137 | View Code Duplication | public function factory($callable) |
|
147 | |||
148 | /** |
||
149 | * Protects a callable from being interpreted as a service. |
||
150 | * |
||
151 | * This is useful when you want to store a callable as a parameter. |
||
152 | * |
||
153 | * @param callable $callable A callable to protect from being evaluated |
||
154 | * |
||
155 | * @return callable The passed callable |
||
156 | * |
||
157 | * @throws \InvalidArgumentException Service definition has to be a closure of an invokable object |
||
158 | */ |
||
159 | View Code Duplication | public function protect($callable) |
|
169 | |||
170 | /** |
||
171 | * Gets a parameter or the closure defining an object. |
||
172 | * |
||
173 | * @param string $id The unique identifier for the parameter or object |
||
174 | * |
||
175 | * @return mixed The value of the parameter or the closure defining an object |
||
176 | * |
||
177 | * @throws \InvalidArgumentException if the identifier is not defined |
||
178 | */ |
||
179 | public function raw($id) |
||
191 | |||
192 | /** |
||
193 | * Extends an object definition. |
||
194 | * |
||
195 | * Useful when you want to extend an existing object definition, |
||
196 | * without necessarily loading that object. |
||
197 | * |
||
198 | * @param string $id The unique identifier for the object |
||
199 | * @param callable $callable A service definition to extend the original |
||
200 | * |
||
201 | * @return callable The wrapped callable |
||
202 | * |
||
203 | * @throws \InvalidArgumentException if the identifier is not defined or not a service definition |
||
204 | */ |
||
205 | public function extend($id, $callable) |
||
232 | |||
233 | /** |
||
234 | * Returns all defined value names. |
||
235 | * |
||
236 | * @return array An array of value names |
||
237 | */ |
||
238 | public function keys() |
||
242 | } |
||
243 |