Complex classes like Container 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.
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 Container, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 57 | class Container extends Dispatcher implements ArrayAccess, ContainerInterface |
||
| 58 | { |
||
| 59 | /** |
||
| 60 | * The objects store |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $store = array(); |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Informations about stored objects |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $storeData = array(); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Container Properties |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | protected $properties = array(); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Properties Keys (cached) |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $propertiesMap = array(); |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Delegates Containers |
||
| 85 | * @var SplObjectStorage |
||
| 86 | */ |
||
| 87 | protected $delegates; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Constructor |
||
| 91 | * |
||
| 92 | * @return void |
||
|
|
|||
| 93 | 40 | */ |
|
| 94 | public function __construct() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Registers a definition |
||
| 102 | * |
||
| 103 | * @param string $name Identifier |
||
| 104 | * @param mixed $definition Definition, callable or value |
||
| 105 | * @param boolean $shared Should the instance be "shared" (singleton) |
||
| 106 | * @param array $data Meta-data associated with this definition |
||
| 107 | * |
||
| 108 | * @return Container |
||
| 109 | 40 | */ |
|
| 110 | public function set($name, $definition, $shared = false, |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Load and returns a definition |
||
| 131 | * |
||
| 132 | * @param string $name Identifier |
||
| 133 | * |
||
| 134 | * @throws Exceptions\DefinitionNotFoundException if $name isn't a valid identifier |
||
| 135 | * @return mixed |
||
| 136 | 26 | */ |
|
| 137 | public function get($name) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns data associated with the given definition (without internals). |
||
| 194 | * |
||
| 195 | * @param string $name Service name |
||
| 196 | * |
||
| 197 | * @throws DefinitionNotFoundException |
||
| 198 | 1 | */ |
|
| 199 | public function getDefinitionData($name) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Loads properties from an INI file as definitions. |
||
| 215 | * Theses properties can then be referenced like @propName in other |
||
| 216 | * definitions. |
||
| 217 | * |
||
| 218 | * @param string $iniFile Path/to/file.ini |
||
| 219 | * @param null|string $category The INI category to be parsed |
||
| 220 | * |
||
| 221 | * @return Container |
||
| 222 | * @throws Exception |
||
| 223 | 5 | */ |
|
| 224 | public function iniProperties($iniFile, $category = null) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Returns a property (or $default if not defined) |
||
| 249 | * |
||
| 250 | * @param string $propName The property name |
||
| 251 | * @param mixed $default Default value if the property is not defined |
||
| 252 | * |
||
| 253 | * @return mixed |
||
| 254 | 3 | */ |
|
| 255 | public function getProperty($propName, $default = null) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Defines a property. |
||
| 268 | * |
||
| 269 | * If the $value is null, the property will be unset. |
||
| 270 | * |
||
| 271 | * It recommended to store only strings as property values. Register a |
||
| 272 | * new Di definition for any other type. |
||
| 273 | * |
||
| 274 | * @param string $propName Property name |
||
| 275 | * @param null|string $value The prop value |
||
| 276 | * |
||
| 277 | * @return Container |
||
| 278 | 7 | */ |
|
| 279 | public function setProperty($propName, $value = null) |
||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * Transform properties references to their respective value |
||
| 296 | * |
||
| 297 | * @param string $str String to be transformed |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | 21 | */ |
|
| 301 | public function propertizeString($str) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Unregisters a definition |
||
| 312 | * |
||
| 313 | * @param string $name Identifier |
||
| 314 | * |
||
| 315 | * @throws Exceptions\DefinitionNotFoundException if $name isn't a valid identifier |
||
| 316 | * @return boolean true on success |
||
| 317 | 3 | */ |
|
| 318 | public function unregister($name) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Tells if a definition has been flagged has "shared" (singleton) |
||
| 332 | * |
||
| 333 | * @param string $name Identifier |
||
| 334 | * |
||
| 335 | * @throws Exceptions\DefinitionNotFoundException if $name isn't a valid identifier |
||
| 336 | * @return boolean |
||
| 337 | 9 | */ |
|
| 338 | public function isShared($name) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Tells if a definition exists at $offset |
||
| 351 | * |
||
| 352 | * @param string $name Identifier |
||
| 353 | * |
||
| 354 | * @return boolean |
||
| 355 | 29 | */ |
|
| 356 | public function has($name) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Tells if a definition is registered at $offset |
||
| 363 | * |
||
| 364 | * @param string $offset Identifier |
||
| 365 | * |
||
| 366 | * @return boolean |
||
| 367 | */ |
||
| 368 | public function offsetExists($offset) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Loads and returns a definition |
||
| 375 | * |
||
| 376 | * @param string $offset Identifier |
||
| 377 | * |
||
| 378 | * @return mixed |
||
| 379 | */ |
||
| 380 | public function offsetGet($offset) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Registers a definition |
||
| 387 | * |
||
| 388 | * @param string $offset Identifier |
||
| 389 | * @param mixed $value Definition |
||
| 390 | * |
||
| 391 | * @return Container |
||
| 392 | 16 | */ |
|
| 393 | public function offsetSet($offset, $value) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Unregisters a Definition |
||
| 400 | * |
||
| 401 | * @param string $offset Identifier |
||
| 402 | * |
||
| 403 | * @return boolean |
||
| 404 | */ |
||
| 405 | public function offsetUnset($offset) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Adds a delegate/backup Container. |
||
| 412 | * |
||
| 413 | * @param ContainerInterface $container |
||
| 414 | * |
||
| 415 | * @return ContainerInterface |
||
| 416 | */ |
||
| 417 | public function delegate(ContainerInterface $container) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Tells if a service is in a delegated Container |
||
| 430 | * |
||
| 431 | * @param string $name |
||
| 432 | * |
||
| 433 | * @return boolean |
||
| 434 | */ |
||
| 435 | public function hasInDelegate($name) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Loads a definition from the first delegated Container having in (FIFO) |
||
| 449 | * |
||
| 450 | * @param string $name Service identifier |
||
| 451 | * |
||
| 452 | * @throws Exceptions\DefinitionNotFoundException when the service is not found |
||
| 453 | * @return mixed |
||
| 454 | 6 | */ |
|
| 455 | public function getFromDelegate($name) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param array $query |
||
| 469 | * |
||
| 470 | * @return array |
||
| 471 | */ |
||
| 472 | public function search(array $query) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Transforms a wildcard to a regex |
||
| 506 | * |
||
| 507 | * @param string $value |
||
| 508 | * |
||
| 509 | * @return string |
||
| 510 | * @throws SearchException |
||
| 511 | */ |
||
| 512 | protected function searchQueryToRegex($value) |
||
| 526 | } |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.