Skip to content

Commit

Permalink
Added a whole bunch of missing @throws
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps committed Dec 4, 2021
1 parent 67a82c0 commit db49000
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,17 @@ public static function writeByte(int $c) : string{

/**
* Reads a 16-bit unsigned big-endian number
*
* @throws BinaryDataException
*/
public static function readShort(string $str) : int{
return self::safeUnpack("n", $str, self::SIZEOF_SHORT)[1];
}

/**
* Reads a 16-bit signed big-endian number
*
* @throws BinaryDataException
*/
public static function readSignedShort(string $str) : int{
return self::signShort(self::safeUnpack("n", $str, self::SIZEOF_SHORT)[1]);
Expand All @@ -169,13 +173,17 @@ public static function writeShort(int $value) : string{

/**
* Reads a 16-bit unsigned little-endian number
*
* @throws BinaryDataException
*/
public static function readLShort(string $str) : int{
return self::safeUnpack("v", $str, self::SIZEOF_SHORT)[1];
}

/**
* Reads a 16-bit signed little-endian number
*
* @throws BinaryDataException
*/
public static function readSignedLShort(string $str) : int{
return self::signShort(self::safeUnpack("v", $str, self::SIZEOF_SHORT)[1]);
Expand All @@ -190,6 +198,8 @@ public static function writeLShort(int $value) : string{

/**
* Reads a 3-byte big-endian number
*
* @throws BinaryDataException
*/
public static function readTriad(string $str) : int{
return self::safeUnpack("N", "\x00" . $str, self::SIZEOF_INT)[1];
Expand All @@ -204,6 +214,8 @@ public static function writeTriad(int $value) : string{

/**
* Reads a 3-byte little-endian number
*
* @throws BinaryDataException
*/
public static function readLTriad(string $str) : int{
return self::safeUnpack("V", $str . "\x00", self::SIZEOF_INT)[1];
Expand All @@ -218,6 +230,8 @@ public static function writeLTriad(int $value) : string{

/**
* Reads a 4-byte signed integer
*
* @throws BinaryDataException
*/
public static function readInt(string $str) : int{
return self::signInt(self::safeUnpack("N", $str, self::SIZEOF_INT)[1]);
Expand All @@ -232,6 +246,8 @@ public static function writeInt(int $value) : string{

/**
* Reads a 4-byte signed little-endian integer
*
* @throws BinaryDataException
*/
public static function readLInt(string $str) : int{
return self::signInt(self::safeUnpack("V", $str, self::SIZEOF_INT)[1]);
Expand All @@ -246,13 +262,17 @@ public static function writeLInt(int $value) : string{

/**
* Reads a 4-byte floating-point number
*
* @throws BinaryDataException
*/
public static function readFloat(string $str) : float{
return self::safeUnpack("G", $str, self::SIZEOF_FLOAT)[1];
}

/**
* Reads a 4-byte floating-point number, rounded to the specified number of decimal places.
*
* @throws BinaryDataException
*/
public static function readRoundedFloat(string $str, int $accuracy) : float{
return round(self::readFloat($str), $accuracy);
Expand All @@ -267,13 +287,17 @@ public static function writeFloat(float $value) : string{

/**
* Reads a 4-byte little-endian floating-point number.
*
* @throws BinaryDataException
*/
public static function readLFloat(string $str) : float{
return self::safeUnpack("g", $str, self::SIZEOF_FLOAT)[1];
}

/**
* Reads a 4-byte little-endian floating-point number rounded to the specified number of decimal places.
*
* @throws BinaryDataException
*/
public static function readRoundedLFloat(string $str, int $accuracy) : float{
return round(self::readLFloat($str), $accuracy);
Expand All @@ -295,6 +319,8 @@ public static function printFloat(float $value) : string{

/**
* Reads an 8-byte floating-point number.
*
* @throws BinaryDataException
*/
public static function readDouble(string $str) : float{
return self::safeUnpack("E", $str, self::SIZEOF_DOUBLE)[1];
Expand All @@ -309,6 +335,8 @@ public static function writeDouble(float $value) : string{

/**
* Reads an 8-byte little-endian floating-point number.
*
* @throws BinaryDataException
*/
public static function readLDouble(string $str) : float{
return self::safeUnpack("e", $str, self::SIZEOF_DOUBLE)[1];
Expand All @@ -323,6 +351,8 @@ public static function writeLDouble(float $value) : string{

/**
* Reads an 8-byte integer.
*
* @throws BinaryDataException
*/
public static function readLong(string $str) : int{
return self::safeUnpack("J", $str, self::SIZEOF_LONG)[1];
Expand All @@ -337,6 +367,8 @@ public static function writeLong(int $value) : string{

/**
* Reads an 8-byte little-endian integer.
*
* @throws BinaryDataException
*/
public static function readLLong(string $str) : int{
return self::safeUnpack("P", $str, self::SIZEOF_LONG)[1];
Expand All @@ -353,6 +385,8 @@ public static function writeLLong(int $value) : string{
* Reads a 32-bit zigzag-encoded variable-length integer.
*
* @param int $offset reference parameter
*
* @throws BinaryDataException
*/
public static function readVarInt(string $buffer, int &$offset) : int{
$raw = self::readUnsignedVarInt($buffer, $offset);
Expand Down Expand Up @@ -418,6 +452,8 @@ public static function writeUnsignedVarInt(int $value) : string{
* Reads a 64-bit zigzag-encoded variable-length integer.
*
* @param int $offset reference parameter
*
* @throws BinaryDataException
*/
public static function readVarLong(string $buffer, int &$offset) : int{
$raw = self::readUnsignedVarLong($buffer, $offset);
Expand Down
22 changes: 22 additions & 0 deletions src/BinaryStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function put(string $str) : void{

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getBool() : bool{
return $this->get(1) !== "\x00";
Expand All @@ -113,6 +114,7 @@ public function putBool(bool $v) : void{

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getByte() : int{
return ord($this->get(1));
Expand All @@ -124,13 +126,15 @@ public function putByte(int $v) : void{

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getShort() : int{
return Binary::readShort($this->get(2));
}

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getSignedShort() : int{
return Binary::readSignedShort($this->get(2));
Expand All @@ -142,13 +146,15 @@ public function putShort(int $v) : void{

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getLShort() : int{
return Binary::readLShort($this->get(2));
}

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getSignedLShort() : int{
return Binary::readSignedLShort($this->get(2));
Expand All @@ -160,6 +166,7 @@ public function putLShort(int $v) : void{

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getTriad() : int{
return Binary::readTriad($this->get(3));
Expand All @@ -171,6 +178,7 @@ public function putTriad(int $v) : void{

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getLTriad() : int{
return Binary::readLTriad($this->get(3));
Expand All @@ -182,6 +190,7 @@ public function putLTriad(int $v) : void{

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getInt() : int{
return Binary::readInt($this->get(4));
Expand All @@ -193,6 +202,7 @@ public function putInt(int $v) : void{

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getLInt() : int{
return Binary::readLInt($this->get(4));
Expand All @@ -204,13 +214,15 @@ public function putLInt(int $v) : void{

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getFloat() : float{
return Binary::readFloat($this->get(4));
}

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getRoundedFloat(int $accuracy) : float{
return Binary::readRoundedFloat($this->get(4), $accuracy);
Expand All @@ -222,13 +234,15 @@ public function putFloat(float $v) : void{

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getLFloat() : float{
return Binary::readLFloat($this->get(4));
}

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getRoundedLFloat(int $accuracy) : float{
return Binary::readRoundedLFloat($this->get(4), $accuracy);
Expand All @@ -240,6 +254,7 @@ public function putLFloat(float $v) : void{

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getDouble() : float{
return Binary::readDouble($this->get(8));
Expand All @@ -251,6 +266,7 @@ public function putDouble(float $v) : void{

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getLDouble() : float{
return Binary::readLDouble($this->get(8));
Expand All @@ -262,6 +278,7 @@ public function putLDouble(float $v) : void{

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getLong() : int{
return Binary::readLong($this->get(8));
Expand All @@ -273,6 +290,7 @@ public function putLong(int $v) : void{

/**
* @phpstan-impure
* @throws BinaryDataException
*/
public function getLLong() : int{
return Binary::readLLong($this->get(8));
Expand All @@ -286,6 +304,7 @@ public function putLLong(int $v) : void{
* Reads a 32-bit variable-length unsigned integer from the buffer and returns it.
*
* @phpstan-impure
* @throws BinaryDataException
*/
public function getUnsignedVarInt() : int{
return Binary::readUnsignedVarInt($this->buffer, $this->offset);
Expand All @@ -302,6 +321,7 @@ public function putUnsignedVarInt(int $v) : void{
* Reads a 32-bit zigzag-encoded variable-length integer from the buffer and returns it.
*
* @phpstan-impure
* @throws BinaryDataException
*/
public function getVarInt() : int{
return Binary::readVarInt($this->buffer, $this->offset);
Expand All @@ -318,6 +338,7 @@ public function putVarInt(int $v) : void{
* Reads a 64-bit variable-length integer from the buffer and returns it.
*
* @phpstan-impure
* @throws BinaryDataException
*/
public function getUnsignedVarLong() : int{
return Binary::readUnsignedVarLong($this->buffer, $this->offset);
Expand All @@ -334,6 +355,7 @@ public function putUnsignedVarLong(int $v) : void{
* Reads a 64-bit zigzag-encoded variable-length integer from the buffer and returns it.
*
* @phpstan-impure
* @throws BinaryDataException
*/
public function getVarLong() : int{
return Binary::readVarLong($this->buffer, $this->offset);
Expand Down

0 comments on commit db49000

Please sign in to comment.