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 | /** |
||
50 | * Files as stored across two collections, the first containing file meta |
||
51 | * information, the second containing chunks of the actual file. By default, |
||
52 | * fs.files and fs.chunks are the collection names used. |
||
53 | * |
||
54 | * @link http://php.net/manual/en/mongogridfs.construct.php |
||
55 | * @param MongoDB $db Database |
||
56 | * @param string $prefix [optional] <p>Optional collection name prefix.</p> |
||
57 | * @param mixed $chunks [optional] |
||
58 | * @return MongoGridFS |
||
59 | */ |
||
60 | public function __construct(MongoDB $db, $prefix = "fs", $chunks = null) |
||
77 | |||
78 | /** |
||
79 | * Drops the files and chunks collections |
||
80 | * @link http://php.net/manual/en/mongogridfs.drop.php |
||
81 | * @return array The database response |
||
82 | */ |
||
83 | public function drop() |
||
88 | |||
89 | /** |
||
90 | * @link http://php.net/manual/en/mongogridfs.find.php |
||
91 | * @param array $query The query |
||
92 | * @param array $fields Fields to return |
||
93 | * @return MongoGridFSCursor A MongoGridFSCursor |
||
94 | */ |
||
95 | View Code Duplication | public function find(array $query = array(), array $fields = array()) |
|
102 | |||
103 | /** |
||
104 | * Stores a file in the database |
||
105 | * @link http://php.net/manual/en/mongogridfs.storefile.php |
||
106 | * @param string $filename The name of the file |
||
107 | * @param array $extra Other metadata to add to the file saved |
||
108 | * @param array $options Options for the store. "safe": Check that this store succeeded |
||
109 | * @return mixed Returns the _id of the saved object |
||
110 | */ |
||
111 | public function storeFile($filename, array $extra = array(), array $options = array()) |
||
133 | |||
134 | /** |
||
135 | * Chunkifies and stores bytes in the database |
||
136 | * @link http://php.net/manual/en/mongogridfs.storebytes.php |
||
137 | * @param string $bytes A string of bytes to store |
||
138 | * @param array $extra Other metadata to add to the file saved |
||
139 | * @param array $options Options for the store. "safe": Check that this store succeeded |
||
140 | * @return mixed The _id of the object saved |
||
141 | */ |
||
142 | public function storeBytes($bytes, array $extra = array(), array $options = array()) |
||
155 | |||
156 | |||
157 | |||
158 | /** |
||
159 | * Returns a single file matching the criteria |
||
160 | * @link http://www.php.net/manual/en/mongogridfs.findone.php |
||
161 | * @param array $query The fields for which to search. |
||
162 | * @param array $fields Fields of the results to return. |
||
163 | * @return MongoGridFSFile|null |
||
164 | */ |
||
165 | public function findOne(array $query = array(), array $fields = array()) |
||
173 | |||
174 | /** |
||
175 | * Removes files from the collections |
||
176 | * @link http://www.php.net/manual/en/mongogridfs.remove.php |
||
177 | * @param array $criteria Description of records to remove. |
||
178 | * @param array $options Options for remove. Valid options are: "safe"- Check that the remove succeeded. |
||
179 | * @throws MongoCursorException |
||
180 | * @return boolean |
||
181 | */ |
||
182 | public function remove(array $criteria = array(), array $options = array()) |
||
192 | |||
193 | /** |
||
194 | * Delete a file from the database |
||
195 | * @link http://php.net/manual/en/mongogridfs.delete.php |
||
196 | * @param mixed $id _id of the file to remove |
||
197 | * @return boolean Returns true if the remove was successfully sent to the database. |
||
198 | */ |
||
199 | public function delete($id) |
||
210 | |||
211 | /** |
||
212 | * Saves an uploaded file directly from a POST to the database |
||
213 | * @link http://www.php.net/manual/en/mongogridfs.storeupload.php |
||
214 | * @param string $name The name attribute of the uploaded file, from <input type="file" name="something"/>. |
||
215 | * @param array $metadata An array of extra fields for the uploaded file. |
||
216 | * @return mixed Returns the _id of the uploaded file. |
||
217 | */ |
||
218 | public function storeUpload($name, array $metadata = array()) |
||
226 | |||
227 | /** |
||
228 | * Retrieve a file from the database |
||
229 | * @link http://www.php.net/manual/en/mongogridfs.get.php |
||
230 | * @param mixed $id _id of the file to find. |
||
231 | * @return MongoGridFSFile|null Returns the file, if found, or NULL. |
||
232 | */ |
||
233 | public function __get($id) |
||
243 | |||
244 | /** |
||
245 | * Stores a file in the database |
||
246 | * @link http://php.net/manual/en/mongogridfs.put.php |
||
247 | * @param string $filename The name of the file |
||
248 | * @param array $extra Other metadata to add to the file saved |
||
249 | * @return mixed Returns the _id of the saved object |
||
250 | */ |
||
251 | public function put($filename, array $extra = array()) |
||
255 | |||
256 | private function ensureIndexes() |
||
265 | |||
266 | private function ensureChunksIndex() |
||
275 | |||
276 | private function ensureFilesIndex() |
||
285 | |||
286 | View Code Duplication | private function insertChunksFromFile($file, $fileInfo) |
|
300 | |||
301 | private function calculateMD5($file, $length) |
||
308 | |||
309 | View Code Duplication | private function insertChunksFromBytes($bytes, $fileInfo) |
|
323 | |||
324 | private function insertChunk($id, $data, $chunkNumber) |
||
333 | |||
334 | private function insertFile($metadata) |
||
341 | |||
342 | } |
||
343 |
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.