Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 21bfd0e

Browse files
committed
Decreased Decimal::fromFloat complexity
1 parent 37b1c6c commit 21bfd0e

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/Decimal.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,12 @@ public static function fromFloat($fltValue, $scale = null)
141141
if (!is_float($fltValue)) {
142142
throw new InvalidArgumentTypeException(
143143
array('float'),
144-
is_object($fltValue) ? get_class($fltValue) : gettype($fltValue),
144+
is_object($fltValue) ?
145+
get_class($fltValue) :
146+
gettype($fltValue),
145147
'$fltValue must be of type float'
146148
);
147-
}
148-
149-
if ($fltValue === INF) {
149+
} elseif ($fltValue === INF) {
150150
return Decimal::getPositiveInfinite();
151151
} elseif ($fltValue === -INF) {
152152
return Decimal::getNegativeInfinite();
@@ -156,14 +156,18 @@ public static function fromFloat($fltValue, $scale = null)
156156
);
157157
}
158158

159+
$dec_scale = $scale === null ?
160+
8 :
161+
$scale;
162+
159163
return new Decimal(
160164
number_format(
161165
$fltValue,
162-
$scale === null ? 8 : $scale,
166+
$dec_scale,
163167
'.',
164168
''
165169
),
166-
$scale === null ? 8 : $scale
170+
$dec_scale
167171
);
168172
}
169173

@@ -182,9 +186,7 @@ public static function fromString($strValue, $scale = null)
182186
is_object($strValue) ? get_class($strValue) : gettype($strValue),
183187
'$strVlue must be of type string.'
184188
);
185-
}
186-
187-
if (preg_match('/^([+\-]?)0*(([1-9][0-9]*|[0-9])(\.[0-9]+)?)$/', $strValue, $captures) === 1) {
189+
} elseif (preg_match('/^([+\-]?)0*(([1-9][0-9]*|[0-9])(\.[0-9]+)?)$/', $strValue, $captures) === 1) {
188190

189191
// Now it's time to strip leading zeros in order to normalize inner values
190192
$value = self::normalizeSign($captures[1]) . $captures[2];

0 commit comments

Comments
 (0)