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 |
||
19 | class getid3_write_id3v1 |
||
20 | { |
||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | public $filename; |
||
25 | |||
26 | /** |
||
27 | * @var int |
||
28 | */ |
||
29 | public $filesize; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | public $tag_data; |
||
35 | |||
36 | /** |
||
37 | * Any non-critical errors will be stored here. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | public $warnings = array(); |
||
42 | |||
43 | /** |
||
44 | * Any critical errors will be stored here. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | public $errors = array(); |
||
49 | |||
50 | public function __construct() { |
||
52 | |||
53 | /** |
||
54 | * @return bool |
||
55 | */ |
||
56 | public function WriteID3v1() { |
||
57 | // File MUST be writeable - CHMOD(646) at least |
||
58 | if (!empty($this->filename) && is_readable($this->filename) && getID3::is_writable($this->filename) && is_file($this->filename)) { |
||
59 | $this->setRealFileSize(); |
||
60 | View Code Duplication | if (($this->filesize <= 0) || !getid3_lib::intValueSupported($this->filesize)) { |
|
|
|||
61 | $this->errors[] = 'Unable to WriteID3v1('.$this->filename.') because filesize ('.$this->filesize.') is larger than '.round(PHP_INT_MAX / 1073741824).'GB'; |
||
62 | return false; |
||
63 | } |
||
64 | if ($fp_source = fopen($this->filename, 'r+b')) { |
||
65 | fseek($fp_source, -128, SEEK_END); |
||
66 | if (fread($fp_source, 3) == 'TAG') { |
||
67 | fseek($fp_source, -128, SEEK_END); // overwrite existing ID3v1 tag |
||
68 | } else { |
||
69 | fseek($fp_source, 0, SEEK_END); // append new ID3v1 tag |
||
70 | } |
||
71 | $this->tag_data['track_number'] = (isset($this->tag_data['track_number']) ? $this->tag_data['track_number'] : ''); |
||
72 | |||
73 | $new_id3v1_tag_data = getid3_id3v1::GenerateID3v1Tag( |
||
74 | (isset($this->tag_data['title'] ) ? $this->tag_data['title'] : ''), |
||
75 | (isset($this->tag_data['artist'] ) ? $this->tag_data['artist'] : ''), |
||
76 | (isset($this->tag_data['album'] ) ? $this->tag_data['album'] : ''), |
||
77 | (isset($this->tag_data['year'] ) ? $this->tag_data['year'] : ''), |
||
78 | (isset($this->tag_data['genreid'] ) ? $this->tag_data['genreid'] : ''), |
||
79 | (isset($this->tag_data['comment'] ) ? $this->tag_data['comment'] : ''), |
||
80 | (isset($this->tag_data['track_number']) ? $this->tag_data['track_number'] : '')); |
||
81 | fwrite($fp_source, $new_id3v1_tag_data, 128); |
||
82 | fclose($fp_source); |
||
83 | return true; |
||
84 | |||
85 | } else { |
||
86 | $this->errors[] = 'Could not fopen('.$this->filename.', "r+b")'; |
||
87 | return false; |
||
88 | } |
||
89 | } |
||
90 | $this->errors[] = 'File is not writeable: '.$this->filename; |
||
91 | return false; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return bool |
||
96 | */ |
||
97 | public function FixID3v1Padding() { |
||
119 | |||
120 | /** |
||
121 | * @return bool |
||
122 | */ |
||
123 | public function RemoveID3v1() { |
||
150 | |||
151 | /** |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function setRealFileSize() { |
||
171 | |||
172 | } |
||
173 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.