Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like MongoClient 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 MongoClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class MongoClient |
||
30 | { |
||
31 | use Helper\ReadPreference; |
||
32 | use Helper\WriteConcern; |
||
33 | |||
34 | const VERSION = '1.6.12'; |
||
35 | const DEFAULT_HOST = "localhost"; |
||
36 | const DEFAULT_PORT = 27017; |
||
37 | const RP_PRIMARY = "primary"; |
||
38 | const RP_PRIMARY_PREFERRED = "primaryPreferred"; |
||
39 | const RP_SECONDARY = "secondary"; |
||
40 | const RP_SECONDARY_PREFERRED = "secondaryPreferred"; |
||
41 | const RP_NEAREST = "nearest"; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | * @deprecated This will not properly work as the underlying driver connects lazily |
||
46 | */ |
||
47 | public $connected = false; |
||
48 | |||
49 | /** |
||
50 | * @var |
||
51 | */ |
||
52 | public $status; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $server; |
||
58 | |||
59 | /** |
||
60 | * @var |
||
61 | */ |
||
62 | protected $persistent; |
||
63 | |||
64 | /** |
||
65 | * @var Client |
||
66 | */ |
||
67 | private $client; |
||
68 | |||
69 | /** |
||
70 | * @var \MongoDB\Driver\Manager |
||
71 | */ |
||
72 | private $manager; |
||
73 | |||
74 | /** |
||
75 | * Creates a new database connection object |
||
76 | * |
||
77 | * @link http://php.net/manual/en/mongo.construct.php |
||
78 | * @param string $server The server name. |
||
79 | * @param array $options An array of options for the connection. |
||
80 | * @param array $driverOptions An array of options for the MongoDB driver. |
||
81 | * @throws MongoConnectionException |
||
82 | */ |
||
83 | public function __construct($server = 'default', array $options = ['connect' => true], array $driverOptions = []) |
||
107 | |||
108 | |||
109 | /** |
||
110 | * Closes this database connection |
||
111 | * |
||
112 | * @link http://www.php.net/manual/en/mongoclient.close.php |
||
113 | * @param boolean|string $connection |
||
114 | * @return boolean If the connection was successfully closed. |
||
115 | */ |
||
116 | public function close($connection = null) |
||
122 | |||
123 | /** |
||
124 | * Connects to a database server |
||
125 | * |
||
126 | * @link http://www.php.net/manual/en/mongoclient.connect.php |
||
127 | * |
||
128 | * @throws MongoConnectionException |
||
129 | * @return boolean If the connection was successful. |
||
130 | */ |
||
131 | public function connect() |
||
137 | |||
138 | /** |
||
139 | * Drops a database |
||
140 | * |
||
141 | * @link http://www.php.net/manual/en/mongoclient.dropdb.php |
||
142 | * @param mixed $db The database to drop. Can be a MongoDB object or the name of the database. |
||
143 | * @return array The database response. |
||
144 | * @deprecated Use MongoDB::drop() instead. |
||
145 | */ |
||
146 | public function dropDB($db) |
||
150 | |||
151 | /** |
||
152 | * Gets a database |
||
153 | * |
||
154 | * @link http://php.net/manual/en/mongoclient.get.php |
||
155 | * @param string $dbname The database name. |
||
156 | * @return MongoDB The database name. |
||
157 | */ |
||
158 | public function __get($dbname) |
||
162 | |||
163 | /** |
||
164 | * Gets the client for this object |
||
165 | * |
||
166 | * @internal This part is not of the ext-mongo API and should not be used |
||
167 | * @return Client |
||
168 | */ |
||
169 | public function getClient() |
||
173 | |||
174 | /** |
||
175 | * Get connections |
||
176 | * |
||
177 | * Returns an array of all open connections, and information about each of the servers |
||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | public static function getConnections() |
||
185 | |||
186 | /** |
||
187 | * Get hosts |
||
188 | * |
||
189 | * This method is only useful with a connection to a replica set. It returns the status of all of the hosts in the |
||
190 | * set. Without a replica set, it will just return an array with one element containing the host that you are |
||
191 | * connected to. |
||
192 | * |
||
193 | * @return array |
||
194 | */ |
||
195 | public function getHosts() |
||
234 | |||
235 | /** |
||
236 | * Kills a specific cursor on the server |
||
237 | * |
||
238 | * @link http://www.php.net/manual/en/mongoclient.killcursor.php |
||
239 | * @param string $server_hash The server hash that has the cursor. |
||
240 | * @param int|MongoInt64 $id The ID of the cursor to kill. |
||
241 | * @return bool |
||
242 | */ |
||
243 | public function killCursor($server_hash, $id) |
||
247 | |||
248 | /** |
||
249 | * Lists all of the databases available |
||
250 | * |
||
251 | * @link http://php.net/manual/en/mongoclient.listdbs.php |
||
252 | * @return array Returns an associative array containing three fields. The first field is databases, which in turn contains an array. Each element of the array is an associative array corresponding to a database, giving the database's name, size, and if it's empty. The other two fields are totalSize (in bytes) and ok, which is 1 if this method ran successfully. |
||
253 | */ |
||
254 | public function listDBs() |
||
279 | |||
280 | /** |
||
281 | * Gets a database collection |
||
282 | * |
||
283 | * @link http://www.php.net/manual/en/mongoclient.selectcollection.php |
||
284 | * @param string $db The database name. |
||
285 | * @param string $collection The collection name. |
||
286 | * @return MongoCollection Returns a new collection object. |
||
287 | * @throws Exception Throws Exception if the database or collection name is invalid. |
||
288 | */ |
||
289 | public function selectCollection($db, $collection) |
||
293 | |||
294 | /** |
||
295 | * Gets a database |
||
296 | * |
||
297 | * @link http://www.php.net/manual/en/mongo.selectdb.php |
||
298 | * @param string $name The database name. |
||
299 | * @return MongoDB Returns a new db object. |
||
300 | * @throws InvalidArgumentException |
||
301 | */ |
||
302 | public function selectDB($name) |
||
303 | { |
||
304 | return new MongoDB($this, $name); |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * {@inheritdoc} |
||
309 | */ |
||
310 | public function setReadPreference($readPreference, $tags = null) |
||
314 | |||
315 | /** |
||
316 | * {@inheritdoc} |
||
317 | */ |
||
318 | public function setWriteConcern($wstring, $wtimeout = 0) |
||
322 | |||
323 | /** |
||
324 | * String representation of this connection |
||
325 | * |
||
326 | * @link http://www.php.net/manual/en/mongoclient.tostring.php |
||
327 | * @return string Returns hostname and port for this connection. |
||
328 | */ |
||
329 | public function __toString() |
||
333 | |||
334 | /** |
||
335 | * Forces a connection by executing the ping command |
||
336 | */ |
||
337 | private function forceConnect() |
||
346 | |||
347 | private function notImplemented() |
||
351 | |||
352 | /** |
||
353 | * @return array |
||
354 | */ |
||
355 | public function __sleep() |
||
361 | |||
362 | /** |
||
363 | * @param $server |
||
364 | * @return array |
||
365 | */ |
||
366 | private function extractUrlOptions($server) |
||
388 | |||
389 | /** |
||
390 | * @param $readPreferenceTagString |
||
391 | * @return array |
||
392 | */ |
||
393 | private function getReadPreferenceTags($readPreferenceTagString) |
||
403 | |||
404 | /** |
||
405 | * @param string $server |
||
406 | * @param array $options |
||
407 | */ |
||
408 | private function applyConnectionOptions($server, array $options) |
||
444 | } |
||
445 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.