NBT: Add typehints to PHP 7.2 standards

This commit is contained in:
Dylan K. Taylor 2017-12-30 15:07:50 +00:00
parent a67f7e3930
commit be02fbb352
5 changed files with 35 additions and 35 deletions

View File

@ -39,7 +39,7 @@ class BigEndianNBTStream extends NBTStream{
return Binary::readSignedShort($this->get(2)); return Binary::readSignedShort($this->get(2));
} }
public function putShort($v){ public function putShort(int $v) : void{
$this->buffer .= Binary::writeShort($v); $this->buffer .= Binary::writeShort($v);
} }
@ -47,7 +47,7 @@ class BigEndianNBTStream extends NBTStream{
return Binary::readInt($this->get(4)); return Binary::readInt($this->get(4));
} }
public function putInt($v){ public function putInt(int $v) : void{
$this->buffer .= Binary::writeInt($v); $this->buffer .= Binary::writeInt($v);
} }
@ -55,7 +55,7 @@ class BigEndianNBTStream extends NBTStream{
return Binary::readLong($this->get(8)); return Binary::readLong($this->get(8));
} }
public function putLong($v){ public function putLong(int $v) : void{
$this->buffer .= Binary::writeLong($v); $this->buffer .= Binary::writeLong($v);
} }
@ -63,7 +63,7 @@ class BigEndianNBTStream extends NBTStream{
return Binary::readFloat($this->get(4)); return Binary::readFloat($this->get(4));
} }
public function putFloat($v){ public function putFloat(float $v) : void{
$this->buffer .= Binary::writeFloat($v); $this->buffer .= Binary::writeFloat($v);
} }
@ -71,7 +71,7 @@ class BigEndianNBTStream extends NBTStream{
return Binary::readDouble($this->get(8)); return Binary::readDouble($this->get(8));
} }
public function putDouble($v){ public function putDouble(float $v) : void{
$this->buffer .= Binary::writeDouble($v); $this->buffer .= Binary::writeDouble($v);
} }

View File

@ -39,7 +39,7 @@ class LittleEndianNBTStream extends NBTStream{
return Binary::readSignedLShort($this->get(2)); return Binary::readSignedLShort($this->get(2));
} }
public function putShort($v){ public function putShort(int $v) : void{
$this->put(Binary::writeLShort($v)); $this->put(Binary::writeLShort($v));
} }
@ -47,7 +47,7 @@ class LittleEndianNBTStream extends NBTStream{
return Binary::readLInt($this->get(4)); return Binary::readLInt($this->get(4));
} }
public function putInt($v){ public function putInt(int $v) : void{
$this->put(Binary::writeLInt($v)); $this->put(Binary::writeLInt($v));
} }
@ -55,7 +55,7 @@ class LittleEndianNBTStream extends NBTStream{
return Binary::readLLong($this->get(8)); return Binary::readLLong($this->get(8));
} }
public function putLong($v){ public function putLong(int $v) : void{
$this->put(Binary::writeLLong($v)); $this->put(Binary::writeLLong($v));
} }
@ -63,7 +63,7 @@ class LittleEndianNBTStream extends NBTStream{
return Binary::readLFloat($this->get(4)); return Binary::readLFloat($this->get(4));
} }
public function putFloat($v){ public function putFloat(float $v) : void{
$this->put(Binary::writeLFloat($v)); $this->put(Binary::writeLFloat($v));
} }
@ -71,7 +71,7 @@ class LittleEndianNBTStream extends NBTStream{
return Binary::readLDouble($this->get(8)); return Binary::readLDouble($this->get(8));
} }
public function putDouble($v){ public function putDouble(float $v) : void{
$this->put(Binary::writeLDouble($v)); $this->put(Binary::writeLDouble($v));
} }

View File

@ -62,7 +62,7 @@ abstract class NBT{
* *
* @return Tag * @return Tag
*/ */
public static function createTag(int $type){ public static function createTag(int $type) : Tag{
switch($type){ switch($type){
case self::TAG_End: case self::TAG_End:
return new EndTag(); return new EndTag();

View File

@ -48,7 +48,7 @@ abstract class NBTStream{
public $offset; public $offset;
private $data; private $data;
public function get($len){ public function get($len) : string{
if($len < 0){ if($len < 0){
$this->offset = strlen($this->buffer) - 1; $this->offset = strlen($this->buffer) - 1;
return ""; return "";
@ -59,7 +59,7 @@ abstract class NBTStream{
return $len === 1 ? $this->buffer{$this->offset++} : substr($this->buffer, ($this->offset += $len) - $len, $len); return $len === 1 ? $this->buffer{$this->offset++} : substr($this->buffer, ($this->offset += $len) - $len, $len);
} }
public function put($v){ public function put(string $v) : void{
$this->buffer .= $v; $this->buffer .= $v;
} }
@ -72,7 +72,7 @@ abstract class NBTStream{
$this->buffer = ""; $this->buffer = "";
} }
public function read($buffer, $doMultiple = false){ public function read(string $buffer, bool $doMultiple = false) : void{
$this->offset = 0; $this->offset = 0;
$this->buffer = $buffer; $this->buffer = $buffer;
$this->data = $this->readTag(); $this->data = $this->readTag();
@ -85,7 +85,7 @@ abstract class NBTStream{
$this->buffer = ""; $this->buffer = "";
} }
public function readCompressed($buffer){ public function readCompressed(string $buffer) : void{
$this->read(zlib_decode($buffer)); $this->read(zlib_decode($buffer));
} }
@ -110,7 +110,7 @@ abstract class NBTStream{
return false; return false;
} }
public function writeCompressed($compression = ZLIB_ENCODING_GZIP, $level = 7){ public function writeCompressed(int $compression = ZLIB_ENCODING_GZIP, int $level = 7){
if(($write = $this->write()) !== false){ if(($write = $this->write()) !== false){
return zlib_encode($write, $compression, $level); return zlib_encode($write, $compression, $level);
} }
@ -118,7 +118,7 @@ abstract class NBTStream{
return false; return false;
} }
public function readTag(){ public function readTag() : Tag{
if($this->feof()){ if($this->feof()){
return new EndTag(); return new EndTag();
} }
@ -134,7 +134,7 @@ abstract class NBTStream{
return $tag; return $tag;
} }
public function writeTag(Tag $tag){ public function writeTag(Tag $tag) : void{
$this->putByte($tag->getType()); $this->putByte($tag->getType());
if($tag instanceof NamedTag){ if($tag instanceof NamedTag){
$this->putString($tag->getName()); $this->putString($tag->getName());
@ -150,7 +150,7 @@ abstract class NBTStream{
return Binary::readSignedByte($this->get(1)); return Binary::readSignedByte($this->get(1));
} }
public function putByte($v){ public function putByte(int $v) : void{
$this->buffer .= Binary::writeByte($v); $this->buffer .= Binary::writeByte($v);
} }
@ -158,32 +158,32 @@ abstract class NBTStream{
abstract public function getSignedShort() : int; abstract public function getSignedShort() : int;
abstract public function putShort($v); abstract public function putShort(int $v) : void;
abstract public function getInt() : int; abstract public function getInt() : int;
abstract public function putInt($v); abstract public function putInt(int $v) : void;
abstract public function getLong() : int; abstract public function getLong() : int;
abstract public function putLong($v); abstract public function putLong(int $v) : void;
abstract public function getFloat() : float; abstract public function getFloat() : float;
abstract public function putFloat($v); abstract public function putFloat(float $v) : void;
abstract public function getDouble() : float; abstract public function getDouble() : float;
abstract public function putDouble($v); abstract public function putDouble(float $v) : void;
public function getString(){ public function getString() : string{
return $this->get($this->getShort()); return $this->get($this->getShort());
} }
public function putString($v){ public function putString(string $v) : void{
$this->putShort(strlen($v)); $this->putShort(strlen($v));
$this->put($v); $this->put($v);
} }
@ -200,7 +200,7 @@ abstract class NBTStream{
return $data; return $data;
} }
private static function toArray(array &$data, Tag $tag){ private static function toArray(array &$data, Tag $tag) : void{
/** @var CompoundTag[]|ListTag[]|IntArrayTag[] $tag */ /** @var CompoundTag[]|ListTag[]|IntArrayTag[] $tag */
foreach($tag as $key => $value){ foreach($tag as $key => $value){
if($value instanceof CompoundTag or $value instanceof ListTag or $value instanceof IntArrayTag){ if($value instanceof CompoundTag or $value instanceof ListTag or $value instanceof IntArrayTag){
@ -212,7 +212,7 @@ abstract class NBTStream{
} }
} }
public static function fromArrayGuesser($key, $value){ public static function fromArrayGuesser(string $key, $value) : ?NamedTag{
if(is_int($value)){ if(is_int($value)){
return new IntTag($key, $value); return new IntTag($key, $value);
}elseif(is_float($value)){ }elseif(is_float($value)){
@ -226,7 +226,7 @@ abstract class NBTStream{
return null; return null;
} }
private static function fromArray(Tag $tag, array $data, callable $guesser){ private static function fromArray(Tag $tag, array $data, callable $guesser) : void{
foreach($data as $key => $value){ foreach($data as $key => $value){
if(is_array($value)){ if(is_array($value)){
$isNumeric = true; $isNumeric = true;
@ -250,7 +250,7 @@ abstract class NBTStream{
} }
} }
public function setArray(array $data, callable $guesser = null){ public function setArray(array $data, callable $guesser = null) : void{
$this->data = new CompoundTag("", []); $this->data = new CompoundTag("", []);
self::fromArray($this->data, $data, $guesser ?? [self::class, "fromArrayGuesser"]); self::fromArray($this->data, $data, $guesser ?? [self::class, "fromArrayGuesser"]);
} }
@ -265,7 +265,7 @@ abstract class NBTStream{
/** /**
* @param CompoundTag|array $data * @param CompoundTag|array $data
*/ */
public function setData($data){ public function setData($data) : void{
$this->data = $data; $this->data = $data;
} }
} }

View File

@ -35,7 +35,7 @@ class NetworkLittleEndianNBTStream extends LittleEndianNBTStream{
return Binary::readVarInt($this->buffer, $this->offset); return Binary::readVarInt($this->buffer, $this->offset);
} }
public function putInt($v){ public function putInt(int $v) : void{
$this->put(Binary::writeVarInt($v)); $this->put(Binary::writeVarInt($v));
} }
@ -43,15 +43,15 @@ class NetworkLittleEndianNBTStream extends LittleEndianNBTStream{
return Binary::readVarLong($this->buffer, $this->offset); return Binary::readVarLong($this->buffer, $this->offset);
} }
public function putLong($v){ public function putLong(int $v) : void{
$this->put(Binary::writeVarLong($v)); $this->put(Binary::writeVarLong($v));
} }
public function getString(){ public function getString() : string{
return $this->get(Binary::readUnsignedVarInt($this->buffer, $this->offset)); return $this->get(Binary::readUnsignedVarInt($this->buffer, $this->offset));
} }
public function putString($v){ public function putString(string $v) : void{
$this->put(Binary::writeUnsignedVarInt(strlen($v)) . $v); $this->put(Binary::writeUnsignedVarInt(strlen($v)) . $v);
} }