Total Complexity | 78 |
Total Lines | 428 |
Duplicated Lines | 11.45 % |
Coverage | 78.43% |
Changes | 0 |
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 Root 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.
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 Root, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class Root extends PPS |
||
35 | { |
||
36 | /** |
||
37 | * Directory for temporary files. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $tempDirectory; |
||
42 | |||
43 | /** |
||
44 | * @var resource |
||
45 | */ |
||
46 | private $fileHandle; |
||
47 | |||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | private $tempFilename; |
||
52 | |||
53 | /** |
||
54 | * @var int |
||
55 | */ |
||
56 | private $smallBlockSize; |
||
57 | |||
58 | /** |
||
59 | * @var int |
||
60 | */ |
||
61 | private $bigBlockSize; |
||
62 | |||
63 | /** |
||
64 | * @param int $time_1st A timestamp |
||
65 | * @param int $time_2nd A timestamp |
||
66 | * @param File[] $raChild |
||
67 | */ |
||
68 | 39 | public function __construct($time_1st, $time_2nd, $raChild) |
|
69 | { |
||
70 | 39 | $this->tempDirectory = \PhpOffice\PhpSpreadsheet\Shared\File::sysGetTempDir(); |
|
71 | |||
72 | 39 | parent::__construct(null, OLE::ascToUcs('Root Entry'), OLE::OLE_PPS_TYPE_ROOT, null, null, null, $time_1st, $time_2nd, null, $raChild); |
|
73 | 39 | } |
|
74 | |||
75 | /** |
||
76 | * Method for saving the whole OLE container (including files). |
||
77 | * In fact, if called with an empty argument (or '-'), it saves to a |
||
78 | * temporary file and then outputs it's contents to stdout. |
||
79 | * If a resource pointer to a stream created by fopen() is passed |
||
80 | * it will be used, but you have to close such stream by yourself. |
||
81 | * |
||
82 | * @param resource|string $filename the name of the file or stream where to save the OLE container |
||
83 | * |
||
84 | * @throws WriterException |
||
85 | * |
||
86 | * @return bool true on success |
||
87 | */ |
||
88 | 39 | public function save($filename) |
|
89 | { |
||
90 | // Initial Setting for saving |
||
91 | 39 | $this->bigBlockSize = pow( |
|
92 | 39 | 2, |
|
93 | 39 | (isset($this->bigBlockSize)) ? self::adjust2($this->bigBlockSize) : 9 |
|
94 | ); |
||
95 | 39 | $this->smallBlockSize = pow( |
|
96 | 39 | 2, |
|
97 | 39 | (isset($this->smallBlockSize)) ? self::adjust2($this->smallBlockSize) : 6 |
|
98 | ); |
||
99 | |||
100 | 39 | if (is_resource($filename)) { |
|
101 | $this->fileHandle = $filename; |
||
102 | 39 | } elseif ($filename == '-' || $filename == '') { |
|
103 | if ($this->tempDirectory === null) { |
||
104 | $this->tempDirectory = \PhpOffice\PhpSpreadsheet\Shared\File::sysGetTempDir(); |
||
105 | } |
||
106 | $this->tempFilename = tempnam($this->tempDirectory, 'OLE_PPS_Root'); |
||
|
|||
107 | $this->fileHandle = fopen($this->tempFilename, 'w+b'); |
||
1 ignored issue
–
show
|
|||
108 | if ($this->fileHandle == false) { |
||
109 | throw new WriterException("Can't create temporary file."); |
||
110 | } |
||
111 | } else { |
||
112 | 39 | $this->fileHandle = fopen($filename, 'wb'); |
|
113 | } |
||
114 | 39 | if ($this->fileHandle == false) { |
|
115 | throw new WriterException("Can't open $filename. It may be in use or protected."); |
||
116 | } |
||
117 | // Make an array of PPS's (for Save) |
||
118 | 39 | $aList = []; |
|
119 | 39 | PPS::_savePpsSetPnt($aList, [$this]); |
|
120 | // calculate values for header |
||
121 | 39 | list($iSBDcnt, $iBBcnt, $iPPScnt) = $this->_calcSize($aList); //, $rhInfo); |
|
122 | // Save Header |
||
123 | 39 | $this->_saveHeader($iSBDcnt, $iBBcnt, $iPPScnt); |
|
124 | |||
125 | // Make Small Data string (write SBD) |
||
126 | 39 | $this->_data = $this->_makeSmallData($aList); |
|
127 | |||
128 | // Write BB |
||
129 | 39 | $this->_saveBigData($iSBDcnt, $aList); |
|
130 | // Write PPS |
||
131 | 39 | $this->_savePps($aList); |
|
132 | // Write Big Block Depot and BDList and Adding Header informations |
||
133 | 39 | $this->_saveBbd($iSBDcnt, $iBBcnt, $iPPScnt); |
|
134 | |||
135 | 39 | if (!is_resource($filename)) { |
|
136 | 39 | fclose($this->fileHandle); |
|
137 | } |
||
138 | |||
139 | 39 | return true; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Calculate some numbers. |
||
144 | * |
||
145 | * @param array $raList Reference to an array of PPS's |
||
146 | * |
||
147 | * @return float[] The array of numbers |
||
148 | */ |
||
149 | 39 | public function _calcSize(&$raList) |
|
150 | { |
||
151 | // Calculate Basic Setting |
||
152 | 39 | list($iSBDcnt, $iBBcnt, $iPPScnt) = [0, 0, 0]; |
|
153 | 39 | $iSmallLen = 0; |
|
154 | 39 | $iSBcnt = 0; |
|
155 | 39 | $iCount = count($raList); |
|
156 | 39 | for ($i = 0; $i < $iCount; ++$i) { |
|
157 | 39 | if ($raList[$i]->Type == OLE::OLE_PPS_TYPE_FILE) { |
|
158 | 39 | $raList[$i]->Size = $raList[$i]->getDataLen(); |
|
159 | 39 | if ($raList[$i]->Size < OLE::OLE_DATA_SIZE_SMALL) { |
|
160 | 39 | $iSBcnt += floor($raList[$i]->Size / $this->smallBlockSize) |
|
161 | 39 | + (($raList[$i]->Size % $this->smallBlockSize) ? 1 : 0); |
|
162 | } else { |
||
163 | 21 | $iBBcnt += (floor($raList[$i]->Size / $this->bigBlockSize) + |
|
164 | 21 | (($raList[$i]->Size % $this->bigBlockSize) ? 1 : 0)); |
|
165 | } |
||
166 | } |
||
167 | } |
||
168 | 39 | $iSmallLen = $iSBcnt * $this->smallBlockSize; |
|
169 | 39 | $iSlCnt = floor($this->bigBlockSize / OLE::OLE_LONG_INT_SIZE); |
|
170 | 39 | $iSBDcnt = floor($iSBcnt / $iSlCnt) + (($iSBcnt % $iSlCnt) ? 1 : 0); |
|
171 | 39 | $iBBcnt += (floor($iSmallLen / $this->bigBlockSize) + |
|
172 | 39 | (($iSmallLen % $this->bigBlockSize) ? 1 : 0)); |
|
173 | 39 | $iCnt = count($raList); |
|
174 | 39 | $iBdCnt = $this->bigBlockSize / OLE::OLE_PPS_SIZE; |
|
175 | 39 | $iPPScnt = (floor($iCnt / $iBdCnt) + (($iCnt % $iBdCnt) ? 1 : 0)); |
|
176 | |||
177 | 39 | return [$iSBDcnt, $iBBcnt, $iPPScnt]; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * Helper function for caculating a magic value for block sizes. |
||
182 | * |
||
183 | * @param int $i2 The argument |
||
184 | * |
||
185 | * @see save() |
||
186 | * |
||
187 | * @return float |
||
188 | */ |
||
189 | private static function adjust2($i2) |
||
190 | { |
||
191 | $iWk = log($i2) / log(2); |
||
192 | |||
193 | return ($iWk > floor($iWk)) ? floor($iWk) + 1 : $iWk; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Save OLE header. |
||
198 | * |
||
199 | * @param int $iSBDcnt |
||
200 | * @param int $iBBcnt |
||
201 | * @param int $iPPScnt |
||
202 | */ |
||
203 | 39 | public function _saveHeader($iSBDcnt, $iBBcnt, $iPPScnt) |
|
272 | } |
||
273 | } |
||
274 | 39 | } |
|
275 | |||
276 | /** |
||
277 | * Saving big data (PPS's with data bigger than \PhpOffice\PhpSpreadsheet\Shared\OLE::OLE_DATA_SIZE_SMALL). |
||
278 | * |
||
279 | * @param int $iStBlk |
||
280 | * @param array &$raList Reference to array of PPS's |
||
281 | */ |
||
282 | 39 | public function _saveBigData($iStBlk, &$raList) |
|
302 | } |
||
303 | } |
||
304 | } |
||
305 | 39 | } |
|
306 | |||
307 | /** |
||
308 | * get small data (PPS's with data smaller than \PhpOffice\PhpSpreadsheet\Shared\OLE::OLE_DATA_SIZE_SMALL). |
||
309 | * |
||
310 | * @param array &$raList Reference to array of PPS's |
||
311 | */ |
||
312 | 39 | public function _makeSmallData(&$raList) |
|
313 | { |
||
314 | 39 | $sRes = ''; |
|
315 | 39 | $FILE = $this->fileHandle; |
|
316 | 39 | $iSmBlk = 0; |
|
317 | |||
318 | 39 | $iCount = count($raList); |
|
319 | 39 | for ($i = 0; $i < $iCount; ++$i) { |
|
320 | // Make SBD, small data string |
||
321 | 39 | if ($raList[$i]->Type == OLE::OLE_PPS_TYPE_FILE) { |
|
322 | 39 | if ($raList[$i]->Size <= 0) { |
|
323 | continue; |
||
324 | } |
||
325 | 39 | if ($raList[$i]->Size < OLE::OLE_DATA_SIZE_SMALL) { |
|
326 | 39 | $iSmbCnt = floor($raList[$i]->Size / $this->smallBlockSize) |
|
327 | 39 | + (($raList[$i]->Size % $this->smallBlockSize) ? 1 : 0); |
|
328 | // Add to SBD |
||
329 | 39 | $jB = $iSmbCnt - 1; |
|
330 | 39 | View Code Duplication | for ($j = 0; $j < $jB; ++$j) { |
331 | 39 | fwrite($FILE, pack('V', $j + $iSmBlk + 1)); |
|
332 | } |
||
333 | 39 | fwrite($FILE, pack('V', -2)); |
|
334 | |||
335 | // Add to Data String(this will be written for RootEntry) |
||
336 | 39 | $sRes .= $raList[$i]->_data; |
|
337 | 39 | View Code Duplication | if ($raList[$i]->Size % $this->smallBlockSize) { |
338 | 39 | $sRes .= str_repeat("\x00", $this->smallBlockSize - ($raList[$i]->Size % $this->smallBlockSize)); |
|
339 | } |
||
340 | // Set for PPS |
||
341 | 39 | $raList[$i]->startBlock = $iSmBlk; |
|
342 | 39 | $iSmBlk += $iSmbCnt; |
|
343 | } |
||
344 | } |
||
345 | } |
||
346 | 39 | $iSbCnt = floor($this->bigBlockSize / OLE::OLE_LONG_INT_SIZE); |
|
347 | 39 | if ($iSmBlk % $iSbCnt) { |
|
348 | 39 | $iB = $iSbCnt - ($iSmBlk % $iSbCnt); |
|
349 | 39 | View Code Duplication | for ($i = 0; $i < $iB; ++$i) { |
350 | 39 | fwrite($FILE, pack('V', -1)); |
|
351 | } |
||
352 | } |
||
353 | |||
354 | 39 | return $sRes; |
|
355 | } |
||
356 | |||
357 | /** |
||
358 | * Saves all the PPS's WKs. |
||
359 | * |
||
360 | * @param array $raList Reference to an array with all PPS's |
||
361 | */ |
||
362 | 39 | public function _savePps(&$raList) |
|
363 | { |
||
364 | // Save each PPS WK |
||
365 | 39 | $iC = count($raList); |
|
366 | 39 | for ($i = 0; $i < $iC; ++$i) { |
|
367 | 39 | fwrite($this->fileHandle, $raList[$i]->_getPpsWk()); |
|
368 | } |
||
369 | // Adjust for Block |
||
370 | 39 | $iCnt = count($raList); |
|
371 | 39 | $iBCnt = $this->bigBlockSize / OLE::OLE_PPS_SIZE; |
|
372 | 39 | if ($iCnt % $iBCnt) { |
|
373 | fwrite($this->fileHandle, str_repeat("\x00", ($iBCnt - ($iCnt % $iBCnt)) * OLE::OLE_PPS_SIZE)); |
||
374 | } |
||
375 | 39 | } |
|
376 | |||
377 | /** |
||
378 | * Saving Big Block Depot. |
||
379 | * |
||
380 | * @param int $iSbdSize |
||
381 | * @param int $iBsize |
||
382 | * @param int $iPpsCnt |
||
383 | */ |
||
384 | 39 | public function _saveBbd($iSbdSize, $iBsize, $iPpsCnt) |
|
462 | } |
||
463 | 39 | } |
|
464 | } |
||
465 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.