Skip to content

Commit

Permalink
Do not encode floats
Browse files Browse the repository at this point in the history
  • Loading branch information
arokettu committed Jul 14, 2023
1 parent 4cb8c27 commit 7ecca82
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 0 additions & 2 deletions src/Engine/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions tests/EncodeScalarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 7ecca82

Please sign in to comment.