Complex classes like File 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 File, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class File |
||
14 | { |
||
15 | /** |
||
16 | * A MD5 hash of the file's path. |
||
17 | * This value is unique, and can be used to query a Song record. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $hash; |
||
22 | |||
23 | /** |
||
24 | * The file's last modified time. |
||
25 | * |
||
26 | * @var int |
||
27 | */ |
||
28 | protected $mtime; |
||
29 | |||
30 | /** |
||
31 | * The file's path. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $path; |
||
36 | |||
37 | /** |
||
38 | * The getID3 object, for ID3 tag reading. |
||
39 | * |
||
40 | * @var getID3 |
||
41 | */ |
||
42 | protected $getID3; |
||
43 | |||
44 | /** |
||
45 | * The SplFileInfo object of the file. |
||
46 | * |
||
47 | * @var SplFileInfo |
||
48 | */ |
||
49 | protected $splFileInfo; |
||
50 | |||
51 | /** |
||
52 | * The song model that's associated with this file. |
||
53 | * |
||
54 | * @var Song |
||
55 | */ |
||
56 | protected $song; |
||
57 | |||
58 | /** |
||
59 | * The last parsing error text, if any. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $syncError; |
||
64 | |||
65 | const SYNC_RESULT_SUCCESS = 1; |
||
66 | const SYNC_RESULT_BAD_FILE = 2; |
||
67 | const SYNC_RESULT_UNMODIFIED = 3; |
||
68 | |||
69 | /** |
||
70 | * Construct our File object. |
||
71 | * Upon construction, we'll set the path, hash, and associated Song object (if any). |
||
72 | * |
||
73 | * @param string|SplFileInfo $path Either the file's path, or a SplFileInfo object |
||
74 | * @param getID3 $getID3 A getID3 object for DI (and better performance) |
||
75 | */ |
||
76 | public function __construct($path, $getID3 = null) |
||
96 | |||
97 | /** |
||
98 | * Get all applicable ID3 info from the file. |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | public function getInfo() |
||
185 | |||
186 | /** |
||
187 | * Sync the song with all available media info against the database. |
||
188 | * |
||
189 | * @param array $tags The (selective) tags to sync (if the song exists) |
||
190 | * @param bool $force Whether to force syncing, even if the file is unchanged |
||
191 | * |
||
192 | * @return bool|Song A Song object on success, |
||
193 | * true if file exists but is unmodified, |
||
194 | * or false on an error. |
||
195 | */ |
||
196 | public function sync($tags, $force = false) |
||
264 | |||
265 | /** |
||
266 | * Try to generate a cover for an album based on extracted data, or use the cover file under the directory. |
||
267 | * |
||
268 | * @param Album $album |
||
269 | * @param $coverData |
||
270 | */ |
||
271 | private function generateAlbumCover(Album $album, $coverData) |
||
289 | |||
290 | /** |
||
291 | * Determine if the file is new (its Song record can't be found in the database). |
||
292 | * |
||
293 | * @return bool |
||
294 | */ |
||
295 | public function isNew() |
||
299 | |||
300 | /** |
||
301 | * Determine if the file is changed (its Song record is found, but the timestamp is different). |
||
302 | * |
||
303 | * @return bool |
||
304 | */ |
||
305 | public function isChanged() |
||
309 | |||
310 | /** |
||
311 | * Determine if the file is new or changed. |
||
312 | * |
||
313 | * @return bool |
||
314 | */ |
||
315 | public function isNewOrChanged() |
||
319 | |||
320 | /** |
||
321 | * @return getID3 |
||
322 | */ |
||
323 | public function getGetID3() |
||
327 | |||
328 | /** |
||
329 | * Get the last parsing error's text. |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | public function getSyncError() |
||
337 | |||
338 | /** |
||
339 | * @param getID3 $getID3 |
||
340 | */ |
||
341 | public function setGetID3($getID3 = null) |
||
345 | |||
346 | /** |
||
347 | * @return string |
||
348 | */ |
||
349 | public function getPath() |
||
353 | |||
354 | /** |
||
355 | * Issue #380. |
||
356 | * Some albums have its own cover image under the same directory as cover|folder.jpg/png. |
||
357 | * We'll check if such a cover file is found, and use it if positive. |
||
358 | * |
||
359 | * @throws \InvalidArgumentException |
||
360 | * |
||
361 | * @return string|false The cover file's full path, or false if none found |
||
362 | */ |
||
363 | private function getCoverFileUnderSameDirectory() |
||
392 | |||
393 | /** |
||
394 | * Get a unique hash from a file path. |
||
395 | * |
||
396 | * @param string $path |
||
397 | * |
||
398 | * @return string |
||
399 | */ |
||
400 | public static function getHash($path) |
||
404 | } |
||
405 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.