ImageBox::measureHeight()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 11
rs 9.9666
c 2
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * ImageBox class.
6
 *
7
 * @package   YetiForcePDF\Layout
8
 *
9
 * @copyright YetiForce Sp. z o.o
10
 * @license   MIT
11
 * @author    Rafal Pospiech <[email protected]>
12
 */
13
14
namespace YetiForcePDF\Layout;
15
16
use YetiForcePDF\Math;
17
18
/**
19
 * Class ImageBox.
20
 */
21
class ImageBox extends InlineBlockBox
22
{
23
	/**
24
	 * Measure width.
25
	 *
26
	 * @param bool $afterPageDividing
27
	 *
28
	 * @return $this
29
	 */
30
	public function measureWidth(bool $afterPageDividing = false)
31
	{
32
		$style = $this->getStyle();
33
		if ('auto' === $style->getRules('width')) {
34
			$img = $style->getBackgroundImageStream();
35
			$width = $img->getWidth();
36
			if (Math::comp($width, $this->getParent()->getDimensions()->computeAvailableSpace()) > 0) {
37
				$width = $this->getParent()->getDimensions()->computeAvailableSpace();
38
			}
39
			if ('auto' !== $style->getRules('height')) {
40
				Math::setAccurate(true);
41
				$ratio = $img->getRatio();
42
				$width = Math::mul($ratio, $this->getDimensions()->getStyleHeight());
0 ignored issues
show
Bug introduced by
It seems like $this->getDimensions()->getStyleHeight() can also be of type array and null; however, parameter $numbers of YetiForcePDF\Math::mul() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
				$width = Math::mul($ratio, /** @scrutinizer ignore-type */ $this->getDimensions()->getStyleHeight());
Loading history...
43
				Math::setAccurate(false);
44
			}
45
			$width = Math::add($width, $style->getHorizontalBordersWidth(), $style->getHorizontalPaddingsWidth());
46
			$this->getDimensions()->setWidth($width);
47
		} else {
48
			$this->applyStyleWidth();
49
		}
50
		return $this;
51
	}
52
53
	/**
54
	 * Measure height.
55
	 *
56
	 * @param bool $afterPageDividing
57
	 *
58
	 * @return $this
59
	 */
60
	public function measureHeight(bool $afterPageDividing = false)
61
	{
62
		$style = $this->getStyle();
63
		$img = $style->getBackgroundImageStream();
64
		Math::setAccurate(true);
65
		$ratio = $img->getRatio();
66
		$height = Math::div($this->getDimensions()->getInnerWidth(), $ratio);
67
		Math::setAccurate(false);
68
		$height = Math::add($height, $style->getVerticalBordersWidth(), $style->getVerticalPaddingsWidth());
69
		$this->getDimensions()->setHeight($height);
70
		return $this;
71
	}
72
}
73