diff --git a/src/APILengthCoDec.php b/src/APILengthCoDec.php index fcbc93e..2f275bd 100644 --- a/src/APILengthCoDec.php +++ b/src/APILengthCoDec.php @@ -15,7 +15,13 @@ use RouterOS\Helpers\BinaryStringHelper; */ class APILengthCoDec { - public static function encodeLength(int $length): string + /** + * Encode string to length of string + * + * @param int|float $length + * @return string + */ + public static function encodeLength($length): string { // Encode the length : // - if length <= 0x7F (binary : 01111111 => 7 bits set to 1) @@ -161,8 +167,9 @@ class APILengthCoDec if (PHP_INT_SIZE < 8) { // Cannot be done on 32 bits systems // PHP5 windows versions of php, even on 64 bits systems was impacted - // see : https://stackoverflow.com/questions/27865340/php-int-size-returns-4-but-my-operating-system-is-64-bit + // see : https://stackoverflow.com/questions/27865340/php-int-size-returns-4-but-my-operating-system-is-64-bit // How can we test it ? + // @codeCoverageIgnoreStart throw new \OverflowException("Your system is using 32 bits integers, cannot decode this value ($firstByte) on this system"); // @codeCoverageIgnoreEnd diff --git a/src/Helpers/BinaryStringHelper.php b/src/Helpers/BinaryStringHelper.php index a5e7e70..9bb2de1 100644 --- a/src/Helpers/BinaryStringHelper.php +++ b/src/Helpers/BinaryStringHelper.php @@ -23,10 +23,10 @@ class BinaryStringHelper * Compatible with 8, 16, 32, 64 etc.. bits systems * * @see https://en.wikipedia.org/wiki/Endianness - * @param int $value the integer value to be converted + * @param int|float $value the integer value to be converted * @return string the binary string */ - public static function IntegerToNBOBinaryString(int $value): string + public static function IntegerToNBOBinaryString($value): string { // Initialize an empty string $buffer = ''; diff --git a/tests/APILengthCoDecTest.php b/tests/APILengthCoDecTest.php index a1252df..5339591 100644 --- a/tests/APILengthCoDecTest.php +++ b/tests/APILengthCoDecTest.php @@ -11,6 +11,7 @@ use RouterOS\Helpers\BinaryStringHelper; /** * Limit code coverage to the class + * * @coversDefaultClass \RouterOS\APILengthCoDec */ class APILengthCoDecTest extends TestCase @@ -25,11 +26,11 @@ class APILengthCoDecTest extends TestCase APILengthCoDec::encodeLength($length); } - public function encodeLengthNegativeProvider() + public function encodeLengthNegativeProvider(): array { return [ - [-1], - [PHP_INT_MIN], + [-1], + [PHP_INT_MIN], ]; } @@ -39,10 +40,10 @@ class APILengthCoDecTest extends TestCase */ public function test__encodeLength($expected, $length) { - $this->assertEquals(BinaryStringHelper::IntegerToNBOBinaryString($expected), APILengthCoDec::encodeLength($length)); + $this->assertEquals(BinaryStringHelper::IntegerToNBOBinaryString((int) $expected), APILengthCoDec::encodeLength($length)); } - public function encodedLengthProvider() + public function encodedLengthProvider(): array { // [encoded length value, length value] return [ @@ -54,14 +55,14 @@ class APILengthCoDecTest extends TestCase [0x9C42, 0x1C42], // Arbitrary median value for 2 bytes encoded length [0xBFFF, 0x3FFF], // High limit value for 2 bytes encoded length - [0xC04000, 0x4000], // Low limit value for 3 bytesv + [0xC04000, 0x4000], // Low limit value for 3 bytes [0xCAD73B, 0xAD73B], // Arbitrary median value for 3 bytes encoded length [0xDFFFFF, 0x1FFFFF], // High limit value for 3 bytes encoded length [0xE0200000, 0x200000], // Low limit value for 4 bytes encoded length [0xE5AD736B, 0x5AD736B], // Arbitrary median value for 4 bytes encoded length [0xEFFFFFFF, 0xFFFFFFF], // High limit value for 4 bytes encoded length - + [0xF010000000, 0x10000000], // Low limit value for 5 bytes encoded length [0xF10D4EF9C3, 0x10D4EF9C3], // Arbitrary median value for 5 bytes encoded length [0xF7FFFFFFFF, 0x7FFFFFFFF], // High limit value for 5 bytes encoded length @@ -83,19 +84,19 @@ class APILengthCoDecTest extends TestCase /** * @dataProvider decodeLengthControlWordProvider * @covers ::decodeLength - * @expectedException UnexpectedValueException + * @expectedException \UnexpectedValueException */ public function test_decodeLengthControlWord(string $encodedLength) { APILengthCoDec::decodeLength(new StringStream($encodedLength)); } - public function decodeLengthControlWordProvider() - { - // Control bytes : 5 most signficants its sets to 1 + public function decodeLengthControlWordProvider(): array + { + // Control bytes: 5 most significance its sets to 1 return [ [chr(0xF8)], // minimum - [chr(0xFC)], // arbitraty value + [chr(0xFC)], // arbitrary value [chr(0xFF)], // maximum ]; } diff --git a/tests/Helpers/BinaryStringHelperTest.php b/tests/Helpers/BinaryStringHelperTest.php index 2b30b83..b45b921 100644 --- a/tests/Helpers/BinaryStringHelperTest.php +++ b/tests/Helpers/BinaryStringHelperTest.php @@ -1,4 +1,5 @@ assertEquals($expected, BinaryStringHelper::IntegerToNBOBinaryString($value)); } - public function IntegerToNBOBinaryStringProvider() + public function IntegerToNBOBinaryStringProvider(): array { return [ - [0, chr(0)], // lower boundary value - [0xFFFFFFFF, chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF)], // 32 bits maximal value + [0, chr(0)], // lower boundary value + [0xFFFFFFFF, chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF)], // 32 bits maximal value // strange behaviour : // TypeError: Argument 1 passed to RouterOS\Tests\Helpers\BinaryStringHelperTest::test__IntegerToNBOBinaryString() must be of the type integer, float given @@ -33,11 +35,11 @@ class BinaryStringHelperTest extends TestCase // [0xFFFFFFFFFFFFFFFF, chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF)], // -1 is encoded with 0xFFFFFFF..... - // 64 bits maximal value (on a 64 bits system) - [-1, chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF)], // 64 bits upper boundary value - // Let's try random value - [0x390DDD99, chr(0x39).chr(0x0D).chr(0xDD).chr(0x99)], + // 64 bits maximal value (on a 64 bits system only) + [-1, chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF)], // 64 bits upper boundary value + // Let's try random value + [0x390DDD99, chr(0x39) . chr(0x0D) . chr(0xDD) . chr(0x99)], ]; } -} \ No newline at end of file +}