Skip to content

Commit 7ea88fd

Browse files
committed
make bitshift count a static property
1 parent 02bcdd2 commit 7ea88fd

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/Unit.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Unit
1111
{
1212
protected $value;
1313

14+
static protected $bitShift = 6;
1415
static protected $typeMap;
1516
static protected $factorMap;
1617
static protected $symbolMap;
@@ -162,17 +163,17 @@ public static function from($value)
162163

163164
if ($classType === 'integer' || $classType === 'double') {
164165
$intValue = (int)$value;
165-
$type = $intValue & 63;
166-
$value = ($intValue >> 6) + ($value - $intValue);
167-
166+
$type = $intValue & ((1 << self::$bitShift) - 1);
167+
$value = ($intValue >> self::$bitShift) + ($value - $intValue);
168+
168169
$typeMap = static::buildTypeMap();
169170

170171
if (isset($typeMap[$type])) {
171172
$baseClass = $typeMap[$type]::BASE_UNIT;
172173

173174
return new $baseClass($value);
174175
}
175-
176+
176177
throw new InvalidArgumentException();
177178
} elseif ($classType === 'string' && !class_exists($classType)) {
178179
// make use of php's type juggling to find symbol
@@ -484,7 +485,7 @@ private static function buildLabelMap($rebuild = false)
484485
public function __invoke()
485486
{
486487
$intBase = (int)$this->toBaseValue();
487-
return ($intBase << 6) + static::TYPE + ($this->toBaseValue() - $intBase);
488+
return ($intBase << self::$bitShift) + static::TYPE + ($this->toBaseValue() - $intBase);
488489
}
489490

490491
public function __toString()

0 commit comments

Comments
 (0)