Skip to content
This repository has been archived by the owner on Apr 28, 2020. It is now read-only.

Disable bigint for 64 bit machines #40

Merged
merged 3 commits into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ All notable changes to this project will be documented in this file, in reverse

### Changed

- Nothing.
Disables use of BigInteger for XMLRPC i8 type if host machine is 64-bit.

### Deprecated

Expand Down
4 changes: 2 additions & 2 deletions docs/book/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ XML-RPC Type | `Zend\XmlRpc\AbstractValue` Constant | `Zend\Xm
---------------- | -------------------------------------------------- | --------------------------
int | `Zend\XmlRpc\AbstractValue::XMLRPC_TYPE_INTEGER` | `Zend\XmlRpc\Value\Integer`
i4 | `Zend\XmlRpc\AbstractValue::XMLRPC_TYPE_I4` | `Zend\XmlRpc\Value\Integer`
i8 | `Zend\XmlRpc\AbstractValue::XMLRPC_TYPE_I8` | `Zend\XmlRpc\Value\BigInteger`
ex:i8 | `Zend\XmlRpc\AbstractValue::XMLRPC_TYPE_APACHEI8` | `Zend\XmlRpc\Value\BigInteger`
i8 | `Zend\XmlRpc\AbstractValue::XMLRPC_TYPE_I8` | `Zend\XmlRpc\Value\BigInteger` or `Zend\XmlRpc\Value\Integer` if machine is 64-bit
ex:i8 | `Zend\XmlRpc\AbstractValue::XMLRPC_TYPE_APACHEI8` | `Zend\XmlRpc\Value\BigInteger` or `Zend\XmlRpc\Value\Integer` if machine is 64-bit
double | `Zend\XmlRpc\AbstractValue::XMLRPC_TYPE_DOUBLE` | `Zend\XmlRpc\ValueDouble`
boolean | `Zend\XmlRpc\AbstractValue::XMLRPC_TYPE_BOOLEAN` | `Zend\XmlRpc\Value\Boolean`
string | `Zend\XmlRpc\AbstractValue::XMLRPC_TYPE_STRING` | `Zend\XmlRpc\Value\Text`
Expand Down
12 changes: 10 additions & 2 deletions src/AbstractValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ abstract class AbstractValue
*/
protected $xml;

/**
* True if BigInteger should be used for XMLRPC i8 types
*
* @internal
* @var bool
*/
public static $USE_BIGINT_FOR_I8 = PHP_INT_SIZE < 8;

/**
* @var \Zend\XmlRpc\Generator\GeneratorInterface
*/
Expand Down Expand Up @@ -193,7 +201,7 @@ public static function getXmlRpcValue($value, $type = self::AUTO_DETECT_TYPE)
case self::XMLRPC_TYPE_I8:
// fall through to the next case
case self::XMLRPC_TYPE_APACHEI8:
return new Value\BigInteger($value);
return self::$USE_BIGINT_FOR_I8 ? new Value\BigInteger($value) : new Value\Integer($value);

case self::XMLRPC_TYPE_DOUBLE:
return new Value\Double($value);
Expand Down Expand Up @@ -337,7 +345,7 @@ protected static function xmlStringToNativeXmlRpc($xml)
case self::XMLRPC_TYPE_APACHEI8:
// Fall through to the next case
case self::XMLRPC_TYPE_I8:
$xmlrpcValue = new Value\BigInteger($value);
$xmlrpcValue = self::$USE_BIGINT_FOR_I8 ? new Value\BigInteger($value) : new Value\Integer($value);
break;
case self::XMLRPC_TYPE_DOUBLE:
$xmlrpcValue = new Value\Double($value);
Expand Down
31 changes: 31 additions & 0 deletions test/BigIntegerValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@
use PHPUnit\Framework\TestCase;
use Zend\XmlRpc\AbstractValue;
use Zend\XmlRpc\Value\BigInteger;
use Zend\XmlRpc\Value\Integer;
use Zend\XmlRpc\Generator\GeneratorInterface as Generator;

/**
* @group Zend_XmlRpc
*/
class BigIntegerValueTest extends TestCase
{
/** @var null|bool */
protected $useBigIntForI8Flag;

public function setUp()
{
$this->useBigIntForI8Flag = AbstractValue::$USE_BIGINT_FOR_I8;
AbstractValue::$USE_BIGINT_FOR_I8 = true;

if (extension_loaded('gmp')) {
$this->markTestSkipped('gmp causes test failure');
}
Expand All @@ -31,6 +38,12 @@ public function setUp()
}
}

public function tearDown()
{
AbstractValue::$USE_BIGINT_FOR_I8 = $this->useBigIntForI8Flag;
$this->useBigIntForI8Flag = null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious what the significance of setting useBigIntForI8Flag to null is.

}

// BigInteger

/**
Expand Down Expand Up @@ -136,4 +149,22 @@ public function wrapXml($xml)
{
return $xml . "\n";
}

public function testMarshalsIntegerForI8ValueByDefaultIfSystemIs64Bit()
{
if ($this->useBigIntForI8Flag) {
$this->markTestSkipped('Test only valid for 64bit systems');
}

AbstractValue::$USE_BIGINT_FOR_I8 = $this->useBigIntForI8Flag;
$integerValue = PHP_INT_MAX;

$value = AbstractValue::getXmlRpcValue(
$integerValue,
AbstractValue::XMLRPC_TYPE_I8
);

$this->assertEquals(AbstractValue::XMLRPC_TYPE_INTEGER, $value->getType());
$this->assertSame($integerValue, $value->getValue());
}
}