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 |
||
61 | class BIFFwriter |
||
62 | { |
||
63 | /** |
||
64 | * The byte order of this architecture. 0 => little endian, 1 => big endian. |
||
65 | * |
||
66 | * @var int |
||
67 | */ |
||
68 | private static $byteOrder; |
||
69 | |||
70 | /** |
||
71 | * The string containing the data of the BIFF stream. |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | public $_data; |
||
76 | |||
77 | /** |
||
78 | * The size of the data in bytes. Should be the same as strlen($this->_data). |
||
79 | * |
||
80 | * @var int |
||
81 | */ |
||
82 | public $_datasize; |
||
83 | |||
84 | /** |
||
85 | * The maximum length for a BIFF record (excluding record header and length field). See addContinue(). |
||
86 | * |
||
87 | * @var int |
||
88 | * |
||
89 | * @see addContinue() |
||
90 | */ |
||
91 | private $limit = 8224; |
||
92 | |||
93 | /** |
||
94 | * Constructor. |
||
95 | */ |
||
96 | 38 | public function __construct() |
|
101 | |||
102 | /** |
||
103 | * Determine the byte order and store it as class data to avoid |
||
104 | * recalculating it for each call to new(). |
||
105 | * |
||
106 | * @return int |
||
107 | */ |
||
108 | 38 | public static function getByteOrder() |
|
127 | |||
128 | /** |
||
129 | * General storage function. |
||
130 | * |
||
131 | * @param string $data binary data to append |
||
132 | */ |
||
133 | 38 | View Code Duplication | protected function append($data) |
141 | |||
142 | /** |
||
143 | * General storage function like append, but returns string instead of modifying $this->_data. |
||
144 | * |
||
145 | * @param string $data binary data to write |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | 38 | View Code Duplication | public function writeData($data) |
150 | { |
||
151 | 38 | if (strlen($data) - 4 > $this->limit) { |
|
152 | 7 | $data = $this->addContinue($data); |
|
153 | } |
||
154 | 38 | $this->_datasize += strlen($data); |
|
155 | |||
156 | 38 | return $data; |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * Writes Excel BOF record to indicate the beginning of a stream or |
||
161 | * sub-stream in the BIFF file. |
||
162 | * |
||
163 | * @param int $type type of BIFF file to write: 0x0005 Workbook, |
||
164 | * 0x0010 Worksheet |
||
165 | */ |
||
166 | 38 | protected function storeBof($type) |
|
183 | |||
184 | /** |
||
185 | * Writes Excel EOF record to indicate the end of a BIFF stream. |
||
186 | */ |
||
187 | 38 | View Code Duplication | protected function storeEof() |
195 | |||
196 | /** |
||
197 | * Writes Excel EOF record to indicate the end of a BIFF stream. |
||
198 | */ |
||
199 | 38 | View Code Duplication | public function writeEof() |
207 | |||
208 | /** |
||
209 | * Excel limits the size of BIFF records. In Excel 5 the limit is 2084 bytes. In |
||
210 | * Excel 97 the limit is 8228 bytes. Records that are longer than these limits |
||
211 | * must be split up into CONTINUE blocks. |
||
212 | * |
||
213 | * This function takes a long BIFF record and inserts CONTINUE records as |
||
214 | * necessary. |
||
215 | * |
||
216 | * @param string $data The original binary data to be written |
||
217 | * |
||
218 | * @return string A very convenient string of continue blocks |
||
219 | */ |
||
220 | 7 | private function addContinue($data) |
|
245 | } |
||
246 |
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.