Conditions | 27 |
Paths | > 20000 |
Total Lines | 132 |
Code Lines | 109 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
267 | protected function writeStylesXML() |
||
268 | { |
||
269 | $r = $this->styleFontIndexes($this->cellStyles); |
||
270 | $fills = $r['fills']; |
||
271 | $fonts = $r['fonts']; |
||
272 | $borders = $r['borders']; |
||
273 | $styleIndexes = $r['styles']; |
||
274 | |||
275 | $temporaryFilename = $this->tempFilename(); |
||
276 | $file = new XlsxWriterBuffer($temporaryFilename); |
||
277 | $file->write('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\n"); |
||
278 | $file->write('<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">'); |
||
279 | $file->write('<numFmts count="'.count($this->numberFormats).'">'); |
||
280 | foreach ($this->numberFormats as $i => $v) { |
||
281 | $file->write('<numFmt numFmtId="'.(164 + $i).'" formatCode="'.Support::xmlSpecialChars($v).'" />'); |
||
282 | } |
||
283 | $file->write('</numFmts>'); |
||
284 | $file->write('<fonts count="'.(count($fonts)).'">'); |
||
285 | $file->write('<font><name val="Arial"/><charset val="1"/><family val="2"/><sz val="10"/></font>'); |
||
286 | $file->write('<font><name val="Arial"/><family val="0"/><sz val="10"/></font>'); |
||
287 | $file->write('<font><name val="Arial"/><family val="0"/><sz val="10"/></font>'); |
||
288 | $file->write('<font><name val="Arial"/><family val="0"/><sz val="10"/></font>'); |
||
289 | |||
290 | foreach ($fonts as $font) { |
||
291 | if (!empty($font)) { //fonts have 4 empty placeholders in array to offset the 4 static xml entries above |
||
292 | $f = json_decode($font, true); |
||
293 | $file->write('<font>'); |
||
294 | $file->write('<name val="'.htmlspecialchars($f['name']).'"/><charset val="1"/><family val="'.intval($f['family']).'"/>'); |
||
295 | $file->write('<sz val="'.intval($f['size']).'"/>'); |
||
296 | if (!empty($f['color'])) { |
||
297 | $file->write('<color rgb="'.strval($f['color']).'"/>'); |
||
298 | } |
||
299 | if (!empty($f['bold'])) { |
||
300 | $file->write('<b val="true"/>'); |
||
301 | } |
||
302 | if (!empty($f['italic'])) { |
||
303 | $file->write('<i val="true"/>'); |
||
304 | } |
||
305 | if (!empty($f['underline'])) { |
||
306 | $file->write('<u val="single"/>'); |
||
307 | } |
||
308 | if (!empty($f['strike'])) { |
||
309 | $file->write('<strike val="true"/>'); |
||
310 | } |
||
311 | $file->write('</font>'); |
||
312 | } |
||
313 | } |
||
314 | $file->write('</fonts>'); |
||
315 | |||
316 | $file->write('<fills count="'.(count($fills)).'">'); |
||
317 | $file->write('<fill><patternFill patternType="none"/></fill>'); |
||
318 | $file->write('<fill><patternFill patternType="gray125"/></fill>'); |
||
319 | foreach ($fills as $fill) { |
||
320 | if (!empty($fill)) { //fills have 2 empty placeholders in array to offset the 2 static xml entries above |
||
321 | $file->write('<fill><patternFill patternType="solid"><fgColor rgb="'.strval($fill).'"/><bgColor indexed="64"/></patternFill></fill>'); |
||
322 | } |
||
323 | } |
||
324 | $file->write('</fills>'); |
||
325 | |||
326 | $file->write('<borders count="'.(count($borders)).'">'); |
||
327 | $file->write('<border diagonalDown="false" diagonalUp="false"><left/><right/><top/><bottom/><diagonal/></border>'); |
||
328 | foreach ($borders as $border) { |
||
329 | if (!empty($border)) { //fonts have an empty placeholder in the array to offset the static xml entry above |
||
330 | $pieces = json_decode($border, true); |
||
331 | $border_style = !empty($pieces['style']) ? $pieces['style'] : 'hair'; |
||
332 | $border_color = !empty($pieces['color']) ? '<color rgb="'.strval($pieces['color']).'"/>' : ''; |
||
333 | $file->write('<border diagonalDown="false" diagonalUp="false">'); |
||
334 | foreach (['left', 'right', 'top', 'bottom'] as $side) { |
||
335 | $show_side = in_array($side, $pieces['side']) ? true : false; |
||
336 | $file->write($show_side ? "<$side style=\"$border_style\">$border_color</$side>" : "<$side/>"); |
||
337 | } |
||
338 | $file->write('<diagonal/>'); |
||
339 | $file->write('</border>'); |
||
340 | } |
||
341 | } |
||
342 | $file->write('</borders>'); |
||
343 | |||
344 | $file->write('<cellStyleXfs count="20">'); |
||
345 | $file->write('<xf applyAlignment="true" applyBorder="true" applyFont="true" applyProtection="true" borderId="0" fillId="0" fontId="0" numFmtId="164">'); |
||
346 | $file->write('<alignment horizontal="general" indent="0" shrinkToFit="false" textRotation="0" vertical="bottom" wrapText="false"/>'); |
||
347 | $file->write('<protection hidden="false" locked="true"/>'); |
||
348 | $file->write('</xf>'); |
||
349 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="0"/>'); |
||
350 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="0"/>'); |
||
351 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="2" numFmtId="0"/>'); |
||
352 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="2" numFmtId="0"/>'); |
||
353 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'); |
||
354 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'); |
||
355 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'); |
||
356 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'); |
||
357 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'); |
||
358 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'); |
||
359 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'); |
||
360 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'); |
||
361 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'); |
||
362 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"/>'); |
||
363 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="43"/>'); |
||
364 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="41"/>'); |
||
365 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="44"/>'); |
||
366 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="42"/>'); |
||
367 | $file->write('<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="9"/>'); |
||
368 | $file->write('</cellStyleXfs>'); |
||
369 | |||
370 | $file->write('<cellXfs count="'.(count($styleIndexes)).'">'); |
||
371 | foreach ($styleIndexes as $v) { |
||
372 | $applyAlignment = isset($v['alignment']) ? 'true' : 'false'; |
||
373 | $wrapText = !empty($v['wrap_text']) ? 'true' : 'false'; |
||
374 | $horizAlignment = isset($v['halign']) ? $v['halign'] : 'general'; |
||
375 | $vertAlignment = isset($v['valign']) ? $v['valign'] : 'bottom'; |
||
376 | $applyBorder = isset($v['border_idx']) ? 'true' : 'false'; |
||
377 | $applyFont = 'true'; |
||
378 | $borderIdx = isset($v['border_idx']) ? intval($v['border_idx']) : 0; |
||
379 | $fillIdx = isset($v['fill_idx']) ? intval($v['fill_idx']) : 0; |
||
380 | $fontIdx = isset($v['font_idx']) ? intval($v['font_idx']) : 0; |
||
381 | $file->write('<xf applyAlignment="'.$applyAlignment.'" applyBorder="'.$applyBorder.'" applyFont="'.$applyFont.'" applyProtection="false" borderId="'.($borderIdx).'" fillId="'.($fillIdx).'" fontId="'.($fontIdx).'" numFmtId="'.(164 + $v['num_fmt_idx']).'" xfId="0">'); |
||
382 | $file->write(' <alignment horizontal="'.$horizAlignment.'" vertical="'.$vertAlignment.'" textRotation="0" wrapText="'.$wrapText.'" indent="0" shrinkToFit="false"/>'); |
||
383 | $file->write(' <protection locked="true" hidden="false"/>'); |
||
384 | $file->write('</xf>'); |
||
385 | } |
||
386 | $file->write('</cellXfs>'); |
||
387 | $file->write('<cellStyles count="6">'); |
||
388 | $file->write('<cellStyle builtinId="0" customBuiltin="false" name="Normal" xfId="0"/>'); |
||
389 | $file->write('<cellStyle builtinId="3" customBuiltin="false" name="Comma" xfId="15"/>'); |
||
390 | $file->write('<cellStyle builtinId="6" customBuiltin="false" name="Comma [0]" xfId="16"/>'); |
||
391 | $file->write('<cellStyle builtinId="4" customBuiltin="false" name="Currency" xfId="17"/>'); |
||
392 | $file->write('<cellStyle builtinId="7" customBuiltin="false" name="Currency [0]" xfId="18"/>'); |
||
393 | $file->write('<cellStyle builtinId="5" customBuiltin="false" name="Percent" xfId="19"/>'); |
||
394 | $file->write('</cellStyles>'); |
||
395 | $file->write('</styleSheet>'); |
||
396 | $file->close(); |
||
397 | |||
398 | return $temporaryFilename; |
||
399 | } |
||
664 |
If you suppress an error, we recommend checking for the error condition explicitly: