Skip to content

Commit

Permalink
Merge pull request #3 from pavog/fix-return-type
Browse files Browse the repository at this point in the history
Change return types of many functions to static and adjust some phpdocs
  • Loading branch information
KurtThiemann authored May 4, 2023
2 parents 7a4fcd6 + 24bfd61 commit c6b0d14
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/IO/Reader/AbstractReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function getFormat(): int

/**
* @param int $format
* @return AbstractReader
* @return $this
*/
public function setFormat(int $format): AbstractReader
public function setFormat(int $format): static
{
$this->format = $format;
$this->deserializer = null;
Expand Down
4 changes: 2 additions & 2 deletions src/IO/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function getFormat(): int

/**
* @param int $format
* @return AbstractWriter
* @return $this
*/
public function setFormat(int $format): AbstractWriter
public function setFormat(int $format): static
{
$this->format = $format;
$this->serializer = null;
Expand Down
4 changes: 2 additions & 2 deletions src/Tag/CompoundTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function jsonSerialize()
* @return $this
* @throws Exception
*/
public function set(?string $name, Tag $tag): CompoundTag
public function set(?string $name, Tag $tag): static
{
$this->offsetSet($name, $tag);
return $this;
Expand All @@ -250,7 +250,7 @@ public function set(?string $name, Tag $tag): CompoundTag
* @return $this
* @throws Exception
*/
public function delete(string $name): CompoundTag
public function delete(string $name): static
{
$this->offsetUnset($name);
return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/Tag/DoubleTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected static function readContentRaw(Reader $reader, TagOptions $options): s
/**
* @inheritDoc
*/
public function setValue(float $value): FloatValueTag
public function setValue(float $value): static
{
$this->resetRawValue();
return parent::setValue($value);
Expand Down
2 changes: 1 addition & 1 deletion src/Tag/FloatTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected static function readContentRaw(Reader $reader, TagOptions $options): s
/**
* @inheritDoc
*/
public function setValue(float $value): FloatValueTag
public function setValue(float $value): static
{
$this->resetRawValue();
return parent::setValue($value);
Expand Down
2 changes: 1 addition & 1 deletion src/Tag/FloatValueTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function getValue(): float
* @param float $value
* @return $this
*/
public function setValue(float $value): FloatValueTag
public function setValue(float $value): static
{
$this->value = $value;
return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/Tag/IntValueTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function getValue(): int
* @param int $value
* @return $this
*/
public function setValue(int $value): IntValueTag
public function setValue(int $value): static
{
$this->value = $value;
return $this;
Expand Down
13 changes: 7 additions & 6 deletions src/Tag/ListTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public function getContentTag(): int

/**
* @param int $contentTagType
* @return ListTag
* @return $this
* @throws Exception
*/
public function setContentTag(int $contentTagType): ListTag
public function setContentTag(int $contentTagType): static
{
if ($this->isRaw()) {
throw new Exception("Raw list tags cannot be modified");
Expand All @@ -83,7 +83,7 @@ public function setContentTag(int $contentTagType): ListTag
protected function readValues(Reader $reader, int $length): array
{
$values = [];
/** @var Tag $tagClass */
/** @var class-string<Tag>|null $tagClass */
$tagClass = Tag::getTagClass($this->contentTagType);
if (is_null($tagClass)) {
throw new Exception("Unknown ListTag content type " . $this->contentTagType);
Expand Down Expand Up @@ -153,7 +153,7 @@ protected static function readValueTagsRaw(Reader $reader, TagOptions $options,
{
$valueData = "";

/** @var Tag $tagClass */
/** @var class-string<Tag>|null $tagClass */
$tagClass = Tag::getTagClass($contentType);
if (is_null($tagClass)) {
throw new Exception("Unknown ListTag content type " . $contentType);
Expand Down Expand Up @@ -198,7 +198,7 @@ public function offsetSet($offset, $value)
throw new Exception("Raw list tags cannot be modified");
}

/** @var Tag $previousValue */
/** @var Tag|null $previousValue */
$previousValue = $this->valueArray[$offset] ?? null;
parent::offsetSet($offset, $value);
$value->setParentTag($this);
Expand All @@ -207,14 +207,15 @@ public function offsetSet($offset, $value)

/**
* @inheritDoc
* @throws Exception
*/
public function offsetUnset($offset)
{
if ($this->isRaw()) {
throw new Exception("Raw list tags cannot be modified");
}

/** @var Tag $previousValue */
/** @var Tag|null $previousValue */
$previousValue = $this->valueArray[$offset] ?? null;
$previousValue?->setParentTag(null);
parent::offsetUnset($offset);
Expand Down
2 changes: 1 addition & 1 deletion src/Tag/LongTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected static function readContentRaw(Reader $reader, TagOptions $options): s
/**
* @inheritDoc
*/
public function setValue(int $value): IntValueTag
public function setValue(int $value): static
{
$this->resetRawValue();
return parent::setValue($value);
Expand Down
4 changes: 2 additions & 2 deletions src/Tag/StringTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public function getValue(): string

/**
* @param string $value
* @return StringTag
* @return $this
*/
public function setValue(string $value): StringTag
public function setValue(string $value): static
{
$this->value = $value;
return $this;
Expand Down
7 changes: 5 additions & 2 deletions src/Tag/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ abstract class Tag implements JsonSerializable
{
public const TYPE = -1;

/**
* @var class-string<Tag>[]
*/
public const TAGS = [
TagType::TAG_End => EndTag::class,
TagType::TAG_Byte => ByteTag::class,
Expand Down Expand Up @@ -46,7 +49,7 @@ public function __construct(?TagOptions $options = null)
* @param string|null $name
* @return $this
*/
public function setName(?string $name): Tag
public function setName(?string $name): static
{
$this->name = $name;
return $this;
Expand Down Expand Up @@ -266,7 +269,7 @@ abstract protected function getValueString(): string;

/**
* @param int $type
* @return string|null
* @return class-string<Tag>|null
*/
public static function getTagClass(int $type): ?string
{
Expand Down
4 changes: 2 additions & 2 deletions src/Tag/TagOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public function getRawCompoundPaths(): array

/**
* @param string[]|null $parsedCompoundPaths
* @return TagOptions
* @return $this
*/
public function setParsedCompoundPaths(?array $parsedCompoundPaths): TagOptions
public function setParsedCompoundPaths(?array $parsedCompoundPaths): static
{
$this->parsedCompoundPaths = $parsedCompoundPaths;
return $this;
Expand Down

0 comments on commit c6b0d14

Please sign in to comment.