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 | { |
||
| 18 | const DEFAULT_CHUNK_SIZE = 262144; // 256 kb |
||
| 19 | |||
| 20 | const ASCENDING = 1; |
||
| 21 | const DESCENDING = -1; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.chunks |
||
| 25 | * @var $chunks MongoCollection |
||
| 26 | */ |
||
| 27 | public $chunks; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.filesname |
||
| 31 | * @var $filesName string |
||
| 32 | */ |
||
| 33 | protected $filesName; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.chunksname |
||
| 37 | * @var $chunksName string |
||
| 38 | */ |
||
| 39 | protected $chunksName; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var MongoDB |
||
| 43 | */ |
||
| 44 | protected $database; |
||
| 45 | |||
| 46 | protected $ensureIndexes = false; |
||
| 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, array $extra = array(), array $options = array()) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Chunkifies and stores bytes in the database |
||
| 135 | * @link http://php.net/manual/en/mongogridfs.storebytes.php |
||
| 136 | * @param string $bytes A string of bytes to store |
||
| 137 | * @param array $extra Other metadata to add to the file saved |
||
| 138 | * @param array $options Options for the store. "safe": Check that this store succeeded |
||
| 139 | * @return mixed The _id of the object saved |
||
| 140 | */ |
||
| 141 | public function storeBytes($bytes, array $extra = array(), array $options = array()) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Returns a single file matching the criteria |
||
| 157 | * @link http://www.php.net/manual/en/mongogridfs.findone.php |
||
| 158 | * @param array $query The fields for which to search. |
||
| 159 | * @param array $fields Fields of the results to return. |
||
| 160 | * @return MongoGridFSFile|null |
||
| 161 | */ |
||
| 162 | public function findOne(array $query = [], array $fields = [], array $options = []) |
||
| 163 | { |
||
| 164 | $file = parent::findOne($query, $fields); |
||
| 165 | if (! $file) { |
||
| 166 | return; |
||
| 167 | } |
||
| 168 | return new MongoGridFSFile($this, $file); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Removes files from the collections |
||
| 173 | * @link http://www.php.net/manual/en/mongogridfs.remove.php |
||
| 174 | * @param array $criteria Description of records to remove. |
||
| 175 | * @param array $options Options for remove. Valid options are: "safe"- Check that the remove succeeded. |
||
| 176 | * @throws MongoCursorException |
||
| 177 | * @return boolean |
||
| 178 | */ |
||
| 179 | public function remove(array $criteria = [], array $options = []) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Delete a file from the database |
||
| 192 | * @link http://php.net/manual/en/mongogridfs.delete.php |
||
| 193 | * @param mixed $id _id of the file to remove |
||
| 194 | * @return boolean Returns true if the remove was successfully sent to the database. |
||
| 195 | */ |
||
| 196 | public function delete($id) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Saves an uploaded file directly from a POST to the database |
||
| 210 | * @link http://www.php.net/manual/en/mongogridfs.storeupload.php |
||
| 211 | * @param string $name The name attribute of the uploaded file, from <input type="file" name="something"/>. |
||
| 212 | * @param array $metadata An array of extra fields for the uploaded file. |
||
| 213 | * @return mixed Returns the _id of the uploaded file. |
||
| 214 | */ |
||
| 215 | public function storeUpload($name, array $metadata = array()) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Retrieve a file from the database |
||
| 226 | * @link http://www.php.net/manual/en/mongogridfs.get.php |
||
| 227 | * @param mixed $id _id of the file to find. |
||
| 228 | * @return MongoGridFSFile|null Returns the file, if found, or NULL. |
||
| 229 | */ |
||
| 230 | public function __get($id) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Stores a file in the database |
||
| 243 | * @link http://php.net/manual/en/mongogridfs.put.php |
||
| 244 | * @param string $filename The name of the file |
||
| 245 | * @param array $extra Other metadata to add to the file saved |
||
| 246 | * @return mixed Returns the _id of the saved object |
||
| 247 | */ |
||
| 248 | public function put($filename, array $extra = array()) |
||
| 252 | |||
| 253 | private function ensureIndexes() |
||
| 262 | |||
| 263 | private function ensureChunksIndex() |
||
| 272 | |||
| 273 | private function ensureFilesIndex() |
||
| 282 | |||
| 283 | private function insertChunksFromFile($file, $fileInfo) |
||
| 299 | |||
| 300 | private function calculateMD5($file) |
||
| 308 | |||
| 309 | private function insertChunksFromBytes($bytes, $fileInfo) |
||
| 321 | |||
| 322 | private function insertChunk($id, $data, $chunkNumber) |
||
| 331 | |||
| 332 | private function insertFile($metadata) |
||
| 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.