Passed
Pull Request — main (#4945)
by
unknown
05:54
created

ReportHtmlTextbox   C

Complexity

Total Complexity 54

Size/Duplication

Total Lines 284
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 166
dl 0
loc 284
rs 6.4799
c 0
b 0
f 0
wmc 54

1 Method

Rating   Name   Duplication   Size   Complexity  
F render() 0 275 54

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      = [];
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 (empty($lastelement)) {
63
                        $lastelement = $element;
64
                    } elseif ($element->getStyleName() === $lastelement->getStyleName()) {
65
                        // Checking if the Text has the same style
66
                        $lastelement->addText(str_replace("\n", '<br>', $element->getValue()));
67
                    } else {
68
                        $newelements[] = $lastelement;
69
                        $lastelement   = $element;
70
                    }
71
                } elseif ($element instanceof ReportHtmlImage) {
72
                    $lastelement   = $element;
73
                } elseif ($element instanceof ReportHtmlFootnote) {
74
                    // Check if the Footnote has been set with it’s link number
75
                    $renderer->checkFootnote($element);
76
                    // Save first the last element if any
77
                    if (!empty($lastelement)) {
78
                        $newelements[] = $lastelement;
79
                        $lastelement   = [];
80
                    }
81
                    // Save the Footnote with it’s link number as key for sorting later
82
                    $footnote_element[$element->num] = $element;
83
                } elseif (trim($element->getValue()) !== '') {
84
                    // Do not keep empty footnotes
85
                    if (!empty($footnote_element)) {
86
                        ksort($footnote_element);
87
                        foreach ($footnote_element as $links) {
88
                            $newelements[] = $links;
89
                        }
90
                        $footnote_element = [];
91
                    }
92
                    if (!empty($lastelement)) {
93
                        $newelements[] = $lastelement;
94
                        $lastelement   = [];
95
                    }
96
                    $newelements[] = $element;
97
                }
98
            } else {
99
                if (!empty($lastelement)) {
100
                    $newelements[] = $lastelement;
101
                    $lastelement   = [];
102
                }
103
                if (!empty($footnote_element)) {
104
                    ksort($footnote_element);
105
                    foreach ($footnote_element as $links) {
106
                        $newelements[] = $links;
107
                    }
108
                    $footnote_element = [];
109
                }
110
                $newelements[] = $element;
111
            }
112
        }
113
        if (!empty($lastelement)) {
114
            $newelements[] = $lastelement;
115
        }
116
        if (!empty($footnote_element)) {
117
            ksort($footnote_element);
118
            foreach ($footnote_element as $links) {
119
                $newelements[] = $links;
120
            }
121
        }
122
        $this->elements = $newelements;
0 ignored issues
show
Documentation Bug introduced by
$newelements is of type array<mixed,Fisharebest\...tHtmlImage|array|mixed>, but the property $elements was declared to be of type array<mixed,Fisharebest\...portBaseElement|string>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
123
        unset($footnote_element, $lastelement, $newelements);
124
125
        $cP = 0; // Class Padding
126
127
        // Used with line breaks and cell height calculation within this box only
128
        $renderer->largestFontHeight = 0;
129
130
        // If current position (left)
131
        if ($this->left === ReportBaseElement::CURRENT_POSITION) {
132
            $cX = $renderer->getX();
133
        } else {
134
            $cX = $this->left;
135
            $renderer->setX($cX);
136
        }
137
        // If current position (top)
138
        $align_Y = false;
139
        $topstr = "";
0 ignored issues
show
Unused Code introduced by
The assignment to $topstr is dead and can be removed.
Loading history...
140
        if ($this->top < -110000) { // pos='abs'
141
            $this->top += 222000;
142
        }
143
        if ($this->top < -10000) { // <= -100000: both pdf and html; -100000 -- -90000: only html
144
            $this->top += 90000;  //= ReportBaseElement::CURRENT_POSITION;
145
            if ($this->top < -9000) {
146
                $this->top += 10000;
147
            }
148
            $topstr = "top:" . $this->top . "pt;";
149
            $align_Y = true;
150
        }
151
        if ($this->top === ReportBaseElement::CURRENT_POSITION) {
152
            $this->top = $renderer->getY();
153
        } else {
154
            $renderer->setY($this->top);
155
        }
156
157
        // Check the width if set to page wide OR set by xml to larger then page width (margin)
158
        if ($this->width === 0.0 || $this->width > $renderer->getRemainingWidth()) {
159
            $this->width = $renderer->getRemainingWidth();
160
        }
161
        // Setup the CellPadding
162
        if ($this->padding) {
163
            $cP = $renderer->cPadding;
164
        }
165
166
        // For padding, we have to use less wrap width
167
        $cW = $this->width - $cP * 2.0;
168
169
        //-- calculate the text box height
170
        // Number of lines, will be converted to height
171
        $cHT = 0;
172
        // Element height (except text)
173
        $eH = 0.0;
174
        // Footnote height (in points)
175
        $fH = 0;
176
        $w  = 0;
177
        //-- $lw is an array
178
        // 0 => last line width
179
        // 1 => 1 if text was wrapped, 0 if text did not wrap
180
        // 2 => number of LF
181
        $lw = [];
182
        // Element counter
183
        $cE = count($this->elements);
184
        for ($i = 0; $i < $cE; $i++) {
185
            if (is_object($this->elements[$i])) {
186
                $ew = $this->elements[$i]->setWrapWidth($cW - $w - 2, $cW);
187
                if ($ew === $cW) {
188
                    $w = 0;
189
                }
190
                $lw = $this->elements[$i]->getWidth($renderer);
191
                // Text is already gets the # LF
192
                $cHT += $lw[2];
193
                if ($lw[1] === 1) {
194
                    $w = $lw[0];
195
                } elseif ($lw[1] === 2) {
196
                    $w = 0;
197
                } else {
198
                    $w += $lw[0];
199
                }
200
                if ($w > $cW) {
201
                    $w = $lw[0];
202
                }
203
                // For anything else but text (images), get the height
204
                $eH += $this->elements[$i]->getHeight($renderer);
205
            } else {
206
                $fH += abs($renderer->getFootnotesHeight($cW));
207
            }
208
        }
209
210
        // Add up what’s the final height
211
        //$cH = $this->height;
212
        $cH = 0;
213
        // If any element exist
214
        if ($cE > 0) {
215
            // Check if this is text or some other element, like images
216
            if ($eH === 0.0) {
0 ignored issues
show
introduced by
The condition $eH === 0.0 is always true.
Loading history...
217
                // Number of LF but at least one line
218
                $cHT = ($cHT + 1) * $renderer->cellHeightRatio;
219
                // Calculate the cell height with the largest font size used
220
                $cHT *= $renderer->largestFontHeight;
221
                if ($cH < $cHT) {
222
                    $cH = $cHT;
223
                }
224
            } else {
225
                // This is any other element
226
                if ($cH < $eH) {
227
                    $cH = $eH;
228
                }
229
                // Add Footnote height to the rest of the height
230
                $cH += $fH;
231
            }
232
        }
233
234
        unset($lw, $cHT, $fH, $w);
235
236
        // Finally, check the last cells height
237
        if ($cH < $renderer->lastCellHeight) {
238
            $cH = $renderer->lastCellHeight;
239
        }
240
        // Update max Y in case of a pagebreak
241
        // We don't want to over write any images or other stuff
242
        $renderer->addMaxY($this->top + $cH);
243
244
        // Start to print HTML
245
        if (!$align_Y) {
246
            echo '<div style="position:absolute;top:', $this->top, 'pt;';
247
        } else {
248
            echo '<div style="position:relative;top:', $this->top, 'pt;';
249
        }
250
        //echo '<div style="position:relative;';
251
        // LTR (left) or RTL (right)
252
        echo $renderer->alignRTL, ':', $cX, 'pt;';
253
        // Background color
254
        if ($this->fill && $this->bgcolor !== '') {
255
            echo ' background-color:', $this->bgcolor, ';';
256
        }
257
        // Print padding only when it’s set
258
        if ($this->padding) {
259
            // Use Cell around padding to support RTL also
260
            echo 'padding:', $cP, 'pt;';
261
        }
262
        // Border setup
263
        if ($this->border) {
264
            echo ' border:solid black 1pt;';
265
            if (!$align_Y) {
266
                echo 'width:', $this->width - 1 - $cP * 2, 'pt;height:', $cH - 1, 'pt;';
267
            } else {
268
                echo 'width:', $this->width - 1 - $cP * 2, 'pt;height:auto;';
269
            } // height:',$this->height,'pt;'; //,$topstr;
270
        } else {
271
            if (!$align_Y) {
272
                echo 'width:', $this->width - $cP * 2, 'pt;height:', $cH, 'pt;';
273
            } else {
274
                echo 'width:', $this->width - $cP * 2, 'pt;height:auto;';
275
            } //height:',$this->height,'pt;'; //,$topstr;
276
        }
277
        echo '">';
278
279
        // Do a little "margin" trick before print
280
        // to get the correct current position => "."
281
        $cXT = $renderer->getX();
282
        $cYT = $renderer->getY();
283
        $renderer->setXy(0, 0);
284
285
        // Print the text elements
286
        foreach ($this->elements as $element) {
287
            if ($element instanceof ReportHtmlText) {
288
                $element->render($renderer, false);
289
            } elseif ($element instanceof ReportBaseElement) {
290
                $element->render($renderer);
291
            } elseif ($element === 'footnotetexts') {
292
                $renderer->footnotes();
293
            } elseif ($element === 'addpage') {
294
                $renderer->addPage();
295
            }
296
        }
297
        echo "</div>\n";
298
299
        // Reset "margins"
300
        $renderer->setXy($cXT, $cYT);
301
        // This will be mostly used to trick the multiple images last height
302
        if ($this->reseth) {
303
            $cH = 0;
304
        }
305
        // New line and some clean up
306
        if (!$this->newline) {
307
            $renderer->setXy($cX + $this->width, $this->top);
308
            $renderer->lastCellHeight = $cH;
309
        } else {
310
            $renderer->setXy(0, $this->top + $cH + $cP * 2);
311
            $renderer->lastCellHeight = 0;
312
        }
313
        // This will make images in textboxes to ignore images in previous textboxes
314
        // Without this trick the $lastpicbottom in the previos textbox will be used in ReportHtmlImage
315
        $renderer->pageN++;
316
    }
317
}
318