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 | |||
| 31 | /** |
||
| 32 | * @var int|string |
||
| 33 | */ |
||
| 34 | private $defaultConnection = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $id = 'mongodb'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $instances = 'mongodb.clients'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | private $isLoaded = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $parameters = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param string $id |
||
| 58 | * @param string $instances |
||
| 59 | */ |
||
| 60 | 11 | public function __construct($id = null, $instances = null) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | 5 | private function buildConnectionString(array $connection) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @param array|string $connection |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | 11 | private function buildUri($connection) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param \Pimple\Container $app |
||
| 103 | * @param string $name |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | 11 | private function getArg(Container $app, $name) |
|
| 111 | |||
| 112 | 5 | private function getArgConnectionAuthority(array $connection) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | 5 | private function getArgConnectionHost(array $connection = array()) |
|
| 140 | |||
| 141 | 1 | private function getArgConnectionHosts(array $connection) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | 11 | private function getArgUri() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @param \Pimple\Container $app |
||
| 162 | * @param string $name |
||
| 163 | * |
||
| 164 | * @return string|array |
||
| 165 | */ |
||
| 166 | 11 | private function getDefaultArg(Container $app, $name) |
|
| 174 | |||
| 175 | 11 | private function loadParameters(Container $app) |
|
| 190 | |||
| 191 | 10 | private function loadSingletonParameters(Container $app) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $uri |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | 5 | private function sanitizeUri($uri) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @param \Pimple\Container $app |
||
| 225 | * @param string $name |
||
| 226 | */ |
||
| 227 | 11 | private function setDefaultArg(Container $app, $name) |
|
| 233 | |||
| 234 | 11 | public function register(Container $app) |
|
| 264 | } |
||
| 265 |