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 |
||
| 20 | class MongoGridFS extends MongoCollection |
||
| 21 | { |
||
| 22 | const ASCENDING = 1; |
||
| 23 | const DESCENDING = -1; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.chunks |
||
| 27 | * @var $chunks MongoCollection |
||
| 28 | */ |
||
| 29 | public $chunks; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.filesname |
||
| 33 | * @var $filesName string |
||
| 34 | */ |
||
| 35 | protected $filesName; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.chunksname |
||
| 39 | * @var $chunksName string |
||
| 40 | */ |
||
| 41 | protected $chunksName; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var MongoDB |
||
| 45 | */ |
||
| 46 | private $database; |
||
| 47 | |||
| 48 | private $prefix; |
||
| 49 | |||
| 50 | private $defaultChunkSize = 261120; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Files as stored across two collections, the first containing file meta |
||
| 54 | * information, the second containing chunks of the actual file. By default, |
||
| 55 | * fs.files and fs.chunks are the collection names used. |
||
| 56 | * |
||
| 57 | * @link http://php.net/manual/en/mongogridfs.construct.php |
||
| 58 | * @param MongoDB $db Database |
||
| 59 | * @param string $prefix [optional] <p>Optional collection name prefix.</p> |
||
| 60 | * @param mixed $chunks [optional] |
||
| 61 | * @throws \Exception |
||
| 62 | */ |
||
| 63 | public function __construct(MongoDB $db, $prefix = "fs", $chunks = null) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Delete a file from the database |
||
| 84 | * |
||
| 85 | * @link http://php.net/manual/en/mongogridfs.delete.php |
||
| 86 | * @param mixed $id _id of the file to remove |
||
| 87 | * @return boolean Returns true if the remove was successfully sent to the database. |
||
| 88 | */ |
||
| 89 | public function delete($id) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Drops the files and chunks collections |
||
| 99 | * @link http://php.net/manual/en/mongogridfs.drop.php |
||
| 100 | * @return array The database response |
||
| 101 | */ |
||
| 102 | public function drop() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @link http://php.net/manual/en/mongogridfs.find.php |
||
| 110 | * @param array $query The query |
||
| 111 | * @param array $fields Fields to return |
||
| 112 | * @param array $options Options for the find command |
||
| 113 | * @return MongoGridFSCursor A MongoGridFSCursor |
||
| 114 | */ |
||
| 115 | View Code Duplication | public function find(array $query = [], array $fields = []) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Returns a single file matching the criteria |
||
| 125 | * |
||
| 126 | * @link http://www.php.net/manual/en/mongogridfs.findone.php |
||
| 127 | * @param mixed $query The fields for which to search or a filename to search for. |
||
| 128 | * @param array $fields Fields of the results to return. |
||
| 129 | * @param array $options Options for the find command |
||
| 130 | * @return MongoGridFSFile|null |
||
| 131 | */ |
||
| 132 | public function findOne($query = [], array $fields = [], array $options = []) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Retrieve a file from the database |
||
| 144 | * |
||
| 145 | * @link http://www.php.net/manual/en/mongogridfs.get.php |
||
| 146 | * @param mixed $id _id of the file to find. |
||
| 147 | * @return MongoGridFSFile|null |
||
| 148 | */ |
||
| 149 | public function get($id) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Stores a file in the database |
||
| 156 | * |
||
| 157 | * @link http://php.net/manual/en/mongogridfs.put.php |
||
| 158 | * @param string $filename The name of the file |
||
| 159 | * @param array $extra Other metadata to add to the file saved |
||
| 160 | * @param array $options An array of options for the insert operations executed against the chunks and files collections. |
||
| 161 | * @return mixed Returns the _id of the saved object |
||
| 162 | */ |
||
| 163 | public function put($filename, array $extra = [], array $options = []) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Removes files from the collections |
||
| 170 | * |
||
| 171 | * @link http://www.php.net/manual/en/mongogridfs.remove.php |
||
| 172 | * @param array $criteria Description of records to remove. |
||
| 173 | * @param array $options Options for remove. |
||
| 174 | * @throws MongoCursorException |
||
| 175 | * @return boolean |
||
| 176 | */ |
||
| 177 | public function remove(array $criteria = [], array $options = []) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Chunkifies and stores bytes in the database |
||
| 192 | * @link http://php.net/manual/en/mongogridfs.storebytes.php |
||
| 193 | * @param string $bytes A string of bytes to store |
||
| 194 | * @param array $extra Other metadata to add to the file saved |
||
| 195 | * @param array $options Options for the store. "safe": Check that this store succeeded |
||
| 196 | * @return mixed The _id of the object saved |
||
| 197 | */ |
||
| 198 | public function storeBytes($bytes, array $extra = [], array $options = []) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Stores a file in the database |
||
| 225 | * |
||
| 226 | * @link http://php.net/manual/en/mongogridfs.storefile.php |
||
| 227 | * @param string $filename The name of the file |
||
| 228 | * @param array $extra Other metadata to add to the file saved |
||
| 229 | * @param array $options Options for the store. "safe": Check that this store succeeded |
||
| 230 | * @return mixed Returns the _id of the saved object |
||
| 231 | * @throws MongoGridFSException |
||
| 232 | * @throws Exception |
||
| 233 | */ |
||
| 234 | public function storeFile($filename, array $extra = [], array $options = []) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Saves an uploaded file directly from a POST to the database |
||
| 301 | * |
||
| 302 | * @link http://www.php.net/manual/en/mongogridfs.storeupload.php |
||
| 303 | * @param string $name The name attribute of the uploaded file, from <input type="file" name="something"/>. |
||
| 304 | * @param array $metadata An array of extra fields for the uploaded file. |
||
| 305 | * @return mixed Returns the _id of the uploaded file. |
||
| 306 | * @throws MongoGridFSException |
||
| 307 | */ |
||
| 308 | public function storeUpload($name, array $metadata = []) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Creates the index on the chunks collection |
||
| 336 | */ |
||
| 337 | private function createChunksIndex() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Inserts a single chunk into the database |
||
| 347 | * |
||
| 348 | * @param mixed $fileId |
||
| 349 | * @param string $data |
||
| 350 | * @param int $chunkNumber |
||
| 351 | * @return array|bool |
||
| 352 | */ |
||
| 353 | private function insertChunk($fileId, $data, $chunkNumber) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Splits a string into chunks and writes them to the database |
||
| 372 | * |
||
| 373 | * @param string $bytes |
||
| 374 | * @param array $record |
||
| 375 | */ |
||
| 376 | private function insertChunksFromBytes($bytes, $record) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Reads chunks from a file and writes them to the database |
||
| 390 | * |
||
| 391 | * @param resource $handle |
||
| 392 | * @param array $record |
||
| 393 | * @param string $md5 |
||
| 394 | * @return int Returns the number of bytes written to the database |
||
| 395 | */ |
||
| 396 | private function insertChunksFromFile($handle, $record, &$md5) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Writes a file record to the database |
||
| 423 | * |
||
| 424 | * @param $record |
||
| 425 | * @param array $options |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | private function insertFile($record, array $options = []) |
||
| 444 | |||
| 445 | private function isOKResult($result) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @return array |
||
| 453 | */ |
||
| 454 | public function __sleep() |
||
| 458 | } |
||
| 459 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.