Completed
Push — develop ( 9087a8...c9b4ef )
by Greg
16:31 queued 05:44
created

ReportPdfFootnote::getWidth()   C

Complexity

Conditions 14
Paths 36

Size

Total Lines 81
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 47
nc 36
nop 1
dl 0
loc 81
rs 5.0042
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * webtrees: online genealogy
4
 * Copyright (C) 2018 webtrees development team
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
 * GNU General Public License for more details.
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15
 */
16
namespace Fisharebest\Webtrees\Report;
17
18
/**
19
 * Class ReportPdfFootnote
20
 */
21
class ReportPdfFootnote extends ReportBaseFootnote {
22
	/**
23
	 * PDF Footnotes number renderer
24
	 *
25
	 * @param ReportTcpdf $renderer
26
	 */
27
	public function render($renderer) {
28
		$renderer->setCurrentStyle('footnotenum');
29
		$renderer->Write($renderer->getCurrentStyleHeight(), $this->numText, $this->addlink); //source link numbers after name
30
	}
31
32
	/**
33
	 * Write the Footnote text
34
	 * Uses style name "footnote" by default
35
	 *
36
	 * @param ReportTcpdf $pdf
37
	 */
38
	public function renderFootnote($pdf) {
39
		if ($pdf->getCurrentStyle() != $this->styleName) {
40
			$pdf->setCurrentStyle($this->styleName);
41
		}
42
		$temptext = str_replace('#PAGENUM#', $pdf->PageNo(), $this->text);
43
		// Set the link to this y/page position
44
		$pdf->SetLink($this->addlink, -1, -1);
45
		// Print first the source number
46
		// working
47
		if ($pdf->getRTL()) {
48
			$pdf->writeHTML('<span> .' . $this->num . '</span>', false, false, false, false, '');
49
		} else {
50
			$temptext = '<span>' . $this->num . '. </span>' . $temptext;
51
		}
52
		// underline «title» part of Source item
53
		$temptext = str_replace(['«', '»'], ['<u>', '</u>'], $temptext);
54
		$pdf->writeHTML($temptext, true, false, true, false, '');
55
	}
56
57
	/**
58
	 * Returns the height in points of the Footnote element
59
	 *
60
	 * @param ReportTcpdf $renderer
61
	 *
62
	 * @return float $h
63
	 */
64
	public function getFootnoteHeight($renderer) {
65
		return 0;
66
	}
67
68
	/**
69
	 * Splits the text into lines to fit into a giving cell
70
	 * and returns the last lines width
71
	 *
72
	 * @param ReportTcpdf $pdf
73
	 *
74
	 * @return array
75
	 */
76
	public function getWidth($pdf) {
77
		// Setup the style name, a font must be selected to calculate the width
78
		$pdf->setCurrentStyle('footnotenum');
79
80
		// Check for the largest font size in the box
81
		$fsize = $pdf->getCurrentStyleHeight();
82
		if ($fsize > $pdf->largestFontHeight) {
83
			$pdf->largestFontHeight = $fsize;
84
		}
85
86
		// Returns the Object if already numbered else false
87
		if (empty($this->num)) {
88
			$pdf->checkFootnote($this);
89
		}
90
91
		// Get the line width
92
		$lw = ceil($pdf->GetStringWidth($this->numText));
93
		// Line Feed counter - Number of lines in the text
94
		$lfct = substr_count($this->numText, "\n") + 1;
95
		// If there is still remaining wrap width...
96
		if ($this->wrapWidthRemaining > 0) {
97
			// Check with line counter too!
98
			// but floor the $wrapWidthRemaining first to keep it bugfree!
99
			$wrapWidthRemaining = (int) ($this->wrapWidthRemaining);
100
			if ($lw >= $wrapWidthRemaining || $lfct > 1) {
101
				$newtext = '';
102
				$lines   = explode("\n", $this->numText);
103
				// Go throught the text line by line
104
				foreach ($lines as $line) {
105
					// Line width in points
106
					$lw = ceil($pdf->GetStringWidth($line));
107
					// If the line has to be wraped
108
					if ($lw >= $wrapWidthRemaining) {
109
						$words    = explode(' ', $line);
110
						$addspace = count($words);
111
						$lw       = 0;
112
						foreach ($words as $word) {
113
							$addspace--;
114
							$lw += ceil($pdf->GetStringWidth($word . ' '));
115
							if ($lw < $wrapWidthRemaining) {
116
								$newtext .= $word;
117
								if ($addspace != 0) {
118
									$newtext .= ' ';
119
								}
120
							} else {
121
								$lw = $pdf->GetStringWidth($word . ' ');
122
								$newtext .= "\n$word";
123
								if ($addspace != 0) {
124
									$newtext .= ' ';
125
								}
126
								// Reset the wrap width to the cell width
127
								$wrapWidthRemaining = $this->wrapWidthCell;
128
							}
129
						}
130
					} else {
131
						$newtext .= $line;
132
					}
133
					// Check the Line Feed counter
134
					if ($lfct > 1) {
135
						// Add a new line feed as long as it’s not the last line
136
						$newtext .= "\n";
137
						// Reset the line width
138
						$lw = 0;
139
						// Reset the wrap width to the cell width
140
						$wrapWidthRemaining = $this->wrapWidthCell;
141
					}
142
					$lfct--;
143
				}
144
				$this->numText = $newtext;
145
				$lfct          = substr_count($this->numText, "\n");
146
147
				return [$lw, 1, $lfct];
148
			}
149
		}
150
		$l    = 0;
151
		$lfct = substr_count($this->numText, "\n");
152
		if ($lfct > 0) {
153
			$l = 2;
154
		}
155
156
		return [$lw, $l, $lfct];
157
	}
158
}
159