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 | private $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 | * Check if client emulates ext-mongo driver by new ext-mongodb extension |
||
| 73 | * |
||
| 74 | * @return bool |
||
| 75 | */ |
||
| 76 | public static function isEmulationMode() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Set credentials to auth on db, specified in connect options or dsn. |
||
| 83 | * If not specified - auth on admin db |
||
| 84 | * |
||
| 85 | * @param type $username |
||
| 86 | * @param type $password |
||
| 87 | * @return \Sokil\Mongo\Client |
||
| 88 | */ |
||
| 89 | public function setCredentials($username, $password) |
||
| 96 | |||
| 97 | public function __get($name) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * |
||
| 104 | * @return string Version of PHP driver |
||
| 105 | */ |
||
| 106 | public function getVersion() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * |
||
| 113 | * @return string version of mongo database |
||
| 114 | */ |
||
| 115 | public function getDbVersion() |
||
| 128 | |||
| 129 | public function setDsn($dsn) |
||
| 134 | |||
| 135 | public function getDsn() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Set connect options |
||
| 142 | * |
||
| 143 | * @link http://php.net/manual/en/mongoclient.construct.php connect options |
||
| 144 | * @param array $options |
||
| 145 | * @return \Sokil\Mongo\Client |
||
| 146 | */ |
||
| 147 | public function setConnectOptions(array $options) |
||
| 152 | |||
| 153 | public function getConnectOptions() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Set mongo's client |
||
| 160 | * |
||
| 161 | * @param \MongoClient $client |
||
| 162 | * @return \Sokil\Mongo\Client |
||
| 163 | */ |
||
| 164 | public function setMongoClient(\MongoClient $client) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get mongo connection instance |
||
| 172 | * |
||
| 173 | * @return \MongoClient |
||
| 174 | * @throws Exception |
||
| 175 | */ |
||
| 176 | public function getMongoClient() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get list of all active connections through this client |
||
| 192 | * |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | public function getConnections() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Map database and collection name to class. |
||
| 202 | * |
||
| 203 | * Collection name -> array definition: |
||
| 204 | * ['acmeDatabaseName' => ['acmeCollectionName' => ['class' => '\Acme\Collection\SomeCollectionClass']]] |
||
| 205 | * Collection name -> collection class name (deprecated: use definition array): |
||
| 206 | * ['acmeDatabaseName' => ['acmeCollectionName' => '\Acme\Collection\SomeCollectionClass']] |
||
| 207 | * Collection's class namespace (deprecated: use definition array): |
||
| 208 | * ['acmeDatabaseName' => '\Acme\Collection'] |
||
| 209 | * |
||
| 210 | * @param array $mapping classpath or class prefix |
||
| 211 | * @return Client |
||
| 212 | */ |
||
| 213 | public function map(array $mapping) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get database instance |
||
| 221 | * |
||
| 222 | * @param string $name database name |
||
| 223 | * @return Database |
||
| 224 | */ |
||
| 225 | public function getDatabase($name = null) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Select database |
||
| 247 | * |
||
| 248 | * @param string $name |
||
| 249 | * @return \Sokil\Mongo\Client |
||
| 250 | */ |
||
| 251 | public function useDatabase($name) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Get name of current database |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | * @throws Exception |
||
| 262 | */ |
||
| 263 | public function getCurrentDatabaseName() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get collection from previously selected database by self::useDatabase() |
||
| 274 | * |
||
| 275 | * @param string $name |
||
| 276 | * @return \Sokil\Mongo\Collection |
||
| 277 | * @throws Exception |
||
| 278 | */ |
||
| 279 | public function getCollection($name) |
||
| 285 | |||
| 286 | public function readPrimaryOnly() |
||
| 291 | |||
| 292 | public function readPrimaryPreferred(array $tags = null) |
||
| 297 | |||
| 298 | public function readSecondaryOnly(array $tags = null) |
||
| 303 | |||
| 304 | public function readSecondaryPreferred(array $tags = null) |
||
| 309 | |||
| 310 | public function readNearest(array $tags = null) |
||
| 315 | |||
| 316 | public function getReadPreference() |
||
| 320 | |||
| 321 | public function setLogger(LoggerInterface $logger) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * |
||
| 329 | * @return \Psr\Log\LoggerInterface |
||
| 330 | */ |
||
| 331 | public function getLogger() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Check if logger defined |
||
| 338 | * |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | public function hasLogger() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Remove logger |
||
| 348 | * |
||
| 349 | * @return \Sokil\Mongo\Client |
||
| 350 | */ |
||
| 351 | public function removeLogger() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Enable or disable debug mode |
||
| 359 | */ |
||
| 360 | public function debug($enabled = true) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Check state of debug mode |
||
| 368 | */ |
||
| 369 | public function isDebugEnabled() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Define write concern on whole requests |
||
| 376 | * |
||
| 377 | * @param string|integer $w write concern |
||
| 378 | * @param int $timeout timeout in milliseconds |
||
| 379 | * @return \Sokil\Mongo\Client |
||
| 380 | * |
||
| 381 | * @throws \Sokil\Mongo\Exception |
||
| 382 | */ |
||
| 383 | public function setWriteConcern($w, $timeout = 10000) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Define unacknowledged write concern on whole requests |
||
| 394 | * |
||
| 395 | * @param int $timeout timeout in milliseconds |
||
| 396 | * @return \Sokil\Mongo\Client |
||
| 397 | */ |
||
| 398 | public function setUnacknowledgedWriteConcern($timeout = 10000) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Define majority write concern on whole requests |
||
| 406 | * |
||
| 407 | * @param int $timeout timeout in milliseconds |
||
| 408 | * @return \Sokil\Mongo\Client |
||
| 409 | */ |
||
| 410 | public function setMajorityWriteConcern($timeout = 10000) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Get currently active write concern on connection level |
||
| 418 | * |
||
| 419 | * @return string|int |
||
| 420 | */ |
||
| 421 | public function getWriteConcern() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Create new persistence manager |
||
| 428 | * @return \Sokil\Mongo\Persistence |
||
| 429 | */ |
||
| 430 | public function createPersistence() |
||
| 440 | } |
||
| 441 |
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: