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 |
||
23 | class MongoGridFS extends MongoCollection |
||
24 | { |
||
25 | const ASCENDING = 1; |
||
26 | const DESCENDING = -1; |
||
27 | |||
28 | /** |
||
29 | * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.chunks |
||
30 | * @var $chunks MongoCollection |
||
31 | */ |
||
32 | public $chunks; |
||
33 | |||
34 | /** |
||
35 | * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.filesname |
||
36 | * @var $filesName string |
||
37 | */ |
||
38 | protected $filesName; |
||
39 | |||
40 | /** |
||
41 | * @link http://php.net/manual/en/class.mongogridfs.php#mongogridfs.props.chunksname |
||
42 | * @var $chunksName string |
||
43 | */ |
||
44 | protected $chunksName; |
||
45 | |||
46 | /** |
||
47 | * @var MongoDB |
||
48 | */ |
||
49 | private $database; |
||
50 | |||
51 | private $prefix; |
||
52 | |||
53 | private $defaultChunkSize = 261120; |
||
|
|||
54 | |||
55 | /** |
||
56 | * @var \MongoDB\GridFS\Bucket |
||
57 | */ |
||
58 | private $bucket; |
||
59 | |||
60 | /** |
||
61 | * Files as stored across two collections, the first containing file meta |
||
62 | * information, the second containing chunks of the actual file. By default, |
||
63 | * fs.files and fs.chunks are the collection names used. |
||
64 | * |
||
65 | * @link http://php.net/manual/en/mongogridfs.construct.php |
||
66 | * @param MongoDB $db Database |
||
67 | * @param string $prefix [optional] <p>Optional collection name prefix.</p> |
||
68 | * @param mixed $chunks [optional] |
||
69 | * @throws \Exception |
||
70 | */ |
||
71 | public function __construct(MongoDB $db, $prefix = "fs", $chunks = null) |
||
90 | |||
91 | /** |
||
92 | * Delete a file from the database |
||
93 | * |
||
94 | * @link http://php.net/manual/en/mongogridfs.delete.php |
||
95 | * @param mixed $id _id of the file to remove |
||
96 | * @return boolean Returns true if the remove was successfully sent to the database. |
||
97 | */ |
||
98 | public function delete($id) |
||
108 | |||
109 | /** |
||
110 | * Drops the files and chunks collections |
||
111 | * @link http://php.net/manual/en/mongogridfs.drop.php |
||
112 | * @return array The database response |
||
113 | */ |
||
114 | public function drop() |
||
120 | |||
121 | /** |
||
122 | * @link http://php.net/manual/en/mongogridfs.find.php |
||
123 | * @param array $query The query |
||
124 | * @param array $fields Fields to return |
||
125 | * @param array $options Options for the find command |
||
126 | * @return MongoGridFSCursor A MongoGridFSCursor |
||
127 | */ |
||
128 | View Code Duplication | public function find(array $query = [], array $fields = []) |
|
135 | |||
136 | /** |
||
137 | * Returns a single file matching the criteria |
||
138 | * |
||
139 | * @link http://www.php.net/manual/en/mongogridfs.findone.php |
||
140 | * @param mixed $query The fields for which to search or a filename to search for. |
||
141 | * @param array $fields Fields of the results to return. |
||
142 | * @param array $options Options for the find command |
||
143 | * @return MongoGridFSFile|null |
||
144 | */ |
||
145 | public function findOne($query = [], array $fields = [], array $options = []) |
||
154 | |||
155 | /** |
||
156 | * Retrieve a file from the database |
||
157 | * |
||
158 | * @link http://www.php.net/manual/en/mongogridfs.get.php |
||
159 | * @param mixed $id _id of the file to find. |
||
160 | * @return MongoGridFSFile|null |
||
161 | */ |
||
162 | public function get($id) |
||
166 | |||
167 | /** |
||
168 | * Stores a file in the database |
||
169 | * |
||
170 | * @link http://php.net/manual/en/mongogridfs.put.php |
||
171 | * @param string $filename The name of the file |
||
172 | * @param array $extra Other metadata to add to the file saved |
||
173 | * @param array $options An array of options for the insert operations executed against the chunks and files collections. |
||
174 | * @return mixed Returns the _id of the saved object |
||
175 | */ |
||
176 | public function put($filename, array $extra = [], array $options = []) |
||
180 | |||
181 | /** |
||
182 | * Removes files from the collections |
||
183 | * |
||
184 | * @link http://www.php.net/manual/en/mongogridfs.remove.php |
||
185 | * @param array $criteria Description of records to remove. |
||
186 | * @param array $options Options for remove. |
||
187 | * @throws MongoCursorException |
||
188 | * @return boolean |
||
189 | */ |
||
190 | public function remove(array $criteria = [], array $options = []) |
||
200 | |||
201 | /** |
||
202 | * Chunkifies and stores bytes in the database |
||
203 | * @link http://php.net/manual/en/mongogridfs.storebytes.php |
||
204 | * @param string $bytes A string of bytes to store |
||
205 | * @param array $extra Other metadata to add to the file saved |
||
206 | * @param array $options Options for the store. "safe": Check that this store succeeded |
||
207 | * @return mixed The _id of the object saved |
||
208 | */ |
||
209 | public function storeBytes($bytes, array $extra = [], array $options = []) |
||
225 | |||
226 | /** |
||
227 | * Stores a file in the database |
||
228 | * |
||
229 | * @link http://php.net/manual/en/mongogridfs.storefile.php |
||
230 | * @param string $filename The name of the file |
||
231 | * @param array $extra Other metadata to add to the file saved |
||
232 | * @param array $options Options for the store. "safe": Check that this store succeeded |
||
233 | * @return mixed Returns the _id of the saved object |
||
234 | * @throws MongoGridFSException |
||
235 | * @throws Exception |
||
236 | */ |
||
237 | public function storeFile($filename, array $extra = [], array $options = []) |
||
260 | |||
261 | /** |
||
262 | * Saves an uploaded file directly from a POST to the database |
||
263 | * |
||
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 | * @throws MongoGridFSException |
||
269 | */ |
||
270 | public function storeUpload($name, array $metadata = []) |
||
295 | |||
296 | /** |
||
297 | * @internal |
||
298 | * @return \MongoDB\GridFS\Bucket |
||
299 | */ |
||
300 | public function getBucket() |
||
304 | |||
305 | /** |
||
306 | * @return array |
||
307 | */ |
||
308 | public function __sleep() |
||
312 | } |
||
313 |
This check marks private properties in classes that are never used. Those properties can be removed.