| Conditions | 56 | 
| Paths | > 20000 | 
| Total Lines | 274 | 
| Code Lines | 166 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| 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 | ||
| 41 | public function render($renderer): void | ||
| 42 |     { | ||
| 43 | static $lastBoxYfinal; | ||
| 44 | // checkFootnote | ||
| 45 | $newelements = []; | ||
| 46 | $lastelement = null; | ||
| 47 | $footnote_element = []; | ||
| 48 | // Element counter | ||
| 49 | $cE = count($this->elements); | ||
| 50 | //-- collapse duplicate elements | ||
| 51 |         for ($i = 0; $i < $cE; $i++) { | ||
| 52 | $element = $this->elements[$i]; | ||
| 53 |             if ($element instanceof ReportBaseElement) { | ||
| 54 |                 if ($element instanceof ReportBaseText) { | ||
| 55 |                     if (!empty($footnote_element)) { | ||
| 56 | ksort($footnote_element); | ||
| 57 |                         foreach ($footnote_element as $links) { | ||
| 58 | $newelements[] = $links; | ||
| 59 | } | ||
| 60 | $footnote_element = []; | ||
| 61 | } | ||
| 62 |                     if (!isset($lastelement)) { | ||
| 63 | $lastelement = $element; | ||
| 64 |                     } elseif (($element instanceof ReportBaseText && $lastelement instanceof ReportBaseText) && ($element->getStyleName() === $lastelement->getStyleName())) { | ||
| 65 |                         $lastelement->addText(str_replace("\n", '<br>', $element->getValue())); | ||
| 66 |                     } else { | ||
| 67 | $newelements[] = $lastelement; | ||
| 68 | $lastelement = $element; | ||
| 69 | } | ||
| 70 |                 } elseif ($element instanceof ReportHtmlImage) { | ||
| 71 | $lastelement = $element; | ||
| 72 |                 } elseif ($element instanceof ReportHtmlFootnote) { | ||
| 73 | // Check if the Footnote has been set with it’s link number | ||
| 74 | $renderer->checkFootnote($element); | ||
| 75 | // Save first the last element if any | ||
| 76 |                     if (isset($lastelement)) { | ||
| 77 | $newelements[] = $lastelement; | ||
| 78 | $lastelement = null; | ||
| 79 | } | ||
| 80 | // Save the Footnote with it’s link number as key for sorting later | ||
| 81 | $footnote_element[$element->num] = $element; | ||
| 82 |                 } elseif (trim($element->getValue()) !== '') { | ||
| 83 | // Do not keep empty footnotes | ||
| 84 |                     if (!empty($footnote_element)) { | ||
| 85 | ksort($footnote_element); | ||
| 86 |                         foreach ($footnote_element as $links) { | ||
| 87 | $newelements[] = $links; | ||
| 88 | } | ||
| 89 | $footnote_element = []; | ||
| 90 | } | ||
| 91 |                     if (isset($lastelement)) { | ||
| 92 | $newelements[] = $lastelement; | ||
| 93 | $lastelement = null; | ||
| 94 | } | ||
| 95 | $newelements[] = $element; | ||
| 96 | } | ||
| 97 |             } else { | ||
| 98 |                 if (isset($lastelement)) { | ||
| 99 | $newelements[] = $lastelement; | ||
| 100 | $lastelement = null; | ||
| 101 | } | ||
| 102 |                 if (!empty($footnote_element)) { | ||
| 103 | ksort($footnote_element); | ||
| 104 |                     foreach ($footnote_element as $links) { | ||
| 105 | $newelements[] = $links; | ||
| 106 | } | ||
| 107 | $footnote_element = []; | ||
| 108 | } | ||
| 109 | $newelements[] = $element; | ||
| 110 | } | ||
| 111 | } | ||
| 112 |         if (isset($lastelement)) { | ||
| 113 | $newelements[] = $lastelement; | ||
| 114 | } | ||
| 115 |         if (!empty($footnote_element)) { | ||
| 116 | ksort($footnote_element); | ||
| 117 |             foreach ($footnote_element as $links) { | ||
| 118 | $newelements[] = $links; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | $this->elements = $newelements; | ||
| 122 | unset($footnote_element, $lastelement, $newelements); | ||
| 123 | |||
| 124 | $cP = 0; // Class Padding | ||
| 125 | |||
| 126 | // Used with line breaks and cell height calculation within this box only | ||
| 127 | $renderer->largestFontHeight = 0; | ||
| 128 | |||
| 129 | // If current position (left) | ||
| 130 |         if ($this->left === ReportBaseElement::CURRENT_POSITION) { | ||
| 131 | $cX = $renderer->getX(); | ||
| 132 |         } else { | ||
| 133 | $cX = $this->left; | ||
| 134 | $renderer->setX($cX); | ||
| 135 | } | ||
| 136 | // If current position (top) | ||
| 137 | $align_Y = false; | ||
| 138 | $topstr = ""; | ||
|  | |||
| 139 |         if ($this->top < -110000) { // pos='abs' | ||
| 140 | $this->top += 222000; | ||
| 141 | } | ||
| 142 |         if ($this->top < -10000) { // <= -100000: both pdf and html; -100000 -- -90000: only html | ||
| 143 | $this->top += 90000; //= ReportBaseElement::CURRENT_POSITION; | ||
| 144 |             if ($this->top < -9000) { | ||
| 145 | $this->top += 10000; | ||
| 146 | } | ||
| 147 | $topstr = "top:" . $this->top . "pt;"; | ||
| 148 | $align_Y = true; | ||
| 149 | } | ||
| 150 |         if ($this->top === ReportBaseElement::CURRENT_POSITION) { | ||
| 151 | $this->top = $renderer->getY(); | ||
| 152 |         } else { | ||
| 153 | $renderer->setY($this->top); | ||
| 154 | } | ||
| 155 | |||
| 156 | // Check the width if set to page wide OR set by xml to larger then page width (margin) | ||
| 157 |         if ($this->width === 0.0 || $this->width > $renderer->getRemainingWidth()) { | ||
| 158 | $this->width = $renderer->getRemainingWidth(); | ||
| 159 | } | ||
| 160 | // Setup the CellPadding | ||
| 161 |         if ($this->padding) { | ||
| 162 | $cP = $renderer->cPadding; | ||
| 163 | } | ||
| 164 | |||
| 165 | // For padding, we have to use less wrap width | ||
| 166 | $cW = $this->width - $cP * 2.0; | ||
| 167 | |||
| 168 | //-- calculate the text box height | ||
| 169 | // Number of lines, will be converted to height | ||
| 170 | $cHT = 0; | ||
| 171 | // Element height (except text) | ||
| 172 | $eH = 0.0; | ||
| 173 | // Footnote height (in points) | ||
| 174 | $fH = 0; | ||
| 175 | $w = 0; | ||
| 176 | //-- $lw is an array | ||
| 177 | // 0 => last line width | ||
| 178 | // 1 => 1 if text was wrapped, 0 if text did not wrap | ||
| 179 | // 2 => number of LF | ||
| 180 | $lw = []; | ||
| 181 | // Element counter | ||
| 182 | $cE = count($this->elements); | ||
| 183 |         for ($i = 0; $i < $cE; $i++) { | ||
| 184 |             if (is_object($this->elements[$i])) { | ||
| 185 | $ew = $this->elements[$i]->setWrapWidth($cW - $w - 2, $cW); | ||
| 186 |                 if ($ew === $cW) { | ||
| 187 | $w = 0; | ||
| 188 | } | ||
| 189 | $lw = $this->elements[$i]->getWidth($renderer); | ||
| 190 | // Text is already gets the # LF | ||
| 191 | $cHT += $lw[2]; | ||
| 192 |                 if ($lw[1] === 1) { | ||
| 193 | $w = $lw[0]; | ||
| 194 |                 } elseif ($lw[1] === 2) { | ||
| 195 | $w = 0; | ||
| 196 |                 } else { | ||
| 197 | $w += $lw[0]; | ||
| 198 | } | ||
| 199 |                 if ($w > $cW) { | ||
| 200 | $w = $lw[0]; | ||
| 201 | } | ||
| 202 | // For anything else but text (images), get the height | ||
| 203 | $eH += $this->elements[$i]->getHeight($renderer); | ||
| 204 |             } else { | ||
| 205 | $fH += abs($renderer->getFootnotesHeight($cW)); | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | // Add up what’s the final height | ||
| 210 | //$cH = $this->height; | ||
| 211 | $cH = 0; | ||
| 212 | // If any element exist | ||
| 213 |         if ($cE > 0) { | ||
| 214 | // Check if this is text or some other element, like images | ||
| 215 |             if ($eH === 0.0) { | ||
| 216 | // Number of LF but at least one line | ||
| 217 | $cHT = ($cHT + 1) * $renderer->cellHeightRatio; | ||
| 218 | // Calculate the cell height with the largest font size used | ||
| 219 | $cHT *= $renderer->largestFontHeight; | ||
| 220 |                 if ($cH < $cHT) { | ||
| 221 | $cH = $cHT; | ||
| 222 | } | ||
| 223 |             } else { | ||
| 224 | // This is any other element | ||
| 225 |                 if ($cH < $eH) { | ||
| 226 | $cH = $eH; | ||
| 227 | } | ||
| 228 | // Add Footnote height to the rest of the height | ||
| 229 | $cH += $fH; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | unset($lw, $cHT, $fH, $w); | ||
| 234 | |||
| 235 | // Finally, check the last cells height | ||
| 236 |         if ($cH < $renderer->lastCellHeight) { | ||
| 237 | $cH = $renderer->lastCellHeight; | ||
| 238 | } | ||
| 239 | // Update max Y in case of a pagebreak | ||
| 240 | // We don't want to over write any images or other stuff | ||
| 241 | $renderer->addMaxY($this->top + $cH); | ||
| 242 | |||
| 243 | // Start to print HTML | ||
| 244 |         if (!$align_Y) { | ||
| 245 | echo '<div style="position:absolute;top:', $this->top, 'pt;'; | ||
| 246 |         } else { | ||
| 247 | echo '<div style="position:relative;top:', $this->top, 'pt;'; | ||
| 248 | } | ||
| 249 | //echo '<div style="position:relative;'; | ||
| 250 | // LTR (left) or RTL (right) | ||
| 251 | echo $renderer->alignRTL, ':', $cX, 'pt;'; | ||
| 252 | // Background color | ||
| 253 |         if ($this->fill && $this->bgcolor !== '') { | ||
| 254 | echo ' background-color:', $this->bgcolor, ';'; | ||
| 255 | } | ||
| 256 | // Print padding only when it’s set | ||
| 257 |         if ($this->padding) { | ||
| 258 | // Use Cell around padding to support RTL also | ||
| 259 | echo 'padding:', $cP, 'pt;'; | ||
| 260 | } | ||
| 261 | // Border setup | ||
| 262 |         if ($this->border) { | ||
| 263 | echo ' border:solid black 1pt;'; | ||
| 264 |             if (!$align_Y) { | ||
| 265 | echo 'width:', $this->width - 1 - $cP * 2, 'pt;height:', $cH - 1, 'pt;'; | ||
| 266 |             } else { | ||
| 267 | echo 'width:', $this->width - 1 - $cP * 2, 'pt;height:auto;'; | ||
| 268 | } // height:',$this->height,'pt;'; //,$topstr; | ||
| 269 |         } else { | ||
| 270 |             if (!$align_Y) { | ||
| 271 | echo 'width:', $this->width - $cP * 2, 'pt;height:', $cH, 'pt;'; | ||
| 272 |             } else { | ||
| 273 | echo 'width:', $this->width - $cP * 2, 'pt;height:auto;'; | ||
| 274 | } //height:',$this->height,'pt;'; //,$topstr; | ||
| 275 | } | ||
| 276 | echo '">'; | ||
| 277 | |||
| 278 | // Do a little "margin" trick before print | ||
| 279 | // to get the correct current position => "." | ||
| 280 | $cXT = $renderer->getX(); | ||
| 281 | $cYT = $renderer->getY(); | ||
| 282 | $renderer->setXy(0, 0); | ||
| 283 | |||
| 284 | // Print the text elements | ||
| 285 |         foreach ($this->elements as $element) { | ||
| 286 |             if ($element instanceof ReportHtmlText) { | ||
| 287 | $element->render($renderer, false); | ||
| 288 |             } elseif ($element instanceof ReportBaseElement) { | ||
| 289 | $element->render($renderer); | ||
| 290 |             } elseif ($element === 'footnotetexts') { | ||
| 291 | $renderer->footnotes(); | ||
| 292 |             } elseif ($element === 'addpage') { | ||
| 293 | $renderer->addPage(); | ||
| 294 | } | ||
| 295 | } | ||
| 296 | echo "</div>\n"; | ||
| 297 | |||
| 298 | // Reset "margins" | ||
| 299 | $renderer->setXy($cXT, $cYT); | ||
| 300 | // This will be mostly used to trick the multiple images last height | ||
| 301 |         if ($this->reseth) { | ||
| 302 | $cH = 0; | ||
| 303 | } | ||
| 304 | // New line and some clean up | ||
| 305 |         if (!$this->newline) { | ||
| 306 | $renderer->setXy($cX + $this->width, $this->top); | ||
| 307 | $renderer->lastCellHeight = $cH; | ||
| 308 |         } else { | ||
| 309 | $renderer->setXy(0, $this->top + $cH + $cP * 2); | ||
| 310 | $renderer->lastCellHeight = 0; | ||
| 311 | } | ||
| 312 | // This will make images in textboxes to ignore images in previous textboxes | ||
| 313 | // Without this trick the $lastpicbottom in the previos textbox will be used in ReportHtmlImage | ||
| 314 | $renderer->pageN++; | ||
| 315 | } | ||
| 317 |