Complex classes like MongoDbServiceProvider 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 MongoDbServiceProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class MongoDbServiceProvider implements ServiceProviderInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var array Buffer. |
||
| 23 | */ |
||
| 24 | private $args = array(); |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private $defaultArgs = array( |
||
| 30 | 'uri_options' => array(), |
||
| 31 | 'driver_options' => array(), |
||
| 32 | ); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var int|string |
||
| 36 | */ |
||
| 37 | private $defaultConnection = 0; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $id = 'mongodb'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $instances = 'mongodb.clients'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var bool |
||
| 51 | */ |
||
| 52 | private $isLoaded = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | private $parameters = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param string $id |
||
| 61 | * @param string $instances |
||
| 62 | */ |
||
| 63 | 11 | public function __construct($id = null, $instances = null) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | 5 | private function buildConnectionString(array $connection) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @param array|string $connection |
||
| 92 | * |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | 11 | private function buildUri($connection) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param \Pimple\Container $app |
||
| 106 | * @param string $name |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | 11 | private function getArg(Container $app, $name) |
|
| 114 | |||
| 115 | 5 | private function getArgConnectionAuthority(array $connection) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | 5 | private function getArgConnectionHost(array $connection = array()) |
|
| 139 | |||
| 140 | private function getArgConnectionHosts(array $connection) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | 11 | private function getArgUri() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @param \Pimple\Container $app |
||
| 165 | * @param string $name |
||
| 166 | * |
||
| 167 | * @return string|array |
||
| 168 | */ |
||
| 169 | 11 | private function getDefaultArg(Container $app, $name) |
|
| 177 | |||
| 178 | 11 | private function loadParameters(Container $app) |
|
| 193 | |||
| 194 | 10 | private function loadSingletonParameters(Container $app) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $uri |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | 5 | private function sanitizeUri($uri) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @param \Pimple\Container $app |
||
| 228 | * @param string $name |
||
| 229 | */ |
||
| 230 | private function setDefaultArg(Container $app, $name) |
||
| 236 | |||
| 237 | 11 | public function register(Container $app) |
|
| 267 | } |
||
| 268 |