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 MongoGridFS 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 MongoGridFS, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class MongoGridFS extends MongoCollection { |
||
|
1 ignored issue
–
show
|
|||
| 17 | const DEFAULT_CHUNK_SIZE = 262144; // 256 kb |
||
| 18 | |||
| 19 | const ASCENDING = 1; |
||
| 20 | const DESCENDING = -1; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.chunks |
||
| 24 | * @var $chunks MongoCollection |
||
| 25 | */ |
||
| 26 | public $chunks; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.filesname |
||
| 30 | * @var $filesName string |
||
| 31 | */ |
||
| 32 | protected $filesName; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.chunksname |
||
| 36 | * @var $chunksName string |
||
| 37 | */ |
||
| 38 | protected $chunksName; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var MongoDB |
||
| 42 | */ |
||
| 43 | protected $database; |
||
| 44 | |||
| 45 | protected $ensureIndexes = false; |
||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * Files as stored across two collections, the first containing file meta |
||
| 50 | * information, the second containing chunks of the actual file. By default, |
||
| 51 | * fs.files and fs.chunks are the collection names used. |
||
| 52 | * |
||
| 53 | * @link http://php.net/manual/en/mongogridfs.construct.php |
||
| 54 | * @param MongoDB $db Database |
||
| 55 | * @param string $prefix [optional] <p>Optional collection name prefix.</p> |
||
| 56 | * @param mixed $chunks [optional] |
||
| 57 | * @return MongoGridFS |
||
| 58 | */ |
||
| 59 | public function __construct(MongoDB $db, $prefix = "fs", $chunks = null) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Drops the files and chunks collections |
||
| 79 | * @link http://php.net/manual/en/mongogridfs.drop.php |
||
| 80 | * @return array The database response |
||
| 81 | */ |
||
| 82 | public function drop() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @link http://php.net/manual/en/mongogridfs.find.php |
||
| 90 | * @param array $query The query |
||
| 91 | * @param array $fields Fields to return |
||
| 92 | * @return MongoGridFSCursor A MongoGridFSCursor |
||
| 93 | */ |
||
| 94 | View Code Duplication | public function find(array $query = array(), array $fields = array()) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Stores a file in the database |
||
| 104 | * @link http://php.net/manual/en/mongogridfs.storefile.php |
||
| 105 | * @param string $filename The name of the file |
||
| 106 | * @param array $extra Other metadata to add to the file saved |
||
| 107 | * @param array $options Options for the store. "safe": Check that this store succeeded |
||
| 108 | * @return mixed Returns the _id of the saved object |
||
| 109 | */ |
||
| 110 | public function storeFile($filename, $extra = array(), $options = array()) |
||
| 132 | |||
| 133 | View Code Duplication | private function insertChunksFromFile($file, $fileInfo) |
|
| 147 | |||
| 148 | private function calculateMD5($file, $length) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Chunkifies and stores bytes in the database |
||
| 158 | * @link http://php.net/manual/en/mongogridfs.storebytes.php |
||
| 159 | * @param string $bytes A string of bytes to store |
||
| 160 | * @param array $extra Other metadata to add to the file saved |
||
| 161 | * @param array $options Options for the store. "safe": Check that this store succeeded |
||
| 162 | * @return mixed The _id of the object saved |
||
| 163 | */ |
||
| 164 | public function storeBytes($bytes, $extra = array(), $options = array()) |
||
| 177 | |||
| 178 | private function insertFile($metadata) |
||
| 185 | |||
| 186 | View Code Duplication | private function insertChunksFromBytes($bytes, $fileInfo) |
|
| 200 | |||
| 201 | private function insertChunk($id, $data, $chunkNumber) |
||
| 210 | |||
| 211 | |||
| 212 | /** |
||
| 213 | * Returns a single file matching the criteria |
||
| 214 | * @link http://www.php.net/manual/en/mongogridfs.findone.php |
||
| 215 | * @param array $query The fields for which to search. |
||
| 216 | * @param array $fields Fields of the results to return. |
||
| 217 | * @return MongoGridFSFile|null |
||
| 218 | */ |
||
| 219 | public function findOne(array $query = array(), array $fields = array()) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Removes files from the collections |
||
| 230 | * @link http://www.php.net/manual/en/mongogridfs.remove.php |
||
| 231 | * @param array $criteria Description of records to remove. |
||
| 232 | * @param array $options Options for remove. Valid options are: "safe"- Check that the remove succeeded. |
||
| 233 | * @throws MongoCursorException |
||
| 234 | * @return boolean |
||
| 235 | */ |
||
| 236 | public function remove(array $criteria = [], array $options = []) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Delete a file from the database |
||
| 249 | * @link http://php.net/manual/en/mongogridfs.delete.php |
||
| 250 | * @param mixed $id _id of the file to remove |
||
| 251 | * @return boolean Returns true if the remove was successfully sent to the database. |
||
| 252 | */ |
||
| 253 | public function delete($id) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Saves an uploaded file directly from a POST to the database |
||
| 267 | * @link http://www.php.net/manual/en/mongogridfs.storeupload.php |
||
| 268 | * @param string $name The name attribute of the uploaded file, from <input type="file" name="something"/>. |
||
| 269 | * @param array $metadata An array of extra fields for the uploaded file. |
||
| 270 | * @return mixed Returns the _id of the uploaded file. |
||
| 271 | */ |
||
| 272 | public function storeUpload($name, array $metadata = []) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Retrieve a file from the database |
||
| 283 | * @link http://www.php.net/manual/en/mongogridfs.get.php |
||
| 284 | * @param mixed $id _id of the file to find. |
||
| 285 | * @return MongoGridFSFile|null Returns the file, if found, or NULL. |
||
| 286 | */ |
||
| 287 | public function __get($id) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Stores a file in the database |
||
| 300 | * @link http://php.net/manual/en/mongogridfs.put.php |
||
| 301 | * @param string $filename The name of the file |
||
| 302 | * @param array $extra Other metadata to add to the file saved |
||
| 303 | * @return mixed Returns the _id of the saved object |
||
| 304 | */ |
||
| 305 | public function put($filename, array $extra = array()) |
||
| 309 | |||
| 310 | private function ensureIndexes() |
||
| 319 | |||
| 320 | private function ensureChunksIndex() |
||
| 329 | |||
| 330 | private function ensureFilesIndex() |
||
| 339 | |||
| 340 | } |
||
| 341 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.