Passed
Pull Request — main (#4945)
by
unknown
06:07
created

ReportHtmlTextbox   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 285
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 167
c 1
b 0
f 0
dl 0
loc 285
rs 5.5199
wmc 56

1 Method

Rating   Name   Duplication   Size   Complexity  
F render() 0 276 56

How to fix   Complexity   

Complex Class

Complex classes like ReportHtmlTextbox 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 ReportHtmlTextbox, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2023 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Report;
21
22
use function abs;
23
use function count;
24
use function is_object;
25
use function ksort;
26
use function str_replace;
27
use function trim;
28
29
/**
30
 * Class ReportHtmlTextbox
31
 */
32
class ReportHtmlTextbox extends ReportBaseTextbox
33
{
34
    /**
35
     * Render the elements.
36
     *
37
     * @param HtmlRenderer $renderer
38
     *
39
     * @return void
40
     */
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) &&
65
                           ($element->getStyleName() === $lastelement->getStyleName())) {
66
                        // Checking if the Text has the same style
67
                        $lastelement->addText(str_replace("\n", '<br>', $element->getValue()));
68
                    } else {
69
                        $newelements[] = $lastelement;
70
                        $lastelement   = $element;
71
                    }
72
                } elseif ($element instanceof ReportHtmlImage) {
73
                    $lastelement   = $element;
74
                } elseif ($element instanceof ReportHtmlFootnote) {
75
                    // Check if the Footnote has been set with it’s link number
76
                    $renderer->checkFootnote($element);
77
                    // Save first the last element if any
78
                    if (isset($lastelement)) {
79
                        $newelements[] = $lastelement;
80
                        $lastelement   = null;
81
                    }
82
                    // Save the Footnote with it’s link number as key for sorting later
83
                    $footnote_element[$element->num] = $element;
84
                } elseif (trim($element->getValue()) !== '') {
85
                    // Do not keep empty footnotes
86
                    if (!empty($footnote_element)) {
87
                        ksort($footnote_element);
88
                        foreach ($footnote_element as $links) {
89
                            $newelements[] = $links;
90
                        }
91
                        $footnote_element = [];
92
                    }
93
                    if (isset($lastelement)) {
94
                        $newelements[] = $lastelement;
95
                        $lastelement   = null;
96
                    }
97
                    $newelements[] = $element;
98
                }
99
            } else {
100
                if (isset($lastelement)) {
101
                    $newelements[] = $lastelement;
102
                    $lastelement   = null;
103
                }
104
                if (!empty($footnote_element)) {
105
                    ksort($footnote_element);
106
                    foreach ($footnote_element as $links) {
107
                        $newelements[] = $links;
108
                    }
109
                    $footnote_element = [];
110
                }
111
                $newelements[] = $element;
112
            }
113
        }
114
        if (isset($lastelement)) {
115
            $newelements[] = $lastelement;
116
        }
117
        if (!empty($footnote_element)) {
118
            ksort($footnote_element);
119
            foreach ($footnote_element as $links) {
120
                $newelements[] = $links;
121
            }
122
        }
123
        $this->elements = $newelements;
124
        unset($footnote_element, $lastelement, $newelements);
125
126
        $cP = 0; // Class Padding
127
128
        // Used with line breaks and cell height calculation within this box only
129
        $renderer->largestFontHeight = 0;
130
131
        // If current position (left)
132
        if ($this->left === ReportBaseElement::CURRENT_POSITION) {
133
            $cX = $renderer->getX();
134
        } else {
135
            $cX = $this->left;
136
            $renderer->setX($cX);
137
        }
138
        // If current position (top)
139
        $align_Y = false;
140
        $topstr = "";
0 ignored issues
show
Unused Code introduced by
The assignment to $topstr is dead and can be removed.
Loading history...
141
        if ($this->top < -110000) { // pos='abs'
142
            $this->top += 222000;
143
        }
144
        if ($this->top < -10000) { // <= -100000: both pdf and html; -100000 -- -90000: only html
145
            $this->top += 90000;  //= ReportBaseElement::CURRENT_POSITION;
146
            if ($this->top < -9000) {
147
                $this->top += 10000;
148
            }
149
            $topstr = "top:" . $this->top . "pt;";
150
            $align_Y = true;
151
        }
152
        if ($this->top === ReportBaseElement::CURRENT_POSITION) {
153
            $this->top = $renderer->getY();
154
        } else {
155
            $renderer->setY($this->top);
156
        }
157
158
        // Check the width if set to page wide OR set by xml to larger then page width (margin)
159
        if ($this->width === 0.0 || $this->width > $renderer->getRemainingWidth()) {
160
            $this->width = $renderer->getRemainingWidth();
161
        }
162
        // Setup the CellPadding
163
        if ($this->padding) {
164
            $cP = $renderer->cPadding;
165
        }
166
167
        // For padding, we have to use less wrap width
168
        $cW = $this->width - $cP * 2.0;
169
170
        //-- calculate the text box height
171
        // Number of lines, will be converted to height
172
        $cHT = 0;
173
        // Element height (except text)
174
        $eH = 0.0;
175
        // Footnote height (in points)
176
        $fH = 0;
177
        $w  = 0;
178
        //-- $lw is an array
179
        // 0 => last line width
180
        // 1 => 1 if text was wrapped, 0 if text did not wrap
181
        // 2 => number of LF
182
        $lw = [];
183
        // Element counter
184
        $cE = count($this->elements);
185
        for ($i = 0; $i < $cE; $i++) {
186
            if (is_object($this->elements[$i])) {
187
                $ew = $this->elements[$i]->setWrapWidth($cW - $w - 2, $cW);
188
                if ($ew === $cW) {
189
                    $w = 0;
190
                }
191
                $lw = $this->elements[$i]->getWidth($renderer);
192
                // Text is already gets the # LF
193
                $cHT += $lw[2];
194
                if ($lw[1] === 1) {
195
                    $w = $lw[0];
196
                } elseif ($lw[1] === 2) {
197
                    $w = 0;
198
                } else {
199
                    $w += $lw[0];
200
                }
201
                if ($w > $cW) {
202
                    $w = $lw[0];
203
                }
204
                // For anything else but text (images), get the height
205
                $eH += $this->elements[$i]->getHeight($renderer);
206
            } else {
207
                $fH += abs($renderer->getFootnotesHeight($cW));
208
            }
209
        }
210
211
        // Add up what’s the final height
212
        //$cH = $this->height;
213
        $cH = 0;
214
        // If any element exist
215
        if ($cE > 0) {
216
            // Check if this is text or some other element, like images
217
            if ($eH === 0.0) {
0 ignored issues
show
introduced by
The condition $eH === 0.0 is always true.
Loading history...
218
                // Number of LF but at least one line
219
                $cHT = ($cHT + 1) * $renderer->cellHeightRatio;
220
                // Calculate the cell height with the largest font size used
221
                $cHT *= $renderer->largestFontHeight;
222
                if ($cH < $cHT) {
223
                    $cH = $cHT;
224
                }
225
            } else {
226
                // This is any other element
227
                if ($cH < $eH) {
228
                    $cH = $eH;
229
                }
230
                // Add Footnote height to the rest of the height
231
                $cH += $fH;
232
            }
233
        }
234
235
        unset($lw, $cHT, $fH, $w);
236
237
        // Finally, check the last cells height
238
        if ($cH < $renderer->lastCellHeight) {
239
            $cH = $renderer->lastCellHeight;
240
        }
241
        // Update max Y in case of a pagebreak
242
        // We don't want to over write any images or other stuff
243
        $renderer->addMaxY($this->top + $cH);
244
245
        // Start to print HTML
246
        if (!$align_Y) {
247
            echo '<div style="position:absolute;top:', $this->top, 'pt;';
248
        } else {
249
            echo '<div style="position:relative;top:', $this->top, 'pt;';
250
        }
251
        //echo '<div style="position:relative;';
252
        // LTR (left) or RTL (right)
253
        echo $renderer->alignRTL, ':', $cX, 'pt;';
254
        // Background color
255
        if ($this->fill && $this->bgcolor !== '') {
256
            echo ' background-color:', $this->bgcolor, ';';
257
        }
258
        // Print padding only when it’s set
259
        if ($this->padding) {
260
            // Use Cell around padding to support RTL also
261
            echo 'padding:', $cP, 'pt;';
262
        }
263
        // Border setup
264
        if ($this->border) {
265
            echo ' border:solid black 1pt;';
266
            if (!$align_Y) {
267
                echo 'width:', $this->width - 1 - $cP * 2, 'pt;height:', $cH - 1, 'pt;';
268
            } else {
269
                echo 'width:', $this->width - 1 - $cP * 2, 'pt;height:auto;';
270
            } // height:',$this->height,'pt;'; //,$topstr;
271
        } else {
272
            if (!$align_Y) {
273
                echo 'width:', $this->width - $cP * 2, 'pt;height:', $cH, 'pt;';
274
            } else {
275
                echo 'width:', $this->width - $cP * 2, 'pt;height:auto;';
276
            } //height:',$this->height,'pt;'; //,$topstr;
277
        }
278
        echo '">';
279
280
        // Do a little "margin" trick before print
281
        // to get the correct current position => "."
282
        $cXT = $renderer->getX();
283
        $cYT = $renderer->getY();
284
        $renderer->setXy(0, 0);
285
286
        // Print the text elements
287
        foreach ($this->elements as $element) {
288
            if ($element instanceof ReportHtmlText) {
289
                $element->render($renderer, false);
290
            } elseif ($element instanceof ReportBaseElement) {
291
                $element->render($renderer);
292
            } elseif ($element === 'footnotetexts') {
293
                $renderer->footnotes();
294
            } elseif ($element === 'addpage') {
295
                $renderer->addPage();
296
            }
297
        }
298
        echo "</div>\n";
299
300
        // Reset "margins"
301
        $renderer->setXy($cXT, $cYT);
302
        // This will be mostly used to trick the multiple images last height
303
        if ($this->reseth) {
304
            $cH = 0;
305
        }
306
        // New line and some clean up
307
        if (!$this->newline) {
308
            $renderer->setXy($cX + $this->width, $this->top);
309
            $renderer->lastCellHeight = $cH;
310
        } else {
311
            $renderer->setXy(0, $this->top + $cH + $cP * 2);
312
            $renderer->lastCellHeight = 0;
313
        }
314
        // This will make images in textboxes to ignore images in previous textboxes
315
        // Without this trick the $lastpicbottom in the previos textbox will be used in ReportHtmlImage
316
        $renderer->pageN++;
317
    }
318
}
319