Skip to content

Commit 8a54aa2

Browse files
committed
Allow using the int Code Calculator
1 parent 49f0289 commit 8a54aa2

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/CodeCalculator/AbstractCodeCalculator.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@ abstract public function decode(string $strippedCode): CodeArea;
6161

6262
/**
6363
* Returns a static instance of the appropriate code calculator depending on whether the current PHP is 32-bit/64-bit.
64-
*
6564
* 32-bit PHP will get a calculator that uses floats, while 64-bit PHP will get a calculator that uses "long" ints.
6665
*
67-
* This is required to allow 32-bit PHP environments to correctly calculate Open Location Codes.
68-
*
69-
* @return CodeCalculatorFloat The appropriate code calculator.
66+
* This is required to allow 32-bit PHP environments to correctly calculate Open Location Codes, while preserving
67+
* the performance advantage of 64-bit PHP by using integer operations.
68+
* @return CodeCalculatorFloat|CodeCalculatorInt The appropriate code calculator.
7069
*/
71-
public static function getDefaultCalculator(): CodeCalculatorFloat
70+
public static function getDefaultCalculator(): CodeCalculatorFloat|CodeCalculatorInt
7271
{
73-
// always give float calculator for now
74-
// todo check platform status to give int calculator
75-
static $defaultCalculator = new CodeCalculatorFloat();
72+
// check PHP environment to give int calculator where possible
73+
// 32-bit PHP has 32-bit int, which uses 4 bytes i.e. PHP_INT_SIZE = 4
74+
// initializing static variables with constructors is only allowed for PHP 8.3+
75+
static $defaultCalculator = PHP_INT_SIZE > 4 ? new CodeCalculatorInt() : new CodeCalculatorFloat();
7676
return $defaultCalculator;
7777
}
7878
}

0 commit comments

Comments
 (0)