Total Complexity | 53 |
Total Lines | 386 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 0 |
Complex classes like InlineBox 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 InlineBox, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class InlineBox extends ElementBox implements BoxInterface, BuildTreeInterface, AppendChildInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var \YetiForcePDF\Layout\TextBox |
||
27 | */ |
||
28 | protected $previousTextBox; |
||
29 | /** |
||
30 | * Parent width cache. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $parentWidth = '0'; |
||
35 | /** |
||
36 | * Parent height cache. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $parentHeight = '0'; |
||
41 | |||
42 | /** |
||
43 | * Go up to Line box and clone and wrap element. |
||
44 | * |
||
45 | * @param Box $box |
||
46 | * |
||
47 | * @return Box |
||
48 | */ |
||
49 | public function cloneParent(Box $box) |
||
50 | { |
||
51 | if ($parent = $this->getParent()) { |
||
52 | $clone = clone $this; |
||
53 | $clone->getStyle()->setBox($clone); |
||
54 | $clone->getDimensions()->setBox($clone); |
||
55 | $clone->getOffset()->setBox($clone); |
||
56 | $clone->getElement()->setBox($clone); |
||
57 | $clone->getCoordinates()->setBox($clone); |
||
58 | $clone->appendChild($box); |
||
59 | if (!$parent instanceof LineBox) { |
||
60 | $parent->cloneParent($clone); |
||
|
|||
61 | } else { |
||
62 | $parent->appendChild($clone); |
||
63 | } |
||
64 | } |
||
65 | return $box; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function appendBlockBox($childDomElement, $element, $style, $parentBlock) |
||
72 | { |
||
73 | $box = (new BlockBox()) |
||
74 | ->setDocument($this->document) |
||
75 | ->setParent($this) |
||
76 | ->setElement($element) |
||
77 | ->setStyle($style) |
||
78 | ->init(); |
||
79 | // if we add this child to parent box we loose parent inline styles if nested |
||
80 | // so we need to wrap this box later and split lines at block element |
||
81 | if (isset($this->getChildren()[0])) { |
||
82 | $this->cloneParent($box); |
||
83 | } else { |
||
84 | $this->appendChild($box); |
||
85 | } |
||
86 | $box->getStyle()->init(); |
||
87 | $box->buildTree($box); |
||
88 | return $box; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function appendTableWrapperBox($childDomElement, $element, $style, $parentBlock) |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function appendInlineBlockBox($childDomElement, $element, $style, $parentBlock) |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function appendInlineBox($childDomElement, $element, $style, $parentBlock) |
||
141 | { |
||
142 | $box = (new self()) |
||
143 | ->setDocument($this->document) |
||
144 | ->setParent($this) |
||
145 | ->setElement($element) |
||
146 | ->setStyle($style) |
||
147 | ->init(); |
||
148 | if (isset($this->getChildren()[0])) { |
||
149 | $this->cloneParent($box); |
||
150 | } else { |
||
151 | $this->appendChild($box); |
||
152 | } |
||
153 | $box->getStyle()->init(); |
||
154 | $box->buildTree($parentBlock); |
||
155 | return $box; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Create text. |
||
160 | * |
||
161 | * @param $content |
||
162 | * @param bool $sameId |
||
163 | * |
||
164 | * @return $this |
||
165 | */ |
||
166 | public function createText($content, bool $sameId = false) |
||
167 | { |
||
168 | if ($sameId && $this->previousTextBox) { |
||
169 | $box = $this->previousTextBox->clone(); |
||
170 | } else { |
||
171 | $box = (new TextBox()) |
||
172 | ->setDocument($this->document) |
||
173 | ->setParent($this) |
||
174 | ->init(); |
||
175 | } |
||
176 | $box->setText($content); |
||
177 | $this->previousTextBox = $box; |
||
178 | if (isset($this->getChildren()[0])) { |
||
179 | $this->previousTextBox = $this->cloneParent($box); |
||
180 | } else { |
||
181 | $this->appendChild($box); |
||
182 | $this->previousTextBox = $box; |
||
183 | } |
||
184 | return $box; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Get previous sibling inline-level element text. |
||
189 | * |
||
190 | * @return string|null |
||
191 | */ |
||
192 | protected function getPreviousText() |
||
193 | { |
||
194 | $closest = $this->getClosestLineBox()->getLastChild(); |
||
195 | $previousTop = $closest->getPrevious(); |
||
196 | if ($previousTop && $textBox = $previousTop->getFirstTextBox()) { |
||
197 | return $textBox->getText(); |
||
198 | } |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Add text. |
||
203 | * |
||
204 | * @param \DOMNode $childDomElement |
||
205 | * @param Element $element |
||
206 | * @param Style $style |
||
207 | * @param \YetiForcePDF\Layout\BlockBox|null $parentBlock |
||
208 | * |
||
209 | * @return $this |
||
210 | */ |
||
211 | public function appendText($childDomElement, $element = null, $style = null, $parentBlock = null) |
||
212 | { |
||
213 | $text = $childDomElement->textContent; |
||
214 | $whiteSpace = $this->getStyle()->getRules('white-space'); |
||
215 | switch ($whiteSpace) { |
||
216 | case 'normal': |
||
217 | case 'nowrap': |
||
218 | $text = preg_replace('/([\t ]+)?\r([\t ]+)?/u', "\r", $text); |
||
219 | $text = preg_replace('/\r+/u', ' ', $text); |
||
220 | $text = preg_replace('/\t+/u', ' ', $text); |
||
221 | $text = preg_replace('/ +/u', ' ', $text); |
||
222 | break; |
||
223 | } |
||
224 | if ('' !== $text) { |
||
225 | if ('normal' === $whiteSpace) { |
||
226 | $words = preg_split('/ /u', $text, 0); |
||
227 | $count = \count($words); |
||
228 | if ($count) { |
||
229 | foreach ($words as $index => $word) { |
||
230 | if ('' !== $word) { |
||
231 | $this->createText($word); |
||
232 | $parent = $this->getParent(); |
||
233 | $anonymous = ($parent instanceof self && $parent->isAnonymous()) || $parent instanceof LineBox; |
||
234 | if ($index + 1 !== $count || $anonymous) { |
||
235 | $this->createText(' ', true); |
||
236 | } |
||
237 | } else { |
||
238 | $this->createText(' ', true); |
||
239 | } |
||
240 | } |
||
241 | } else { |
||
242 | $this->createText(' ', true); |
||
243 | } |
||
244 | } elseif ('nowrap' === $whiteSpace) { |
||
245 | $this->createText($text, true); |
||
246 | } |
||
247 | } |
||
248 | return $this; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Measure width. |
||
253 | * |
||
254 | * @param bool $afterPageDividing |
||
255 | * |
||
256 | * @return $this |
||
257 | */ |
||
258 | public function measureWidth(bool $afterPageDividing = false) |
||
259 | { |
||
260 | $style = $this->getStyle(); |
||
261 | if ($this->parentWidth === $this->getParent()->getDimensions()->getWidth() && null !== $this->getDimensions()->getWidth()) { |
||
262 | if (!$this->isForMeasurement()) { |
||
263 | $this->getDimensions()->setWidth(Math::add($style->getHorizontalBordersWidth(), $style->getHorizontalPaddingsWidth())); |
||
264 | } |
||
265 | return $this; |
||
266 | } |
||
267 | $this->parentWidth = $this->getParent()->getDimensions()->getWidth(); |
||
268 | $width = '0'; |
||
269 | if ($this->isForMeasurement()) { |
||
270 | foreach ($this->getChildren() as $child) { |
||
271 | $child->measureWidth($afterPageDividing); |
||
272 | $width = Math::add($width, $child->getDimensions()->getOuterWidth()); |
||
273 | } |
||
274 | } |
||
275 | $width = Math::add($width, $style->getHorizontalBordersWidth(), $style->getHorizontalPaddingsWidth()); |
||
276 | $this->getDimensions()->setWidth($width); |
||
277 | $this->applyStyleWidth(); |
||
278 | return $this; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Measure height. |
||
283 | * |
||
284 | * @param bool $afterPageDividing |
||
285 | * |
||
286 | * @return $this |
||
287 | */ |
||
288 | public function measureHeight(bool $afterPageDividing = false) |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Position. |
||
300 | * |
||
301 | * @param bool $afterPageDividing |
||
302 | * |
||
303 | * @return $this |
||
304 | */ |
||
305 | public function measureOffset(bool $afterPageDividing = false) |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * Position. |
||
337 | * |
||
338 | * @param bool $afterPageDividing |
||
339 | * |
||
340 | * @return $this |
||
341 | */ |
||
342 | public function measurePosition(bool $afterPageDividing = false) |
||
352 | } |
||
353 | |||
354 | public function __clone() |
||
355 | { |
||
356 | $this->element = clone $this->element; |
||
357 | $this->element->setBox($this); |
||
358 | $this->style = clone $this->style; |
||
359 | $this->style->setBox($this); |
||
360 | $this->offset = clone $this->offset; |
||
361 | $this->offset->setBox($this); |
||
362 | $this->dimensions = clone $this->dimensions; |
||
363 | $this->dimensions->setBox($this); |
||
364 | $this->coordinates = clone $this->coordinates; |
||
365 | $this->coordinates->setBox($this); |
||
366 | $this->children = []; |
||
367 | } |
||
368 | |||
369 | public function addBackgroundColorInstructions(array $element, $pdfX, $pdfY, $width, $height) |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * Get element PDF instructions to use in content stream. |
||
394 | * |
||
395 | * @return string |
||
396 | */ |
||
397 | public function getInstructions(): string |
||
411 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.