| Conditions | 36 |
| Paths | > 20000 |
| Total Lines | 87 |
| Code Lines | 63 |
| 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 |
||
| 65 | public function styleFontIndexes(array $cellStyles) |
||
| 66 | { |
||
| 67 | $fills = ['', '']; //2 placeholders for static xml later |
||
| 68 | $fonts = ['', '', '', '']; //4 placeholders for static xml later |
||
| 69 | $borders = ['']; //1 placeholder for static xml later |
||
| 70 | $styleIndexes = []; |
||
| 71 | foreach ($cellStyles as $i => $cellStyleString) { |
||
| 72 | $semiColonPos = strpos($cellStyleString, ';'); |
||
| 73 | $numberFormatIdx = substr($cellStyleString, 0, $semiColonPos); |
||
| 74 | $styleJsonString = substr($cellStyleString, $semiColonPos + 1); |
||
| 75 | $style = @json_decode($styleJsonString, true); |
||
| 76 | $styleIndexes[$i] = ['num_fmt_idx' => $numberFormatIdx]; //initialize entry |
||
| 77 | if (isset($style['border']) && is_string($style['border'])) { //border is a comma delimited str |
||
| 78 | $borderValue['side'] = array_intersect(explode(',', $style['border']), $this->borderAllowed); |
||
| 79 | if (isset($style['border-style']) && in_array($style['border-style'], $this->borderStyleAllowed)) { |
||
| 80 | $borderValue['style'] = $style['border-style']; |
||
| 81 | } |
||
| 82 | if (isset($style['border-color']) && is_string($style['border-color']) && '#' == $style['border-color'][0]) { |
||
| 83 | $v = substr($style['border-color'], 1, 6); |
||
| 84 | $v = 3 == strlen($v) ? $v[0].$v[0].$v[1].$v[1].$v[2].$v[2] : $v; // expand cf0 => ccff00 |
||
| 85 | $border_value['color'] = 'FF'.strtoupper($v); |
||
| 86 | } |
||
| 87 | $styleIndexes[$i]['border_idx'] = Support::add2listGetIndex($borders, json_encode($borderValue)); |
||
| 88 | } |
||
| 89 | if (isset($style['fill']) && is_string($style['fill']) && '#' == $style['fill'][0]) { |
||
| 90 | $v = substr($style['fill'], 1, 6); |
||
| 91 | $v = 3 == strlen($v) ? $v[0].$v[0].$v[1].$v[1].$v[2].$v[2] : $v; // expand cf0 => ccff00 |
||
| 92 | $styleIndexes[$i]['fill_idx'] = Support::add2listGetIndex($fills, 'FF'.strtoupper($v)); |
||
| 93 | } |
||
| 94 | if (isset($style['halign']) && in_array($style['halign'], $this->horizontalAllowed)) { |
||
| 95 | $styleIndexes[$i]['alignment'] = true; |
||
| 96 | $styleIndexes[$i]['halign'] = $style['halign']; |
||
| 97 | } |
||
| 98 | if (isset($style['valign']) && in_array($style['valign'], $this->verticalAllowed)) { |
||
| 99 | $styleIndexes[$i]['alignment'] = true; |
||
| 100 | $styleIndexes[$i]['valign'] = $style['valign']; |
||
| 101 | } |
||
| 102 | if (isset($style['wrap_text'])) { |
||
| 103 | $styleIndexes[$i]['alignment'] = true; |
||
| 104 | $styleIndexes[$i]['wrap_text'] = (bool) $style['wrap_text']; |
||
| 105 | } |
||
| 106 | |||
| 107 | $font = $this->defaultFont; |
||
| 108 | if (isset($style['font-size'])) { |
||
| 109 | $font['size'] = floatval($style['font-size']); //floatval to allow "10.5" etc |
||
| 110 | } |
||
| 111 | if (isset($style['font']) && is_string($style['font'])) { |
||
| 112 | if ('Comic Sans MS' == $style['font']) { |
||
| 113 | $font['family'] = 4; |
||
| 114 | } |
||
| 115 | if ('Times New Roman' == $style['font']) { |
||
| 116 | $font['family'] = 1; |
||
| 117 | } |
||
| 118 | if ('Courier New' == $style['font']) { |
||
| 119 | $font['family'] = 3; |
||
| 120 | } |
||
| 121 | $font['name'] = strval($style['font']); |
||
| 122 | } |
||
| 123 | if (isset($style['font-style']) && is_string($style['font-style'])) { |
||
| 124 | if (false !== strpos($style['font-style'], 'bold')) { |
||
| 125 | $font['bold'] = true; |
||
| 126 | } |
||
| 127 | if (false !== strpos($style['font-style'], 'italic')) { |
||
| 128 | $font['italic'] = true; |
||
| 129 | } |
||
| 130 | if (false !== strpos($style['font-style'], 'strike')) { |
||
| 131 | $font['strike'] = true; |
||
| 132 | } |
||
| 133 | if (false !== strpos($style['font-style'], 'underline')) { |
||
| 134 | $font['underline'] = true; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | if (isset($style['color']) && is_string($style['color']) && '#' == $style['color'][0]) { |
||
| 138 | $v = substr($style['color'], 1, 6); |
||
| 139 | $v = 3 == strlen($v) ? $v[0].$v[0].$v[1].$v[1].$v[2].$v[2] : $v; // expand cf0 => ccff00 |
||
| 140 | $font['color'] = 'FF'.strtoupper($v); |
||
| 141 | } |
||
| 142 | if ($font != $this->defaultFont) { |
||
| 143 | $styleIndexes[$i]['font_idx'] = Support::add2listGetIndex($fonts, json_encode($font)); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | return [ |
||
| 148 | 'fills' => $fills, |
||
| 149 | 'fonts' => $fonts, |
||
| 150 | 'borders' => $borders, |
||
| 151 | 'styles' => $styleIndexes, |
||
| 152 | ]; |
||
| 155 |