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:
| 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 | |||
| 46 | /** |
||
| 47 | * Files as stored across two collections, the first containing file meta |
||
| 48 | * information, the second containing chunks of the actual file. By default, |
||
| 49 | * fs.files and fs.chunks are the collection names used. |
||
| 50 | * |
||
| 51 | * @link http://php.net/manual/en/mongogridfs.construct.php |
||
| 52 | * @param MongoDB $db Database |
||
| 53 | * @param string $prefix [optional] <p>Optional collection name prefix.</p> |
||
| 54 | * @param mixed $chunks [optional] |
||
| 55 | * @return MongoGridFS |
||
| 56 | */ |
||
| 57 | public function __construct(MongoDB $db, $prefix = "fs", $chunks = null) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Drops the files and chunks collections |
||
| 77 | * @link http://php.net/manual/en/mongogridfs.drop.php |
||
| 78 | * @return array The database response |
||
| 79 | */ |
||
| 80 | public function drop() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @link http://php.net/manual/en/mongogridfs.find.php |
||
| 88 | * @param array $query The query |
||
| 89 | * @param array $fields Fields to return |
||
| 90 | * @return MongoGridFSCursor A MongoGridFSCursor |
||
| 91 | */ |
||
| 92 | View Code Duplication | public function find(array $query = array(), array $fields = array()) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Stores a file in the database |
||
| 102 | * @link http://php.net/manual/en/mongogridfs.storefile.php |
||
| 103 | * @param string $filename The name of the file |
||
| 104 | * @param array $extra Other metadata to add to the file saved |
||
| 105 | * @param array $options Options for the store. "safe": Check that this store succeeded |
||
| 106 | * @return mixed Returns the _id of the saved object |
||
| 107 | */ |
||
| 108 | public function storeFile($filename, $extra = array(), $options = array()) |
||
| 130 | |||
| 131 | View Code Duplication | private function insertChunksFromFile($file, $fileInfo) |
|
| 145 | |||
| 146 | private function calculateMD5($file, $length) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Chunkifies and stores bytes in the database |
||
| 156 | * @link http://php.net/manual/en/mongogridfs.storebytes.php |
||
| 157 | * @param string $bytes A string of bytes to store |
||
| 158 | * @param array $extra Other metadata to add to the file saved |
||
| 159 | * @param array $options Options for the store. "safe": Check that this store succeeded |
||
| 160 | * @return mixed The _id of the object saved |
||
| 161 | */ |
||
| 162 | public function storeBytes($bytes, $extra = array(), $options = array()) |
||
| 175 | |||
| 176 | private function insertFile($metadata) |
||
| 182 | |||
| 183 | View Code Duplication | private function insertChunksFromBytes($bytes, $fileInfo) |
|
| 197 | |||
| 198 | private function insertChunk($id, $data, $chunkNumber) |
||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * Returns a single file matching the criteria |
||
| 211 | * @link http://www.php.net/manual/en/mongogridfs.findone.php |
||
| 212 | * @param array $query The fields for which to search. |
||
| 213 | * @param array $fields Fields of the results to return. |
||
| 214 | * @return MongoGridFSFile|null |
||
| 215 | */ |
||
| 216 | public function findOne(array $query = array(), array $fields = array()) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Removes files from the collections |
||
| 227 | * @link http://www.php.net/manual/en/mongogridfs.remove.php |
||
| 228 | * @param array $criteria Description of records to remove. |
||
| 229 | * @param array $options Options for remove. Valid options are: "safe"- Check that the remove succeeded. |
||
| 230 | * @throws MongoCursorException |
||
| 231 | * @return boolean |
||
| 232 | */ |
||
| 233 | public function remove(array $criteria = [], array $options = []) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Delete a file from the database |
||
| 246 | * @link http://php.net/manual/en/mongogridfs.delete.php |
||
| 247 | * @param mixed $id _id of the file to remove |
||
| 248 | * @return boolean Returns true if the remove was successfully sent to the database. |
||
| 249 | */ |
||
| 250 | public function delete($id) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Saves an uploaded file directly from a POST to the database |
||
| 264 | * @link http://www.php.net/manual/en/mongogridfs.storeupload.php |
||
| 265 | * @param string $name The name attribute of the uploaded file, from <input type="file" name="something"/>. |
||
| 266 | * @param array $metadata An array of extra fields for the uploaded file. |
||
| 267 | * @return mixed Returns the _id of the uploaded file. |
||
| 268 | */ |
||
| 269 | public function storeUpload($name, array $metadata = []) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Retrieve a file from the database |
||
| 280 | * @link http://www.php.net/manual/en/mongogridfs.get.php |
||
| 281 | * @param mixed $id _id of the file to find. |
||
| 282 | * @return MongoGridFSFile|null Returns the file, if found, or NULL. |
||
| 283 | */ |
||
| 284 | public function __get($id) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Stores a file in the database |
||
| 297 | * @link http://php.net/manual/en/mongogridfs.put.php |
||
| 298 | * @param string $filename The name of the file |
||
| 299 | * @param array $extra Other metadata to add to the file saved |
||
| 300 | * @return mixed Returns the _id of the saved object |
||
| 301 | */ |
||
| 302 | public function put($filename, array $extra = array()) |
||
| 306 | |||
| 307 | } |
||
| 308 |
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.