Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Client |
||
| 23 | { |
||
| 24 | const DEFAULT_DSN = 'mongodb://127.0.0.1'; |
||
| 25 | |||
| 26 | private $dsn = self::DEFAULT_DSN; |
||
| 27 | |||
| 28 | private $connectOptions = array(); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * |
||
| 32 | * @var \MongoClient |
||
| 33 | */ |
||
| 34 | private $mongoClient; |
||
| 35 | |||
| 36 | private $databasePool = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array Database to class mapping |
||
| 40 | */ |
||
| 41 | protected $_mapping = array(); |
||
| 42 | |||
| 43 | |||
| 44 | private $logger; |
||
| 45 | |||
| 46 | private $currentDatabaseName; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * |
||
| 50 | * @var string version of MongoDb |
||
| 51 | */ |
||
| 52 | private $dbVersion; |
||
| 53 | |||
| 54 | private $debug = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * |
||
| 58 | * @param string $dsn Data Source Name |
||
| 59 | * @param array $options |
||
| 60 | */ |
||
| 61 | public function __construct($dsn = null, array $options = null) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Set credentials to auth on db, specified in connect options or dsn. |
||
| 73 | * If not specified - auth on admin db |
||
| 74 | * |
||
| 75 | * @param type $username |
||
| 76 | * @param type $password |
||
| 77 | * @return \Sokil\Mongo\Client |
||
| 78 | */ |
||
| 79 | public function setCredentials($username, $password) |
||
| 86 | |||
| 87 | public function __get($name) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * |
||
| 94 | * @return string Version of PHP driver |
||
| 95 | */ |
||
| 96 | public function getVersion() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * |
||
| 103 | * @return string version of mongo database |
||
| 104 | */ |
||
| 105 | public function getDbVersion() |
||
| 118 | |||
| 119 | public function setDsn($dsn) |
||
| 124 | |||
| 125 | public function getDsn() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Set connect options |
||
| 132 | * |
||
| 133 | * @link http://php.net/manual/en/mongoclient.construct.php connect options |
||
| 134 | * @param array $options |
||
| 135 | * @return \Sokil\Mongo\Client |
||
| 136 | */ |
||
| 137 | public function setConnectOptions(array $options) |
||
| 142 | |||
| 143 | public function getConnectOptions() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Set mongo's client |
||
| 150 | * |
||
| 151 | * @param \MongoClient $client |
||
| 152 | * @return \Sokil\Mongo\Client |
||
| 153 | */ |
||
| 154 | public function setMongoClient(\MongoClient $client) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get mongo connection instance |
||
| 162 | * |
||
| 163 | * @return \MongoClient |
||
| 164 | * @throws \Sokil\Mongo\Exception |
||
| 165 | */ |
||
| 166 | public function getMongoClient() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get list of all active connections through this client |
||
| 179 | * |
||
| 180 | * @return type |
||
| 181 | */ |
||
| 182 | public function getConnections() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Map database and collection name to class |
||
| 189 | * |
||
| 190 | * @param array $mapping classpath or class prefix |
||
| 191 | * Classpath: |
||
| 192 | * [dbname => [collectionName => collectionClass, ...], ...] |
||
| 193 | * Class prefix: |
||
| 194 | * [dbname => classPrefix] |
||
| 195 | * |
||
| 196 | * @return \Sokil\Mongo\Client |
||
| 197 | */ |
||
| 198 | public function map(array $mapping) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * |
||
| 206 | * @param string $name database name |
||
| 207 | * @return \Sokil\Mongo\Database |
||
| 208 | */ |
||
| 209 | public function getDatabase($name = null) { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Select database |
||
| 231 | * |
||
| 232 | * @param string $name |
||
| 233 | * @return \Sokil\Mongo\Client |
||
| 234 | */ |
||
| 235 | public function useDatabase($name) |
||
| 240 | |||
| 241 | public function getCurrentDatabaseName() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get collection from previously selected database by self::useDatabase() |
||
| 252 | * |
||
| 253 | * @param string $name |
||
| 254 | * @return \Sokil\Mongo\Collection |
||
| 255 | * @throws Exception |
||
| 256 | */ |
||
| 257 | public function getCollection($name) |
||
| 263 | |||
| 264 | public function readPrimaryOnly() |
||
| 269 | |||
| 270 | public function readPrimaryPreferred(array $tags = null) |
||
| 275 | |||
| 276 | public function readSecondaryOnly(array $tags = null) |
||
| 281 | |||
| 282 | public function readSecondaryPreferred(array $tags = null) |
||
| 287 | |||
| 288 | public function readNearest(array $tags = null) |
||
| 293 | |||
| 294 | public function getReadPreference() |
||
| 298 | |||
| 299 | public function setLogger(LoggerInterface $logger) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * |
||
| 307 | * @return \Psr\Log\LoggerInterface |
||
| 308 | */ |
||
| 309 | public function getLogger() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Check if logger defined |
||
| 316 | * |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | public function hasLogger() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Remove logger |
||
| 326 | * |
||
| 327 | * @return \Sokil\Mongo\Client |
||
| 328 | */ |
||
| 329 | public function removeLogger() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Enable or disable debug mode |
||
| 337 | */ |
||
| 338 | public function debug($enabled = true) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Check state of debug mode |
||
| 346 | */ |
||
| 347 | public function isDebugEnabled() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Define write concern on whole requests |
||
| 354 | * |
||
| 355 | * @param string|integer $w write concern |
||
| 356 | * @param int $timeout timeout in milliseconds |
||
| 357 | * @return \Sokil\Mongo\Client |
||
| 358 | * |
||
| 359 | * @throws \Sokil\Mongo\Exception |
||
| 360 | */ |
||
| 361 | public function setWriteConcern($w, $timeout = 10000) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Define unacknowledged write concern on whole requests |
||
| 372 | * |
||
| 373 | * @param int $timeout timeout in milliseconds |
||
| 374 | * @return \Sokil\Mongo\Client |
||
| 375 | */ |
||
| 376 | public function setUnacknowledgedWriteConcern($timeout = 10000) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Define majority write concern on whole requests |
||
| 384 | * |
||
| 385 | * @param int $timeout timeout in milliseconds |
||
| 386 | * @return \Sokil\Mongo\Client |
||
| 387 | */ |
||
| 388 | public function setMajorityWriteConcern($timeout = 10000) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Get currently active write concern on connection level |
||
| 396 | * |
||
| 397 | * @return string|int |
||
| 398 | */ |
||
| 399 | public function getWriteConcern() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Create new persistence manager |
||
| 406 | * @return \Sokil\Mongo\Persistence |
||
| 407 | */ |
||
| 408 | public function createPersistence() |
||
| 418 | } |
||
| 419 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: