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 | { |
||
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 | private $database; |
||
45 | |||
46 | private $prefix; |
||
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 | * @throws \Exception |
||
59 | */ |
||
60 | public function __construct(MongoDB $db, $prefix = "fs", $chunks = null) |
||
61 | { |
||
62 | if ($chunks) { |
||
63 | trigger_error("The 'chunks' argument is deprecated and ignored", E_DEPRECATED); |
||
64 | } |
||
65 | if (empty($prefix)) { |
||
66 | throw new \Exception('MongoGridFS::__construct(): invalid prefix'); |
||
67 | } |
||
68 | |||
69 | $this->database = $db; |
||
70 | $this->prefix = $prefix; |
||
71 | $this->filesName = $prefix . '.files'; |
||
72 | $this->chunksName = $prefix . '.chunks'; |
||
73 | |||
74 | $this->chunks = $db->selectCollection($this->chunksName); |
||
75 | |||
76 | parent::__construct($db, $this->filesName); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Delete a file from the database |
||
81 | * |
||
82 | * @link http://php.net/manual/en/mongogridfs.delete.php |
||
83 | * @param mixed $id _id of the file to remove |
||
84 | * @return boolean Returns true if the remove was successfully sent to the database. |
||
85 | */ |
||
86 | public function delete($id) |
||
87 | { |
||
88 | $this->createChunksIndex(); |
||
89 | |||
90 | $this->chunks->remove(['files_id' => $id], ['justOne' => false]); |
||
91 | return parent::remove(['_id' => $id]); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Drops the files and chunks collections |
||
96 | * @link http://php.net/manual/en/mongogridfs.drop.php |
||
97 | * @return array The database response |
||
98 | */ |
||
99 | public function drop() |
||
100 | { |
||
101 | $this->chunks->drop(); |
||
102 | return parent::drop(); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @link http://php.net/manual/en/mongogridfs.find.php |
||
107 | * @param array $query The query |
||
108 | * @param array $fields Fields to return |
||
109 | * @param array $options Options for the find command |
||
110 | * @return MongoGridFSCursor A MongoGridFSCursor |
||
111 | */ |
||
112 | View Code Duplication | public function find(array $query = [], array $fields = []) |
|
113 | { |
||
114 | $cursor = new MongoGridFSCursor($this, $this->db->getConnection(), (string) $this, $query, $fields); |
||
115 | $cursor->setReadPreference($this->getReadPreference()); |
||
116 | |||
117 | return $cursor; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Returns a single file matching the criteria |
||
122 | * |
||
123 | * @link http://www.php.net/manual/en/mongogridfs.findone.php |
||
124 | * @param array $query The fields for which to search. |
||
125 | * @param array $fields Fields of the results to return. |
||
126 | * @param array $options Options for the find command |
||
127 | * @return MongoGridFSFile|null |
||
128 | */ |
||
129 | public function findOne(array $query = [], array $fields = [], array $options = []) |
||
130 | { |
||
131 | if (is_string($query)) { |
||
132 | $query = ['filename' => $query]; |
||
133 | } |
||
134 | |||
135 | $items = iterator_to_array($this->find($query, $fields)->limit(1)); |
||
136 | return current($items); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Retrieve a file from the database |
||
141 | * |
||
142 | * @link http://www.php.net/manual/en/mongogridfs.get.php |
||
143 | * @param mixed $id _id of the file to find. |
||
144 | * @return MongoGridFSFile|null |
||
145 | */ |
||
146 | public function get($id) |
||
150 | |||
151 | /** |
||
152 | * Stores a file in the database |
||
153 | * |
||
154 | * @link http://php.net/manual/en/mongogridfs.put.php |
||
155 | * @param string $filename The name of the file |
||
156 | * @param array $extra Other metadata to add to the file saved |
||
157 | * @param array $options An array of options for the insert operations executed against the chunks and files collections. |
||
158 | * @return mixed Returns the _id of the saved object |
||
159 | */ |
||
160 | public function put($filename, array $extra = [], array $options = []) |
||
164 | |||
165 | /** |
||
166 | * Removes files from the collections |
||
167 | * |
||
168 | * @link http://www.php.net/manual/en/mongogridfs.remove.php |
||
169 | * @param array $criteria Description of records to remove. |
||
170 | * @param array $options Options for remove. |
||
171 | * @throws MongoCursorException |
||
172 | * @return boolean |
||
173 | */ |
||
174 | public function remove(array $criteria = [], array $options = []) |
||
186 | |||
187 | /** |
||
188 | * Chunkifies and stores bytes in the database |
||
189 | * @link http://php.net/manual/en/mongogridfs.storebytes.php |
||
190 | * @param string $bytes A string of bytes to store |
||
191 | * @param array $extra Other metadata to add to the file saved |
||
192 | * @param array $options Options for the store. "safe": Check that this store succeeded |
||
193 | * @return mixed The _id of the object saved |
||
194 | */ |
||
195 | public function storeBytes($bytes, array $extra = [], array $options = []) |
||
209 | |||
210 | /** |
||
211 | * Stores a file in the database |
||
212 | * |
||
213 | * @link http://php.net/manual/en/mongogridfs.storefile.php |
||
214 | * @param string $filename The name of the file |
||
215 | * @param array $extra Other metadata to add to the file saved |
||
216 | * @param array $options Options for the store. "safe": Check that this store succeeded |
||
217 | * @return mixed Returns the _id of the saved object |
||
218 | * @throws MongoGridFSException |
||
219 | * @throws Exception |
||
220 | */ |
||
221 | public function storeFile($filename, array $extra = [], array $options = []) |
||
262 | |||
263 | /** |
||
264 | * Saves an uploaded file directly from a POST to the database |
||
265 | * |
||
266 | * @link http://www.php.net/manual/en/mongogridfs.storeupload.php |
||
267 | * @param string $name The name attribute of the uploaded file, from <input type="file" name="something"/>. |
||
268 | * @param array $metadata An array of extra fields for the uploaded file. |
||
269 | * @return mixed Returns the _id of the uploaded file. |
||
270 | * @throws MongoGridFSException |
||
271 | */ |
||
272 | public function storeUpload($name, array $metadata = []) |
||
297 | |||
298 | /** |
||
299 | * Creates the index on the chunks collection |
||
300 | */ |
||
301 | private function createChunksIndex() |
||
305 | |||
306 | /** |
||
307 | * Inserts a single chunk into the database |
||
308 | * |
||
309 | * @param mixed $fileId |
||
310 | * @param string $data |
||
311 | * @param int $chunkNumber |
||
312 | * @return array|bool |
||
313 | */ |
||
314 | private function insertChunk($fileId, $data, $chunkNumber) |
||
323 | |||
324 | /** |
||
325 | * Splits a string into chunks and writes them to the database |
||
326 | * |
||
327 | * @param string $bytes |
||
328 | * @param array $record |
||
329 | */ |
||
330 | private function insertChunksFromBytes($bytes, $record) |
||
341 | |||
342 | /** |
||
343 | * Reads chunks from a file and writes them to the database |
||
344 | * |
||
345 | * @param resource $handle |
||
346 | * @param array $record |
||
347 | * @param string $md5 |
||
348 | * @return int Returns the number of bytes written to the database |
||
349 | */ |
||
350 | private function insertChunksFromFile($handle, $record, &$md5) |
||
374 | |||
375 | /** |
||
376 | * Writes a file record to the database |
||
377 | * |
||
378 | * @param $record |
||
379 | * @param array $options |
||
380 | * @return array |
||
381 | */ |
||
382 | private function insertFile($record, array $options = []) |
||
394 | } |
||
395 |
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.