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 ElggDiskFilestore 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 ElggDiskFilestore, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class ElggDiskFilestore extends \ElggFilestore { |
||
12 | /** |
||
13 | * Directory root. |
||
14 | */ |
||
15 | private $dir_root; |
||
16 | |||
17 | /** |
||
18 | * Number of entries per matrix dir. |
||
19 | * You almost certainly don't want to change this. |
||
20 | */ |
||
21 | const BUCKET_SIZE = 5000; |
||
22 | |||
23 | /** |
||
24 | * Global Elgg configuration |
||
25 | * |
||
26 | * @var \stdClass |
||
27 | */ |
||
28 | private $CONFIG; |
||
29 | |||
30 | /** |
||
31 | * Construct a disk filestore using the given directory root. |
||
32 | * |
||
33 | * @param string $directory_root Root directory, must end in "/" |
||
34 | */ |
||
35 | public function __construct($directory_root = "") { |
||
45 | |||
46 | /** |
||
47 | * Open a file for reading, writing, or both. |
||
48 | * |
||
49 | * @note All files are opened binary safe. |
||
50 | * @note This will try to create the a directory if it doesn't exist and is opened |
||
51 | * in write or append mode. |
||
52 | * |
||
53 | * @param \ElggFile $file The file to open |
||
54 | * @param string $mode read, write, or append. |
||
55 | * |
||
56 | * @throws InvalidParameterException |
||
57 | * @return resource File pointer resource |
||
58 | * @todo This really shouldn't try to create directories if not writing. |
||
59 | */ |
||
60 | public function open(\ElggFile $file, $mode) { |
||
103 | |||
104 | /** |
||
105 | * Write data to a file. |
||
106 | * |
||
107 | * @param resource $f File pointer resource |
||
108 | * @param mixed $data The data to write. |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | public function write($f, $data) { |
||
115 | |||
116 | /** |
||
117 | * Read data from a file. |
||
118 | * |
||
119 | * @param resource $f File pointer resource |
||
120 | * @param int $length The number of bytes to read |
||
121 | * @param int $offset The number of bytes to start after |
||
122 | * |
||
123 | * @return mixed Contents of file or false on fail. |
||
124 | */ |
||
125 | public function read($f, $length, $offset = 0) { |
||
132 | |||
133 | /** |
||
134 | * Close a file pointer |
||
135 | * |
||
136 | * @param resource $f A file pointer resource |
||
137 | * |
||
138 | * @return bool |
||
139 | */ |
||
140 | public function close($f) { |
||
143 | |||
144 | /** |
||
145 | * Delete an \ElggFile file. |
||
146 | * |
||
147 | * @param \ElggFile $file File to delete |
||
148 | * |
||
149 | * @return bool |
||
150 | */ |
||
151 | public function delete(\ElggFile $file) { |
||
159 | |||
160 | /** |
||
161 | * Seek to the specified position. |
||
162 | * |
||
163 | * @param resource $f File resource |
||
164 | * @param int $position Position in bytes |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | public function seek($f, $position) { |
||
171 | |||
172 | /** |
||
173 | * Return the current location of the internal pointer |
||
174 | * |
||
175 | * @param resource $f File pointer resource |
||
176 | * |
||
177 | * @return int|false |
||
178 | */ |
||
179 | public function tell($f) { |
||
182 | |||
183 | /** |
||
184 | * Tests for end of file on a file pointer |
||
185 | * |
||
186 | * @param resource $f File pointer resource |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | public function eof($f) { |
||
193 | |||
194 | /** |
||
195 | * Returns the file size of an \ElggFile file. |
||
196 | * |
||
197 | * @param \ElggFile $file File object |
||
198 | * |
||
199 | * @return int The file size |
||
200 | */ |
||
201 | public function getFileSize(\ElggFile $file) { |
||
204 | |||
205 | /** |
||
206 | * Get the filename as saved on disk for an \ElggFile object |
||
207 | * |
||
208 | * Returns an empty string if no filename set |
||
209 | * |
||
210 | * @param \ElggFile $file File object |
||
211 | * |
||
212 | * @return string The full path of where the file is stored |
||
213 | * @throws InvalidParameterException |
||
214 | */ |
||
215 | public function getFilenameOnFilestore(\ElggFile $file) { |
||
235 | |||
236 | /** |
||
237 | * Returns the contents of the \ElggFile file. |
||
238 | * |
||
239 | * @param \ElggFile $file File object |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | public function grabFile(\ElggFile $file) { |
||
246 | |||
247 | /** |
||
248 | * Tests if an \ElggFile file exists. |
||
249 | * |
||
250 | * @param \ElggFile $file File object |
||
251 | * |
||
252 | * @return bool |
||
253 | */ |
||
254 | public function exists(\ElggFile $file) { |
||
260 | |||
261 | /** |
||
262 | * Returns the size of all data stored under a directory in the disk store. |
||
263 | * |
||
264 | * @param string $prefix The prefix to check under. |
||
265 | * @param string $container_guid The guid of the entity whose data you want to check. |
||
266 | * |
||
267 | * @return int|false |
||
268 | */ |
||
269 | public function getSize($prefix, $container_guid) { |
||
277 | |||
278 | // @codingStandardsIgnoreStart |
||
279 | /** |
||
280 | * Create a directory $dirroot |
||
281 | * |
||
282 | * @param string $dirroot The full path of the directory to create |
||
283 | * |
||
284 | * @throws IOException |
||
285 | * @return true |
||
286 | * @deprecated 1.8 Use \ElggDiskFilestore::makeDirectoryRoot() |
||
287 | */ |
||
288 | protected function make_directory_root($dirroot) { |
||
293 | // @codingStandardsIgnoreEnd |
||
294 | |||
295 | /** |
||
296 | * Create a directory $dirroot |
||
297 | * |
||
298 | * @param string $dirroot The full path of the directory to create |
||
299 | * |
||
300 | * @throws IOException |
||
301 | * @return true |
||
302 | */ |
||
303 | protected function makeDirectoryRoot($dirroot) { |
||
312 | |||
313 | /** |
||
314 | * Returns a list of attributes to save to the database when saving |
||
315 | * the \ElggFile object using this file store. |
||
316 | * |
||
317 | * @return array |
||
318 | */ |
||
319 | public function getParameters() { |
||
322 | |||
323 | /** |
||
324 | * Sets parameters that should be saved to database. |
||
325 | * |
||
326 | * @param array $parameters Set parameters to save to DB for this filestore. |
||
327 | * |
||
328 | * @return bool |
||
329 | */ |
||
330 | public function setParameters(array $parameters) { |
||
338 | |||
339 | |||
340 | |||
341 | /** |
||
342 | * Deprecated methods |
||
343 | */ |
||
344 | |||
345 | // @codingStandardsIgnoreStart |
||
346 | /** |
||
347 | * Construct a file path matrix for an entity. |
||
348 | * |
||
349 | * @param int $identifier The guid of the entity to store the data under. |
||
350 | * |
||
351 | * @return string The path where the entity's data will be stored. |
||
352 | * @deprecated 1.8 Use \Elgg\EntityDirLocator |
||
353 | */ |
||
354 | protected function make_file_matrix($identifier) { |
||
359 | // @codingStandardsIgnoreEnd |
||
360 | |||
361 | // @codingStandardsIgnoreStart |
||
362 | /** |
||
363 | * Construct a filename matrix. |
||
364 | * |
||
365 | * Generates a matrix using the entity's creation time and |
||
366 | * unique guid. |
||
367 | * |
||
368 | * @param int $guid The entity to contrust a matrix for |
||
369 | * |
||
370 | * @deprecated 1.8 Use \ElggDiskFileStore::makeFileMatrix() |
||
371 | * @return string The |
||
372 | */ |
||
373 | protected function user_file_matrix($guid) { |
||
378 | // @codingStandardsIgnoreEnd |
||
379 | |||
380 | // @codingStandardsIgnoreStart |
||
381 | /** |
||
382 | * Multibyte string tokeniser. |
||
383 | * |
||
384 | * Splits a string into an array. Will fail safely if mbstring is |
||
385 | * not installed. |
||
386 | * |
||
387 | * @param string $string String |
||
388 | * @param string $charset The charset, defaults to UTF8 |
||
389 | * |
||
390 | * @return array |
||
391 | * @deprecated 1.8 Files are stored by date and guid; no need for this. |
||
392 | */ |
||
393 | View Code Duplication | private function mb_str_split($string, $charset = 'UTF8') { |
|
412 | // @codingStandardsIgnoreEnd |
||
413 | |||
414 | /** |
||
415 | * Construct a file path matrix for an entity. |
||
416 | * |
||
417 | * @param int $guid The guid of the entity to store the data under. |
||
418 | * |
||
419 | * @return string The path where the entity's data will be stored relative to the data dir. |
||
420 | * @deprecated 1.9 Use \Elgg\EntityDirLocator() |
||
421 | */ |
||
422 | protected function makeFileMatrix($guid) { |
||
433 | } |
||
434 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.