From 7ecca82e0a8f1e5da010773b5025ec04631cfdfd Mon Sep 17 00:00:00 2001 From: Anton Smirnov Date: Fri, 14 Jul 2023 19:58:17 +0300 Subject: [PATCH] Do not encode floats --- src/Engine/Encoder.php | 2 -- tests/EncodeScalarTest.php | 10 ++++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Engine/Encoder.php b/src/Engine/Encoder.php index 40c89ea..1b99cda 100644 --- a/src/Engine/Encoder.php +++ b/src/Engine/Encoder.php @@ -58,9 +58,7 @@ private function encodeValue(mixed $value): void $value instanceof \Math_BigInteger, => $this->encodeInteger($value), // process strings - // floats become strings \is_string($value) => $this->encodeString($value), - \is_float($value) => $this->encodeString(\strval($value)), // process arrays \is_array($value) => $this->encodeArray($value), // process objects diff --git a/tests/EncodeScalarTest.php b/tests/EncodeScalarTest.php index 31f8703..7476057 100644 --- a/tests/EncodeScalarTest.php +++ b/tests/EncodeScalarTest.php @@ -5,6 +5,7 @@ namespace Arokettu\Bencode\Tests; use Arokettu\Bencode\Bencode; +use Arokettu\Bencode\Exceptions\InvalidArgumentException; use PHPUnit\Framework\TestCase; class EncodeScalarTest extends TestCase @@ -37,8 +38,13 @@ public function testString(): void // unicode. prefix number reflects the number if bytes self::assertEquals('9:日本語', Bencode::encode('日本語')); + } - // scalars converted to string - self::assertEquals('6:3.1416', Bencode::encode(3.1416)); + public function testFloat(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Bencode doesn't know how to serialize an instance of float"); + // float cannot be reliably encoded + Bencode::encode(3.1416); } }