From 803582ca5ad24e1700070a70e16a5bd91758fc1d Mon Sep 17 00:00:00 2001 From: Paul Vogel Date: Thu, 4 May 2023 19:49:08 +0200 Subject: [PATCH 1/2] Change return types of many functions to static and adjust some phpdocs --- src/IO/Reader/AbstractReader.php | 4 ++-- src/IO/Writer/AbstractWriter.php | 4 ++-- src/Tag/CompoundTag.php | 4 ++-- src/Tag/DoubleTag.php | 2 +- src/Tag/FloatTag.php | 2 +- src/Tag/FloatValueTag.php | 2 +- src/Tag/IntValueTag.php | 2 +- src/Tag/ListTag.php | 11 ++++++----- src/Tag/LongTag.php | 2 +- src/Tag/StringTag.php | 4 ++-- src/Tag/Tag.php | 7 +++++-- src/Tag/TagOptions.php | 4 ++-- 12 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/IO/Reader/AbstractReader.php b/src/IO/Reader/AbstractReader.php index 585c991..9ec80cd 100644 --- a/src/IO/Reader/AbstractReader.php +++ b/src/IO/Reader/AbstractReader.php @@ -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; diff --git a/src/IO/Writer/AbstractWriter.php b/src/IO/Writer/AbstractWriter.php index f301ab8..dd359e3 100644 --- a/src/IO/Writer/AbstractWriter.php +++ b/src/IO/Writer/AbstractWriter.php @@ -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; diff --git a/src/Tag/CompoundTag.php b/src/Tag/CompoundTag.php index 32d48b3..015e119 100644 --- a/src/Tag/CompoundTag.php +++ b/src/Tag/CompoundTag.php @@ -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; @@ -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; diff --git a/src/Tag/DoubleTag.php b/src/Tag/DoubleTag.php index 33f1ee3..703279d 100644 --- a/src/Tag/DoubleTag.php +++ b/src/Tag/DoubleTag.php @@ -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); diff --git a/src/Tag/FloatTag.php b/src/Tag/FloatTag.php index 69f23a2..aab7954 100644 --- a/src/Tag/FloatTag.php +++ b/src/Tag/FloatTag.php @@ -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); diff --git a/src/Tag/FloatValueTag.php b/src/Tag/FloatValueTag.php index ee1c2c7..a6ff846 100644 --- a/src/Tag/FloatValueTag.php +++ b/src/Tag/FloatValueTag.php @@ -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; diff --git a/src/Tag/IntValueTag.php b/src/Tag/IntValueTag.php index 7551153..96d4462 100644 --- a/src/Tag/IntValueTag.php +++ b/src/Tag/IntValueTag.php @@ -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; diff --git a/src/Tag/ListTag.php b/src/Tag/ListTag.php index 5076f51..89d2a42 100644 --- a/src/Tag/ListTag.php +++ b/src/Tag/ListTag.php @@ -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"); @@ -153,7 +153,7 @@ protected static function readValueTagsRaw(Reader $reader, TagOptions $options, { $valueData = ""; - /** @var Tag $tagClass */ + /** @var class-string|null $tagClass */ $tagClass = Tag::getTagClass($contentType); if (is_null($tagClass)) { throw new Exception("Unknown ListTag content type " . $contentType); @@ -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); @@ -207,6 +207,7 @@ public function offsetSet($offset, $value) /** * @inheritDoc + * @throws Exception */ public function offsetUnset($offset) { @@ -214,7 +215,7 @@ public function offsetUnset($offset) 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); diff --git a/src/Tag/LongTag.php b/src/Tag/LongTag.php index 0956a00..f5d65cd 100644 --- a/src/Tag/LongTag.php +++ b/src/Tag/LongTag.php @@ -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); diff --git a/src/Tag/StringTag.php b/src/Tag/StringTag.php index ced3927..bafcc38 100644 --- a/src/Tag/StringTag.php +++ b/src/Tag/StringTag.php @@ -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; diff --git a/src/Tag/Tag.php b/src/Tag/Tag.php index 0d71172..dca6bf7 100644 --- a/src/Tag/Tag.php +++ b/src/Tag/Tag.php @@ -13,6 +13,9 @@ abstract class Tag implements JsonSerializable { public const TYPE = -1; + /** + * @var class-string[] + */ public const TAGS = [ TagType::TAG_End => EndTag::class, TagType::TAG_Byte => ByteTag::class, @@ -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; @@ -266,7 +269,7 @@ abstract protected function getValueString(): string; /** * @param int $type - * @return string|null + * @return class-string|null */ public static function getTagClass(int $type): ?string { diff --git a/src/Tag/TagOptions.php b/src/Tag/TagOptions.php index 27ee2b5..5e1cfe4 100644 --- a/src/Tag/TagOptions.php +++ b/src/Tag/TagOptions.php @@ -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; From 24bfd611b81ff3d2204c31d430f64d88e271fcda Mon Sep 17 00:00:00 2001 From: Paul Vogel Date: Thu, 4 May 2023 19:52:51 +0200 Subject: [PATCH 2/2] Adjust phpdoc with @var --- src/Tag/ListTag.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tag/ListTag.php b/src/Tag/ListTag.php index 89d2a42..d171ae1 100644 --- a/src/Tag/ListTag.php +++ b/src/Tag/ListTag.php @@ -83,7 +83,7 @@ public function setContentTag(int $contentTagType): static protected function readValues(Reader $reader, int $length): array { $values = []; - /** @var Tag $tagClass */ + /** @var class-string|null $tagClass */ $tagClass = Tag::getTagClass($this->contentTagType); if (is_null($tagClass)) { throw new Exception("Unknown ListTag content type " . $this->contentTagType);