Passed
Pull Request — main (#4945)
by
unknown
08:24
created

ReportHtmlTextbox   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 283
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
F render() 0 274 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) && ($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 = "";
0 ignored issues
show
Unused Code introduced by
The assignment to $topstr is dead and can be removed.
Loading history...
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) {
0 ignored issues
show
introduced by
The condition $eH === 0.0 is always true.
Loading history...
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
    }
316
}
317