NumericValue   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 34
c 1
b 0
f 0
dl 0
loc 206
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getIsFont() 0 3 1
A getConverted() 0 3 1
A setIsFont() 0 5 1
A setUnit() 0 5 1
A convert() 0 4 1
A setOriginal() 0 5 1
A getUnit() 0 3 1
A getValue() 0 3 1
A init() 0 7 1
A setConverted() 0 5 1
A setValue() 0 5 1
A get() 0 9 3
A __toString() 0 3 1
A getOriginal() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * NumericValue class.
7
 *
8
 * @copyright YetiForce Sp. z o.o.
9
 * @license   MIT
10
 * @author Rafal Pospiech <[email protected]>
11
 */
12
13
namespace YetiForcePDF\Style;
14
15
/**
16
 * Class NumericValue.
17
 */
18
class NumericValue
19
{
20
	/**
21
	 * Original css value 12px for example.
22
	 *
23
	 * @var string
24
	 */
25
	private $original;
26
	/**
27
	 * Numeric string representation of original value (without unit).
28
	 *
29
	 * @var string
30
	 */
31
	private $value;
32
	/**
33
	 * Unit of original value for '12px' it will be 'px'.
34
	 *
35
	 * @var string
36
	 */
37
	private $unit;
38
	/**
39
	 * Converted to pixel value from other unit (from 'em' for example).
40
	 *
41
	 * @var string
42
	 */
43
	private $converted = '0';
44
	/**
45
	 * Is this numeric value for font size?
46
	 *
47
	 * @var bool
48
	 */
49
	private $isFont;
50
51
	/**
52
	 * Initialize numeric value.
53
	 *
54
	 * @param string $value  css value 12px for example
55
	 * @param bool   $isFont
56
	 *
57
	 * @return self
58
	 */
59
	public function init(string $value, bool $isFont = false)
60
	{
61
		$this->original = $value;
62
		$this->value = \YetiForcePDF\Style\Normalizer\Normalizer::getNumericValue($value);
0 ignored issues
show
Documentation Bug introduced by
It seems like YetiForcePDF\Style\Norma...getNumericValue($value) can also be of type boolean. However, the property $value is declared as type string. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
63
		$this->unit = \YetiForcePDF\Style\Normalizer\Normalizer::getNumericUnit($value, 'px');
64
		$this->isFont = $isFont;
65
		return $this;
66
	}
67
68
	/**
69
	 * Get numeric value out of string value or return original if it's not numeric.
70
	 *
71
	 * @param any|string $value
0 ignored issues
show
Bug introduced by
The type YetiForcePDF\Style\any was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
72
	 * @param Style      $style
73
	 * @param bool       $isFont
74
	 *
75
	 * @return any|NumericValue
76
	 */
77
	public static function get($value, bool $isFont = false)
78
	{
79
		if (!\is_string($value)) {
80
			return $value;
81
		}
82
		if (false !== \YetiForcePDF\Style\Normalizer\Normalizer::getNumericValue($value)) {
83
			return (new self())->init($value, $isFont);
84
		}
85
		return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value returns the type string which is incompatible with the documented return type YetiForcePDF\Style\Numer...|YetiForcePDF\Style\any.
Loading history...
86
	}
87
88
	/**
89
	 * Get the value of original.
90
	 *
91
	 * @return string
92
	 */
93
	public function getOriginal(): string
94
	{
95
		return $this->original;
96
	}
97
98
	/**
99
	 * Set the value of original.
100
	 *
101
	 * @param string $original
102
	 *
103
	 * @return self
104
	 */
105
	public function setOriginal(string $original)
106
	{
107
		$this->original = $original;
108
109
		return $this;
110
	}
111
112
	/**
113
	 * Get the value of value.
114
	 *
115
	 * @return string
116
	 */
117
	public function getValue(): string
118
	{
119
		return $this->value;
120
	}
121
122
	/**
123
	 * Set the value of value.
124
	 *
125
	 * @param string $value
126
	 *
127
	 * @return self
128
	 */
129
	public function setValue(string $value)
130
	{
131
		$this->value = $value;
132
133
		return $this;
134
	}
135
136
	/**
137
	 * Get the value of unit.
138
	 *
139
	 * @return string
140
	 */
141
	public function getUnit(): string
142
	{
143
		return $this->unit;
144
	}
145
146
	/**
147
	 * Set the value of unit.
148
	 *
149
	 * @param string $unit
150
	 *
151
	 * @return self
152
	 */
153
	public function setUnit(string $unit)
154
	{
155
		$this->unit = $unit;
156
157
		return $this;
158
	}
159
160
	/**
161
	 * Get the value of converted.
162
	 *
163
	 * @return string
164
	 */
165
	public function getConverted(): string
166
	{
167
		return $this->converted;
168
	}
169
170
	/**
171
	 * Set the value of converted.
172
	 *
173
	 * @param string $converted
174
	 *
175
	 * @return self
176
	 */
177
	public function setConverted(string $converted)
178
	{
179
		$this->converted = $converted;
180
181
		return $this;
182
	}
183
184
	/**
185
	 * Convert unit.
186
	 *
187
	 * @param Style $style
188
	 *
189
	 * @return self
190
	 */
191
	public function convert(Style $style)
192
	{
193
		$this->converted = $style->convertUnits($this->unit, $this->value, $this->isFont);
194
		return $this;
195
	}
196
197
	/**
198
	 * Get the value of isFont.
199
	 *
200
	 * @return bool
201
	 */
202
	public function getIsFont(): bool
203
	{
204
		return $this->isFont;
205
	}
206
207
	/**
208
	 * Set the value of isFont.
209
	 *
210
	 * @param bool $isFont
211
	 *
212
	 * @return self
213
	 */
214
	public function setIsFont(bool $isFont)
215
	{
216
		$this->isFont = $isFont;
217
218
		return $this;
219
	}
220
221
	public function __toString()
222
	{
223
		return $this->converted;
224
	}
225
}
226