Complex classes like CallbackContainerBuilder 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 CallbackContainerBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class CallbackContainerBuilder implements ContainerBuilder { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $registry = array(); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $singletons = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $expectedReturnTypeByHandler = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $aliases = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $recursiveMarker = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @since 2.0 |
||
| 50 | * |
||
| 51 | * {@inheritDoc} |
||
| 52 | */ |
||
| 53 | 34 | public function __construct( CallbackContainer $callbackContainer = null ) { |
|
| 58 | |||
| 59 | /** |
||
| 60 | * @since 2.0 |
||
| 61 | * |
||
| 62 | * {@inheritDoc} |
||
| 63 | */ |
||
| 64 | 4 | public function registerCallbackContainer( CallbackContainer $callbackContainer ) { |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @since 2.0 |
||
| 70 | * |
||
| 71 | * {@inheritDoc} |
||
| 72 | */ |
||
| 73 | 5 | public function registerFromFile( $file ) { |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @since 2.0 |
||
| 84 | * |
||
| 85 | * {@inheritDoc} |
||
| 86 | */ |
||
| 87 | 18 | public function registerCallback( $serviceName, callable $callback ) { |
|
| 95 | |||
| 96 | /** |
||
| 97 | * If you are not running PHPUnit or for that matter any other testing |
||
| 98 | * environment then you are not suppose to use this function. |
||
| 99 | * |
||
| 100 | * @since 2.0 |
||
| 101 | * |
||
| 102 | * {@inheritDoc} |
||
| 103 | */ |
||
| 104 | 5 | public function registerObject( $serviceName, $instance ) { |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @since 2.0 |
||
| 122 | * |
||
| 123 | * {@inheritDoc} |
||
| 124 | */ |
||
| 125 | 15 | public function registerExpectedReturnType( $serviceName, $type ) { |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @since 2.0 |
||
| 136 | * |
||
| 137 | * {@inheritDoc} |
||
| 138 | */ |
||
| 139 | 6 | public function registerAlias( $serviceName, $alias ) { |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @since 2.0 |
||
| 158 | * |
||
| 159 | * {@inheritDoc} |
||
| 160 | */ |
||
| 161 | 3 | public function isRegistered( $serviceName ) { |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @since 2.0 |
||
| 172 | * |
||
| 173 | * {@inheritDoc} |
||
| 174 | */ |
||
| 175 | 18 | public function create( $serviceName ) { |
|
| 183 | |||
| 184 | /** |
||
| 185 | * @since 2.0 |
||
| 186 | * |
||
| 187 | * {@inheritDoc} |
||
| 188 | */ |
||
| 189 | 11 | public function singleton( $serviceName ) { |
|
| 197 | |||
| 198 | /** |
||
| 199 | * @since 2.0 |
||
| 200 | * |
||
| 201 | * @param string $serviceName |
||
| 202 | */ |
||
| 203 | 1 | public function deregister( $serviceName ) { |
|
| 214 | |||
| 215 | 8 | private function register( $serviceDefinitions ) { |
|
| 227 | |||
| 228 | 23 | private function addRecursiveMarkerFor( $serviceName ) { |
|
| 244 | |||
| 245 | 22 | private function getReturnValueFromCallbackHandlerFor( $serviceName, $parameters ) { |
|
| 269 | |||
| 270 | 11 | private function getReturnValueFromSingletonFor( $serviceName, $parameters ) { |
|
| 297 | |||
| 298 | } |
||
| 299 |