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 PpsRoot 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 PpsRoot, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class PpsRoot extends PPS |
||
6 | { |
||
7 | /** |
||
8 | * @var |
||
9 | */ |
||
10 | protected $smallBlockSize; |
||
11 | |||
12 | /** |
||
13 | * @var |
||
14 | */ |
||
15 | protected $bigBlockSize; |
||
16 | |||
17 | /** |
||
18 | * @var |
||
19 | */ |
||
20 | protected $rootFilePointer; |
||
21 | |||
22 | /** |
||
23 | * @param integer $timestamp A timestamp |
||
24 | * @param PpsFile[] $children |
||
25 | */ |
||
26 | public function __construct( |
||
42 | |||
43 | /** |
||
44 | * Method for saving the whole OLE container (including files). |
||
45 | * |
||
46 | * @param string $filename The name of the file where to save the OLE container |
||
47 | * @throws \Exception |
||
48 | * @return boolean |
||
49 | */ |
||
50 | public function save($filename) |
||
70 | |||
71 | /** |
||
72 | * |
||
73 | */ |
||
74 | protected function setBlockSizes() |
||
79 | |||
80 | /** |
||
81 | * @param $filename |
||
82 | * |
||
83 | * @throws \Exception |
||
84 | */ |
||
85 | protected function openFile($filename) |
||
92 | |||
93 | /** |
||
94 | * @param $size |
||
95 | * @param $blockSize |
||
96 | * |
||
97 | * @return float |
||
98 | */ |
||
99 | protected function getBlocksCount($size, $blockSize) |
||
103 | |||
104 | /** |
||
105 | * Calculate some numbers |
||
106 | * |
||
107 | * @param PPS[] $list Reference to an array of PPS's |
||
108 | * |
||
109 | * @return array The array of numbers |
||
110 | */ |
||
111 | protected function calcSize($list) |
||
137 | |||
138 | /** |
||
139 | * Saving big data (PPS's with data bigger than OLE_DATA_SIZE_SMALL) |
||
140 | * |
||
141 | * @param integer $iStBlk |
||
142 | * @param PPS[] &$list Reference to array of PPS's |
||
143 | */ |
||
144 | public function saveBigData($iStBlk, &$list) |
||
164 | |||
165 | /** |
||
166 | * get small data (PPS's with data smaller than OLE_DATA_SIZE_SMALL) |
||
167 | * |
||
168 | * @param PPS[] &$list Reference to array of PPS's |
||
169 | * @return string |
||
170 | */ |
||
171 | protected function saveSmallData(&$list) |
||
212 | |||
213 | /** |
||
214 | * Saves all the PPS's WKs |
||
215 | * |
||
216 | * @param PPS[] $list Reference to an array with all PPS's |
||
217 | */ |
||
218 | protected function savePps(&$list) |
||
234 | |||
235 | /** |
||
236 | * @param $value |
||
237 | * @param int $count |
||
238 | */ |
||
239 | protected function writeUlong($value, $count = 1) |
||
244 | |||
245 | /** |
||
246 | * @param $data |
||
247 | */ |
||
248 | protected function write($data) |
||
252 | |||
253 | /** |
||
254 | * Saving Big Block Depot |
||
255 | * |
||
256 | * @param integer $numSbBlocks - number of Smallblock depot blocks |
||
257 | * @param integer $numBbBlocks - number of Bigblock depot blocks |
||
258 | * @param integer $numPpsBlocks - number of PropertySetStorage blocks |
||
259 | */ |
||
260 | protected function saveBigBlockChain($numSbBlocks, $numBbBlocks, $numPpsBlocks) |
||
329 | |||
330 | /** |
||
331 | * Save OLE header |
||
332 | * |
||
333 | * @param integer $numSbBlocks - number of Smallblock depot blocks |
||
334 | * @param integer $numBbBlocks - number of Bigblock depot blocks |
||
335 | * @param integer $numPpsBlocks - number of PropertySetStorage blocks |
||
336 | */ |
||
337 | public function saveHeader($numSbBlocks, $numBbBlocks, $numPpsBlocks) |
||
377 | |||
378 | /** |
||
379 | * New method to calculate Bigblock chain |
||
380 | * |
||
381 | * @param integer $numSb - number of Smallblock depot blocks |
||
382 | * @param integer $numBb - number of Bigblock depot blocks |
||
383 | * @param integer $numPps - number of PropertySetStorage blocks |
||
384 | * @return array |
||
385 | */ |
||
386 | protected function calcBigBlockChain($numSb, $numBb, $numPps) |
||
408 | |||
409 | /** |
||
410 | * @param array $info |
||
411 | * |
||
412 | * @return array |
||
413 | */ |
||
414 | protected function calcBigBlockChainExtra($info) |
||
437 | |||
438 | /** |
||
439 | * Calculates number of pointer blocks |
||
440 | * |
||
441 | * @param integer $numPointers - number of pointers |
||
442 | * |
||
443 | * @return int |
||
444 | */ |
||
445 | protected function getNumberOfPointerBlocks($numPointers) |
||
449 | |||
450 | /** |
||
451 | * @param int $blockSize |
||
452 | * @param int $pointerSize |
||
453 | * |
||
454 | * @return int |
||
455 | */ |
||
456 | protected function getPointersPerBlock($blockSize, $pointerSize = self::LONG_INT_SIZE) |
||
460 | } |
||
461 |
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.