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 |
||
| 34 | class Container implements \ArrayAccess |
||
| 35 | { |
||
| 36 | private $values = array(); |
||
| 37 | private $factories; |
||
| 38 | private $protected; |
||
| 39 | private $frozen = array(); |
||
| 40 | private $raw = array(); |
||
| 41 | private $keys = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Instantiate the container. |
||
| 45 | * |
||
| 46 | * Objects and parameters can be passed as argument to the constructor. |
||
| 47 | * |
||
| 48 | * @param array $values The parameters or objects. |
||
| 49 | */ |
||
| 50 | public function __construct(array $values = array()) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Sets a parameter or an object. |
||
| 62 | * |
||
| 63 | * Objects must be defined as Closures. |
||
| 64 | * |
||
| 65 | * Allowing any PHP callable leads to difficult to debug problems |
||
| 66 | * as function names (strings) are callable (creating a function with |
||
| 67 | * the same name as an existing parameter would break your container). |
||
| 68 | * |
||
| 69 | * @param string $id The unique identifier for the parameter or object |
||
| 70 | * @param mixed $value The value of the parameter or a closure to define an object |
||
| 71 | * |
||
| 72 | * @throws \RuntimeException Prevent override of a frozen service |
||
| 73 | */ |
||
| 74 | public function offsetSet($id, $value) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Gets a parameter or an object. |
||
| 86 | * |
||
| 87 | * @param string $id The unique identifier for the parameter or object |
||
| 88 | * |
||
| 89 | * @return mixed The value of the parameter or an object |
||
| 90 | * |
||
| 91 | * @throws \InvalidArgumentException if the identifier is not defined |
||
| 92 | */ |
||
| 93 | public function offsetGet($id) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Checks if a parameter or an object is set. |
||
| 123 | * |
||
| 124 | * @param string $id The unique identifier for the parameter or object |
||
| 125 | * |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | public function offsetExists($id) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Unsets a parameter or an object. |
||
| 135 | * |
||
| 136 | * @param string $id The unique identifier for the parameter or object |
||
| 137 | */ |
||
| 138 | public function offsetUnset($id) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Marks a callable as being a factory service. |
||
| 151 | * |
||
| 152 | * @param callable $callable A service definition to be used as a factory |
||
| 153 | * |
||
| 154 | * @return callable The passed callable |
||
| 155 | * |
||
| 156 | * @throws \InvalidArgumentException Service definition has to be a closure of an invokable object |
||
| 157 | */ |
||
| 158 | View Code Duplication | public function factory($callable) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Protects a callable from being interpreted as a service. |
||
| 171 | * |
||
| 172 | * This is useful when you want to store a callable as a parameter. |
||
| 173 | * |
||
| 174 | * @param callable $callable A callable to protect from being evaluated |
||
| 175 | * |
||
| 176 | * @return callable The passed callable |
||
| 177 | * |
||
| 178 | * @throws \InvalidArgumentException Service definition has to be a closure of an invokable object |
||
| 179 | */ |
||
| 180 | View Code Duplication | public function protect($callable) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Gets a parameter or the closure defining an object. |
||
| 193 | * |
||
| 194 | * @param string $id The unique identifier for the parameter or object |
||
| 195 | * |
||
| 196 | * @return mixed The value of the parameter or the closure defining an object |
||
| 197 | * |
||
| 198 | * @throws \InvalidArgumentException if the identifier is not defined |
||
| 199 | */ |
||
| 200 | public function raw($id) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Extends an object definition. |
||
| 215 | * |
||
| 216 | * Useful when you want to extend an existing object definition, |
||
| 217 | * without necessarily loading that object. |
||
| 218 | * |
||
| 219 | * @param string $id The unique identifier for the object |
||
| 220 | * @param callable $callable A service definition to extend the original |
||
| 221 | * |
||
| 222 | * @return callable The wrapped callable |
||
| 223 | * |
||
| 224 | * @throws \InvalidArgumentException if the identifier is not defined or not a service definition |
||
| 225 | */ |
||
| 226 | public function extend($id, $callable) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Returns all defined value names. |
||
| 256 | * |
||
| 257 | * @return array An array of value names |
||
| 258 | */ |
||
| 259 | public function keys() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Registers a service provider. |
||
| 266 | * |
||
| 267 | * @param ServiceProviderInterface $provider A ServiceProviderInterface instance |
||
| 268 | * @param array $values An array of values that customizes the provider |
||
| 269 | * |
||
| 270 | * @return static |
||
| 271 | */ |
||
| 272 | public function register(ServiceProviderInterface $provider, array $values = array()) |
||
| 282 | } |
||
| 283 |
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.