Add typehints and PhpDoc to NBT API

This commit is contained in:
Dylan K. Taylor 2017-10-14 22:07:25 +01:00
parent ce67bc620a
commit 51906daad0
14 changed files with 60 additions and 56 deletions

View File

@ -39,15 +39,15 @@ class ByteArrayTag extends NamedTag{
parent::__construct($name, $value);
}
public function getType(){
public function getType() : int{
return NBT::TAG_ByteArray;
}
public function read(NBT $nbt, bool $network = false){
public function read(NBT $nbt, bool $network = false) : void{
$this->value = $nbt->get($nbt->getInt($network));
}
public function write(NBT $nbt, bool $network = false){
public function write(NBT $nbt, bool $network = false) : void{
$nbt->putInt(strlen($this->value), $network);
$nbt->put($this->value);
}
@ -64,7 +64,7 @@ class ByteArrayTag extends NamedTag{
*
* @throws \TypeError
*/
public function setValue($value){
public function setValue($value) : void{
if(!is_string($value)){
throw new \TypeError("ByteArrayTag value must be of type string, " . gettype($value) . " given");
}

View File

@ -39,15 +39,15 @@ class ByteTag extends NamedTag{
parent::__construct($name, $value);
}
public function getType(){
public function getType() : int{
return NBT::TAG_Byte;
}
public function read(NBT $nbt, bool $network = false){
public function read(NBT $nbt, bool $network = false) : void{
$this->value = $nbt->getSignedByte();
}
public function write(NBT $nbt, bool $network = false){
public function write(NBT $nbt, bool $network = false) : void{
$nbt->putByte($this->value);
}
@ -63,7 +63,7 @@ class ByteTag extends NamedTag{
*
* @throws \TypeError
*/
public function setValue($value){
public function setValue($value) : void{
if(!is_int($value)){
throw new \TypeError("ByteTag value must be of type int, " . gettype($value) . " given");
}elseif($value < -(2 ** 7) or $value > ((2 ** 7) - 1)){

View File

@ -55,7 +55,7 @@ class CompoundTag extends NamedTag implements \ArrayAccess{
*
* @throws \TypeError
*/
public function setValue($value){
public function setValue($value) : void{
if(is_array($value)){
foreach($value as $name => $tag){
if($tag instanceof NamedTag){
@ -385,11 +385,11 @@ class CompoundTag extends NamedTag implements \ArrayAccess{
unset($this->{$offset});
}
public function getType(){
public function getType() : int{
return NBT::TAG_Compound;
}
public function read(NBT $nbt, bool $network = false){
public function read(NBT $nbt, bool $network = false) : void{
$this->value = [];
do{
$tag = $nbt->readTag($network);
@ -399,7 +399,7 @@ class CompoundTag extends NamedTag implements \ArrayAccess{
}while(!($tag instanceof EndTag) and !$nbt->feof());
}
public function write(NBT $nbt, bool $network = false){
public function write(NBT $nbt, bool $network = false) : void{
foreach($this as $tag){
if($tag instanceof Tag and !($tag instanceof EndTag)){
$nbt->writeTag($tag, $network);

View File

@ -39,15 +39,15 @@ class DoubleTag extends NamedTag{
parent::__construct($name, $value);
}
public function getType(){
public function getType() : int{
return NBT::TAG_Double;
}
public function read(NBT $nbt, bool $network = false){
public function read(NBT $nbt, bool $network = false) : void{
$this->value = $nbt->getDouble();
}
public function write(NBT $nbt, bool $network = false){
public function write(NBT $nbt, bool $network = false) : void{
$nbt->putDouble($this->value);
}
@ -63,7 +63,7 @@ class DoubleTag extends NamedTag{
*
* @throws \TypeError
*/
public function setValue($value){
public function setValue($value) : void{
if(!is_float($value) and !is_int($value)){
throw new \TypeError("DoubleTag value must be of type double, " . gettype($value) . " given");
}

View File

@ -27,15 +27,15 @@ use pocketmine\nbt\NBT;
class EndTag extends Tag{
public function getType(){
public function getType() : int{
return NBT::TAG_End;
}
public function read(NBT $nbt, bool $network = false){
public function read(NBT $nbt, bool $network = false) : void{
}
public function write(NBT $nbt, bool $network = false){
public function write(NBT $nbt, bool $network = false) : void{
}
}

View File

@ -39,15 +39,15 @@ class FloatTag extends NamedTag{
parent::__construct($name, $value);
}
public function getType(){
public function getType() : int{
return NBT::TAG_Float;
}
public function read(NBT $nbt, bool $network = false){
public function read(NBT $nbt, bool $network = false) : void{
$this->value = $nbt->getFloat();
}
public function write(NBT $nbt, bool $network = false){
public function write(NBT $nbt, bool $network = false) : void{
$nbt->putFloat($this->value);
}
@ -58,7 +58,7 @@ class FloatTag extends NamedTag{
return parent::getValue();
}
public function setValue($value){
public function setValue($value) : void{
if(!is_float($value) and !is_int($value)){
throw new \TypeError("FloatTag value must be of type float, " . gettype($value) . " given");
}

View File

@ -39,16 +39,16 @@ class IntArrayTag extends NamedTag{
parent::__construct($name, $value);
}
public function getType(){
public function getType() : int{
return NBT::TAG_IntArray;
}
public function read(NBT $nbt, bool $network = false){
public function read(NBT $nbt, bool $network = false) : void{
$size = $nbt->getInt($network);
$this->value = array_values(unpack($nbt->endianness === NBT::LITTLE_ENDIAN ? "V*" : "N*", $nbt->get($size * 4)));
}
public function write(NBT $nbt, bool $network = false){
public function write(NBT $nbt, bool $network = false) : void{
$nbt->putInt(count($this->value), $network);
$nbt->put(pack($nbt->endianness === NBT::LITTLE_ENDIAN ? "V*" : "N*", ...$this->value));
}
@ -71,7 +71,7 @@ class IntArrayTag extends NamedTag{
*
* @throws \TypeError
*/
public function setValue($value){
public function setValue($value) : void{
if(!is_array($value)){
throw new \TypeError("IntArrayTag value must be of type int[], " . gettype($value) . " given");
}

View File

@ -37,15 +37,15 @@ class IntTag extends NamedTag{
parent::__construct($name, $value);
}
public function getType(){
public function getType() : int{
return NBT::TAG_Int;
}
public function read(NBT $nbt, bool $network = false){
public function read(NBT $nbt, bool $network = false) : void{
$this->value = $nbt->getInt($network);
}
public function write(NBT $nbt, bool $network = false){
public function write(NBT $nbt, bool $network = false) : void{
$nbt->putInt($this->value, $network);
}
@ -61,7 +61,7 @@ class IntTag extends NamedTag{
*
* @throws \TypeError
*/
public function setValue($value){
public function setValue($value) : void{
if(!is_int($value)){
throw new \TypeError("IntTag value must be of type int, " . gettype($value) . " given");
}elseif($value < -(2 ** 31) or $value > ((2 ** 31) - 1)){

View File

@ -63,7 +63,7 @@ class ListTag extends NamedTag implements \ArrayAccess, \Countable{
*
* @throws \TypeError
*/
public function setValue($value){
public function setValue($value) : void{
if(is_array($value)){
foreach($value as $name => $tag){
if($tag instanceof NamedTag){
@ -129,7 +129,7 @@ class ListTag extends NamedTag implements \ArrayAccess, \Countable{
return $count;
}
public function getType(){
public function getType() : int{
return NBT::TAG_List;
}
@ -141,7 +141,7 @@ class ListTag extends NamedTag implements \ArrayAccess, \Countable{
return $this->tagType;
}
public function read(NBT $nbt, bool $network = false){
public function read(NBT $nbt, bool $network = false) : void{
$this->value = [];
$this->tagType = $nbt->getByte();
$size = $nbt->getInt($network);
@ -154,7 +154,7 @@ class ListTag extends NamedTag implements \ArrayAccess, \Countable{
}
}
public function write(NBT $nbt, bool $network = false){
public function write(NBT $nbt, bool $network = false) : void{
if($this->tagType === NBT::TAG_End){ //previously empty list, try detecting type from tag children
$id = NBT::TAG_End;
foreach($this as $tag){
@ -162,7 +162,7 @@ class ListTag extends NamedTag implements \ArrayAccess, \Countable{
if($id === NBT::TAG_End){
$id = $tag->getType();
}elseif($id !== $tag->getType()){
return false;
return; //TODO: throw exception?
}
}
}
@ -182,8 +182,6 @@ class ListTag extends NamedTag implements \ArrayAccess, \Countable{
foreach($tags as $tag){
$tag->write($nbt, $network);
}
return true;
}
public function __toString(){

View File

@ -39,15 +39,15 @@ class LongTag extends NamedTag{
parent::__construct($name, $value);
}
public function getType(){
public function getType() : int{
return NBT::TAG_Long;
}
public function read(NBT $nbt, bool $network = false){
public function read(NBT $nbt, bool $network = false) : void{
$this->value = $nbt->getLong($network);
}
public function write(NBT $nbt, bool $network = false){
public function write(NBT $nbt, bool $network = false) : void{
$nbt->putLong($this->value, $network);
}

View File

@ -25,7 +25,7 @@ namespace pocketmine\nbt\tag;
abstract class NamedTag extends Tag{
/** @var string */
protected $__name;
/**
@ -39,11 +39,17 @@ abstract class NamedTag extends Tag{
}
}
public function getName(){
/**
* @return string
*/
public function getName() : string{
return $this->__name;
}
public function setName($name){
/**
* @param string $name
*/
public function setName(string $name) : void{
$this->__name = $name;
}
}

View File

@ -39,15 +39,15 @@ class ShortTag extends NamedTag{
parent::__construct($name, $value);
}
public function getType(){
public function getType() : int{
return NBT::TAG_Short;
}
public function read(NBT $nbt, bool $network = false){
public function read(NBT $nbt, bool $network = false) : void{
$this->value = $nbt->getSignedShort();
}
public function write(NBT $nbt, bool $network = false){
public function write(NBT $nbt, bool $network = false) : void{
$nbt->putShort($this->value);
}
@ -63,7 +63,7 @@ class ShortTag extends NamedTag{
*
* @throws \TypeError
*/
public function setValue($value){
public function setValue($value) : void{
if(!is_int($value)){
throw new \TypeError("ShortTag value must be of type int, " . gettype($value) . " given");
}elseif($value < -(2 ** 15) or $value > ((2 ** 15) - 1)){

View File

@ -39,15 +39,15 @@ class StringTag extends NamedTag{
parent::__construct($name, $value);
}
public function getType(){
public function getType() : int{
return NBT::TAG_String;
}
public function read(NBT $nbt, bool $network = false){
public function read(NBT $nbt, bool $network = false) : void{
$this->value = $nbt->getString($network);
}
public function write(NBT $nbt, bool $network = false){
public function write(NBT $nbt, bool $network = false) : void{
$nbt->putString($this->value, $network);
}
@ -63,7 +63,7 @@ class StringTag extends NamedTag{
*
* @throws \TypeError
*/
public function setValue($value){
public function setValue($value) : void{
if(!is_string($value)){
throw new \TypeError("ShortTag value must be of type int, " . gettype($value) . " given");
}

View File

@ -36,15 +36,15 @@ abstract class Tag extends \stdClass{
return $this->value;
}
abstract public function getType();
abstract public function getType() : int;
public function setValue($value){
public function setValue($value) : void{
$this->value = $value;
}
abstract public function write(NBT $nbt, bool $network = false);
abstract public function write(NBT $nbt, bool $network = false) : void;
abstract public function read(NBT $nbt, bool $network = false);
abstract public function read(NBT $nbt, bool $network = false) : void;
public function __toString(){
return (string) $this->value;