diff --git a/composer.json b/composer.json index 59200c4..968efe2 100644 --- a/composer.json +++ b/composer.json @@ -4,14 +4,18 @@ "license": "MIT", "keywords": ["dns"], "require": { - "php": ">=5.4.0" + "php": ">=7.0" }, "autoload": { "psr-4": { "LibDNS\\": "src/" - } + }, + "files": ["src/functions.php"] }, "support": { "issues": "https://github.com/DaveRandom/LibDNS/issues" + }, + "suggest": { + "ext-intl": "Required for IDN support" } } diff --git a/examples/AQuery.php b/examples/AQuery.php index 8ea7e0b..07df0e6 100644 --- a/examples/AQuery.php +++ b/examples/AQuery.php @@ -21,7 +21,7 @@ use \LibDNS\Decoder\DecoderFactory; // Config -$queryName = 'google.com'; +$queryName = 'faß.de'; $serverIP = '8.8.8.8'; $requestTimeout = 3; diff --git a/src/Decoder/Decoder.php b/src/Decoder/Decoder.php index e8d32fe..f3158b5 100644 --- a/src/Decoder/Decoder.php +++ b/src/Decoder/Decoder.php @@ -1,4 +1,4 @@ -packetFactory = $packetFactory; $this->messageFactory = $messageFactory; @@ -113,7 +115,7 @@ public function __construct( * @return string * @throws \UnexpectedValueException When the read operation does not result in the requested number of bytes */ - private function readDataFromPacket(Packet $packet, $length) + private function readDataFromPacket(Packet $packet, int $length): string { if ($packet->getBytesRemaining() < $length) { throw new \UnexpectedValueException('Decode error: Incomplete packet (tried to read ' . $length . ' bytes from index ' . $packet->getPointer()); @@ -131,7 +133,7 @@ private function readDataFromPacket(Packet $packet, $length) */ private function decodeHeader(DecodingContext $decodingContext, Message $message) { - $header = unpack('nid/nmeta/nqd/nan/nns/nar', $this->readDataFromPacket($decodingContext->getPacket(), 12)); + $header = \unpack('nid/nmeta/nqd/nan/nns/nar', $this->readDataFromPacket($decodingContext->getPacket(), 12)); if (!$header) { throw new \UnexpectedValueException('Decode error: Header unpack failed'); } @@ -140,10 +142,10 @@ private function decodeHeader(DecodingContext $decodingContext, Message $message $message->setType(($header['meta'] & 0b1000000000000000) >> 15); $message->setOpCode(($header['meta'] & 0b0111100000000000) >> 11); - $message->isAuthoritative(($header['meta'] & 0b0000010000000000) >> 10); - $message->isTruncated(($header['meta'] & 0b0000001000000000) >> 9); - $message->isRecursionDesired(($header['meta'] & 0b0000000100000000) >> 8); - $message->isRecursionAvailable(($header['meta'] & 0b0000000010000000) >> 7); + $message->isAuthoritative((bool)(($header['meta'] & 0b0000010000000000) >> 10)); + $message->isTruncated((bool)(($header['meta'] & 0b0000001000000000) >> 9)); + $message->isRecursionDesired((bool)(($header['meta'] & 0b0000000100000000) >> 8)); + $message->isRecursionAvailable((bool)(($header['meta'] & 0b0000000010000000) >> 7)); $message->setResponseCode($header['meta'] & 0b0000000000001111); $decodingContext->setExpectedQuestionRecords($header['qd']); @@ -161,7 +163,7 @@ private function decodeHeader(DecodingContext $decodingContext, Message $message * @return int The number of packet bytes consumed by the operation * @throws \UnexpectedValueException When the packet data is invalid */ - private function decodeAnything(DecodingContext $decodingContext, Anything $anything, $length) + private function decodeAnything(DecodingContext $decodingContext, Anything $anything, int $length): int { $anything->setValue($this->readDataFromPacket($decodingContext->getPacket(), $length)); @@ -177,7 +179,7 @@ private function decodeAnything(DecodingContext $decodingContext, Anything $anyt * @return int The number of packet bytes consumed by the operation * @throws \UnexpectedValueException When the packet data is invalid */ - private function decodeBitMap(DecodingContext $decodingContext, BitMap $bitMap, $length) + private function decodeBitMap(DecodingContext $decodingContext, BitMap $bitMap, int $length): int { $bitMap->setValue($this->readDataFromPacket($decodingContext->getPacket(), $length)); @@ -192,9 +194,9 @@ private function decodeBitMap(DecodingContext $decodingContext, BitMap $bitMap, * @return int The number of packet bytes consumed by the operation * @throws \UnexpectedValueException When the packet data is invalid */ - private function decodeChar(DecodingContext $decodingContext, Char $char) + private function decodeChar(DecodingContext $decodingContext, Char $char): int { - $value = unpack('C', $this->readDataFromPacket($decodingContext->getPacket(), 1))[1]; + $value = \unpack('C', $this->readDataFromPacket($decodingContext->getPacket(), 1))[1]; $char->setValue($value); return 1; @@ -208,10 +210,10 @@ private function decodeChar(DecodingContext $decodingContext, Char $char) * @return int The number of packet bytes consumed by the operation * @throws \UnexpectedValueException When the packet data is invalid */ - private function decodeCharacterString(DecodingContext $decodingContext, CharacterString $characterString) + private function decodeCharacterString(DecodingContext $decodingContext, CharacterString $characterString): int { $packet = $decodingContext->getPacket(); - $length = ord($this->readDataFromPacket($packet, 1)); + $length = \ord($this->readDataFromPacket($packet, 1)); $characterString->setValue($this->readDataFromPacket($packet, $length)); return $length + 1; @@ -225,32 +227,32 @@ private function decodeCharacterString(DecodingContext $decodingContext, Charact * @return int The number of packet bytes consumed by the operation * @throws \UnexpectedValueException When the packet data is invalid */ - private function decodeDomainName(DecodingContext $decodingContext, DomainName $domainName) + private function decodeDomainName(DecodingContext $decodingContext, DomainName $domainName): int { $packet = $decodingContext->getPacket(); - $startIndex = '0x' . dechex($packet->getPointer()); + $startIndex = '0x' . \dechex($packet->getPointer()); $labelRegistry = $decodingContext->getLabelRegistry(); $labels = []; $totalLength = 0; - while (++$totalLength && $length = ord($this->readDataFromPacket($packet, 1))) { + while (++$totalLength && $length = \ord($this->readDataFromPacket($packet, 1))) { $labelType = $length & 0b11000000; if ($labelType === 0b00000000) { $index = $packet->getPointer() - 1; $label = $this->readDataFromPacket($packet, $length); - array_unshift($labels, [$index, $label]); + \array_unshift($labels, [$index, $label]); $totalLength += $length; } else if ($labelType === 0b11000000) { - $index = (($length & 0b00111111) << 8) | ord($this->readDataFromPacket($packet, 1)); + $index = (($length & 0b00111111) << 8) | \ord($this->readDataFromPacket($packet, 1)); $ref = $labelRegistry->lookupLabel($index); if ($ref === null) { throw new \UnexpectedValueException('Decode error: Invalid compression pointer reference in domain name at position ' . $startIndex); } - array_unshift($labels, $ref); + \array_unshift($labels, $ref); $totalLength++; break; @@ -265,8 +267,8 @@ private function decodeDomainName(DecodingContext $decodingContext, DomainName $ $result = []; foreach ($labels as $label) { - if (is_int($label[0])) { - array_unshift($result, $label[1]); + if (\is_int($label[0])) { + \array_unshift($result, $label[1]); $labelRegistry->register($result, $label[0]); } else { $result = $label; @@ -285,9 +287,9 @@ private function decodeDomainName(DecodingContext $decodingContext, DomainName $ * @return int The number of packet bytes consumed by the operation * @throws \UnexpectedValueException When the packet data is invalid */ - private function decodeIPv4Address(DecodingContext $decodingContext, IPv4Address $ipv4Address) + private function decodeIPv4Address(DecodingContext $decodingContext, IPv4Address $ipv4Address): int { - $octets = unpack('C4', $this->readDataFromPacket($decodingContext->getPacket(), 4)); + $octets = \unpack('C4', $this->readDataFromPacket($decodingContext->getPacket(), 4)); $ipv4Address->setOctets($octets); return 4; @@ -301,9 +303,9 @@ private function decodeIPv4Address(DecodingContext $decodingContext, IPv4Address * @return int The number of packet bytes consumed by the operation * @throws \UnexpectedValueException When the packet data is invalid */ - private function decodeIPv6Address(DecodingContext $decodingContext, IPv6Address $ipv6Address) + private function decodeIPv6Address(DecodingContext $decodingContext, IPv6Address $ipv6Address): int { - $shorts = unpack('n8', $this->readDataFromPacket($decodingContext->getPacket(), 16)); + $shorts = \unpack('n8', $this->readDataFromPacket($decodingContext->getPacket(), 16)); $ipv6Address->setShorts($shorts); return 16; @@ -317,9 +319,9 @@ private function decodeIPv6Address(DecodingContext $decodingContext, IPv6Address * @return int The number of packet bytes consumed by the operation * @throws \UnexpectedValueException When the packet data is invalid */ - private function decodeLong(DecodingContext $decodingContext, Long $long) + private function decodeLong(DecodingContext $decodingContext, Long $long): int { - $value = unpack('N', $this->readDataFromPacket($decodingContext->getPacket(), 4))[1]; + $value = \unpack('N', $this->readDataFromPacket($decodingContext->getPacket(), 4))[1]; $long->setValue($value); return 4; @@ -333,9 +335,9 @@ private function decodeLong(DecodingContext $decodingContext, Long $long) * @return int The number of packet bytes consumed by the operation * @throws \UnexpectedValueException When the packet data is invalid */ - private function decodeShort(DecodingContext $decodingContext, Short $short) + private function decodeShort(DecodingContext $decodingContext, Short $short): int { - $value = unpack('n', $this->readDataFromPacket($decodingContext->getPacket(), 2))[1]; + $value = \unpack('n', $this->readDataFromPacket($decodingContext->getPacket(), 2))[1]; $short->setValue($value); return 2; @@ -351,7 +353,7 @@ private function decodeShort(DecodingContext $decodingContext, Short $short) * @throws \UnexpectedValueException When the packet data is invalid * @throws \InvalidArgumentException When the Type subtype is unknown */ - private function decodeType(DecodingContext $decodingContext, Type $type, $length) + private function decodeType(DecodingContext $decodingContext, Type $type, int $length): int { if ($type instanceof Anything) { $result = $this->decodeAnything($decodingContext, $type, $length); @@ -372,7 +374,7 @@ private function decodeType(DecodingContext $decodingContext, Type $type, $lengt } else if ($type instanceof Short) { $result = $this->decodeShort($decodingContext, $type); } else { - throw new \InvalidArgumentException('Unknown Type ' . get_class($type)); + throw new \InvalidArgumentException('Unknown Type ' . \get_class($type)); } return $result; @@ -385,12 +387,12 @@ private function decodeType(DecodingContext $decodingContext, Type $type, $lengt * @return \LibDNS\Records\Question * @throws \UnexpectedValueException When the record is invalid */ - private function decodeQuestionRecord(DecodingContext $decodingContext) + private function decodeQuestionRecord(DecodingContext $decodingContext): Question { /** @var \LibDNS\Records\Types\DomainName $domainName */ $domainName = $this->typeBuilder->build(Types::DOMAIN_NAME); $this->decodeDomainName($decodingContext, $domainName); - $meta = unpack('ntype/nclass', $this->readDataFromPacket($decodingContext->getPacket(), 4)); + $meta = \unpack('ntype/nclass', $this->readDataFromPacket($decodingContext->getPacket(), 4)); $question = $this->questionFactory->create($meta['type']); $question->setName($domainName); @@ -407,12 +409,12 @@ private function decodeQuestionRecord(DecodingContext $decodingContext) * @throws \UnexpectedValueException When the record is invalid * @throws \InvalidArgumentException When a type subtype is unknown */ - private function decodeResourceRecord(DecodingContext $decodingContext) + private function decodeResourceRecord(DecodingContext $decodingContext): Resource { /** @var \LibDNS\Records\Types\DomainName $domainName */ $domainName = $this->typeBuilder->build(Types::DOMAIN_NAME); $this->decodeDomainName($decodingContext, $domainName); - $meta = unpack('ntype/nclass/Nttl/nlength', $this->readDataFromPacket($decodingContext->getPacket(), 10)); + $meta = \unpack('ntype/nclass/Nttl/nlength', $this->readDataFromPacket($decodingContext->getPacket(), 10)); $resource = $this->resourceBuilder->build($meta['type']); $resource->setName($domainName); @@ -452,7 +454,7 @@ private function decodeResourceRecord(DecodingContext $decodingContext) * @throws \UnexpectedValueException When the packet data is invalid * @throws \InvalidArgumentException When a type subtype is unknown */ - public function decode($data) + public function decode(string $data): Message { $packet = $this->packetFactory->create($data); $decodingContext = $this->decodingContextFactory->create($packet); diff --git a/src/Decoder/DecoderFactory.php b/src/Decoder/DecoderFactory.php index b063667..0e6e58e 100644 --- a/src/Decoder/DecoderFactory.php +++ b/src/Decoder/DecoderFactory.php @@ -1,4 +1,4 @@ -packet; } @@ -82,7 +82,7 @@ public function getPacket() * * @return \LibDNS\Packets\LabelRegistry */ - public function getLabelRegistry() + public function getLabelRegistry(): LabelRegistry { return $this->labelRegistry; } @@ -92,7 +92,7 @@ public function getLabelRegistry() * * @return int */ - public function getExpectedQuestionRecords() + public function getExpectedQuestionRecords(): int { return $this->expectedQuestionRecords; } @@ -102,9 +102,9 @@ public function getExpectedQuestionRecords() * * @param int $expectedQuestionRecords */ - public function setExpectedQuestionRecords($expectedQuestionRecords) + public function setExpectedQuestionRecords(int $expectedQuestionRecords) { - $this->expectedQuestionRecords = (int) $expectedQuestionRecords; + $this->expectedQuestionRecords = $expectedQuestionRecords; } /** @@ -112,7 +112,7 @@ public function setExpectedQuestionRecords($expectedQuestionRecords) * * @return int */ - public function getExpectedAnswerRecords() + public function getExpectedAnswerRecords(): int { return $this->expectedAnswerRecords; } @@ -122,9 +122,9 @@ public function getExpectedAnswerRecords() * * @param int $expectedAnswerRecords */ - public function setExpectedAnswerRecords($expectedAnswerRecords) + public function setExpectedAnswerRecords(int $expectedAnswerRecords) { - $this->expectedAnswerRecords = (int) $expectedAnswerRecords; + $this->expectedAnswerRecords = $expectedAnswerRecords; } /** @@ -132,7 +132,7 @@ public function setExpectedAnswerRecords($expectedAnswerRecords) * * @return int */ - public function getExpectedAuthorityRecords() + public function getExpectedAuthorityRecords(): int { return $this->expectedAuthorityRecords; } @@ -142,9 +142,9 @@ public function getExpectedAuthorityRecords() * * @param int $expectedAuthorityRecords */ - public function setExpectedAuthorityRecords($expectedAuthorityRecords) + public function setExpectedAuthorityRecords(int $expectedAuthorityRecords) { - $this->expectedAuthorityRecords = (int) $expectedAuthorityRecords; + $this->expectedAuthorityRecords = $expectedAuthorityRecords; } /** @@ -152,7 +152,7 @@ public function setExpectedAuthorityRecords($expectedAuthorityRecords) * * @return int */ - public function getExpectedAdditionalRecords() + public function getExpectedAdditionalRecords(): int { return $this->expectedAdditionalRecords; } @@ -162,8 +162,8 @@ public function getExpectedAdditionalRecords() * * @param int $expectedAdditionalRecords */ - public function setExpectedAdditionalRecords($expectedAdditionalRecords) + public function setExpectedAdditionalRecords(int $expectedAdditionalRecords) { - $this->expectedAdditionalRecords = (int) $expectedAdditionalRecords; + $this->expectedAdditionalRecords = $expectedAdditionalRecords; } } diff --git a/src/Decoder/DecodingContextFactory.php b/src/Decoder/DecodingContextFactory.php index 12bacad..129860f 100644 --- a/src/Decoder/DecodingContextFactory.php +++ b/src/Decoder/DecodingContextFactory.php @@ -1,4 +1,4 @@ - $message->getID(), @@ -78,7 +78,7 @@ private function encodeHeader(EncodingContext $encodingContext, Message $message 'ar' => $message->getAdditionalRecords()->count() ]; - $header['meta'] |= $message->getType() << 16; + $header['meta'] |= $message->getType() << 15; $header['meta'] |= $message->getOpCode() << 11; $header['meta'] |= ((int) $message->isAuthoritative()) << 10; $header['meta'] |= ((int) $encodingContext->isTruncated()) << 9; @@ -86,7 +86,7 @@ private function encodeHeader(EncodingContext $encodingContext, Message $message $header['meta'] |= ((int) $message->isRecursionAvailable()) << 7; $header['meta'] |= $message->getResponseCode(); - return pack('n*', $header['id'], $header['meta'], $header['qd'], $header['an'], $header['ns'], $header['ar']); + return \pack('n*', $header['id'], $header['meta'], $header['qd'], $header['an'], $header['ns'], $header['ar']); } /** @@ -95,7 +95,7 @@ private function encodeHeader(EncodingContext $encodingContext, Message $message * @param \LibDNS\Records\Types\Anything $anything * @return string */ - private function encodeAnything(Anything $anything) + private function encodeAnything(Anything $anything): string { return $anything->getValue(); } @@ -106,7 +106,7 @@ private function encodeAnything(Anything $anything) * @param \LibDNS\Records\Types\BitMap $bitMap * @return string */ - private function encodeBitMap(BitMap $bitMap) + private function encodeBitMap(BitMap $bitMap): string { return $bitMap->getValue(); } @@ -117,9 +117,9 @@ private function encodeBitMap(BitMap $bitMap) * @param \LibDNS\Records\Types\Char $char * @return string */ - private function encodeChar(Char $char) + private function encodeChar(Char $char): string { - return chr($char->getValue()); + return \chr($char->getValue()); } /** @@ -128,10 +128,10 @@ private function encodeChar(Char $char) * @param \LibDNS\Records\Types\CharacterString $characterString * @return string */ - private function encodeCharacterString(CharacterString $characterString) + private function encodeCharacterString(CharacterString $characterString): string { $data = $characterString->getValue(); - return chr(strlen($data)) . $data; + return \chr(\strlen($data)) . $data; } /** @@ -141,7 +141,7 @@ private function encodeCharacterString(CharacterString $characterString) * @param \LibDNS\Encoder\EncodingContext $encodingContext * @return string */ - private function encodeDomainName(DomainName $domainName, EncodingContext $encodingContext) + private function encodeDomainName(DomainName $domainName, EncodingContext $encodingContext): string { $packetIndex = $encodingContext->getPacket()->getLength() + 12; $labelRegistry = $encodingContext->getLabelRegistry(); @@ -151,19 +151,19 @@ private function encodeDomainName(DomainName $domainName, EncodingContext $encod if ($encodingContext->useCompression()) { do { - $part = implode('.', $labels); + $part = \implode('.', $labels); $index = $labelRegistry->lookupIndex($part); if ($index === null) { $labelRegistry->register($part, $packetIndex); - $label = array_shift($labels); - $length = strlen($label); + $label = \array_shift($labels); + $length = \strlen($label); - $result .= chr($length) . $label; + $result .= \chr($length) . $label; $packetIndex += $length + 1; } else { - $result .= pack('n', 0b1100000000000000 | $index); + $result .= \pack('n', 0b1100000000000000 | $index); break; } } while($labels); @@ -173,7 +173,7 @@ private function encodeDomainName(DomainName $domainName, EncodingContext $encod } } else { foreach ($labels as $label) { - $result .= chr(strlen($label)) . $label; + $result .= \chr(\strlen($label)) . $label; } $result .= "\x00"; @@ -188,10 +188,10 @@ private function encodeDomainName(DomainName $domainName, EncodingContext $encod * @param \LibDNS\Records\Types\IPv4Address $ipv4Address * @return string */ - private function encodeIPv4Address(IPv4Address $ipv4Address) + private function encodeIPv4Address(IPv4Address $ipv4Address): string { $octets = $ipv4Address->getOctets(); - return pack('C*', $octets[0], $octets[1], $octets[2], $octets[3]); + return \pack('C*', $octets[0], $octets[1], $octets[2], $octets[3]); } /** @@ -200,10 +200,10 @@ private function encodeIPv4Address(IPv4Address $ipv4Address) * @param \LibDNS\Records\Types\IPv6Address $ipv6Address * @return string */ - private function encodeIPv6Address(IPv6Address $ipv6Address) + private function encodeIPv6Address(IPv6Address $ipv6Address): string { $shorts = $ipv6Address->getShorts(); - return pack('n*', $shorts[0], $shorts[1], $shorts[2], $shorts[3], $shorts[4], $shorts[5], $shorts[6], $shorts[7]); + return \pack('n*', $shorts[0], $shorts[1], $shorts[2], $shorts[3], $shorts[4], $shorts[5], $shorts[6], $shorts[7]); } /** @@ -212,9 +212,9 @@ private function encodeIPv6Address(IPv6Address $ipv6Address) * @param \LibDNS\Records\Types\Long $long * @return string */ - private function encodeLong(Long $long) + private function encodeLong(Long $long): string { - return pack('N', $long->getValue()); + return \pack('N', $long->getValue()); } /** @@ -223,9 +223,9 @@ private function encodeLong(Long $long) * @param \LibDNS\Records\Types\Short $short * @return string */ - private function encodeShort(Short $short) + private function encodeShort(Short $short): string { - return pack('n', $short->getValue()); + return \pack('n', $short->getValue()); } /** @@ -235,7 +235,7 @@ private function encodeShort(Short $short) * @param \LibDNS\Records\Types\Type $type * @return string */ - private function encodeType(EncodingContext $encodingContext, Type $type) + private function encodeType(EncodingContext $encodingContext, Type $type): string { if ($type instanceof Anything) { $result = $this->encodeAnything($type); @@ -256,7 +256,7 @@ private function encodeType(EncodingContext $encodingContext, Type $type) } else if ($type instanceof Short) { $result = $this->encodeShort($type); } else { - throw new \InvalidArgumentException('Unknown Type ' . get_class($type)); + throw new \InvalidArgumentException('Unknown Type ' . \get_class($type)); } return $result; @@ -273,9 +273,9 @@ private function encodeQuestionRecord(EncodingContext $encodingContext, Question if (!$encodingContext->isTruncated()) { $packet = $encodingContext->getPacket(); $name = $this->encodeDomainName($record->getName(), $encodingContext); - $meta = pack('n*', $record->getType(), $record->getClass()); + $meta = \pack('n*', $record->getType(), $record->getClass()); - if (12 + $packet->getLength() + strlen($name) + 4 > 512) { + if (12 + $packet->getLength() + \strlen($name) + 4 > 512) { $encodingContext->isTruncated(true); } else { $packet->write($name); @@ -301,9 +301,9 @@ private function encodeResourceRecord(EncodingContext $encodingContext, Resource $data .= $this->encodeType($encodingContext, $field); } - $meta = pack('n2Nn', $record->getType(), $record->getClass(), $record->getTTL(), strlen($data)); + $meta = \pack('n2Nn', $record->getType(), $record->getClass(), $record->getTTL(), \strlen($data)); - if (12 + $packet->getLength() + strlen($name) + 10 + strlen($data) > 512) { + if (12 + $packet->getLength() + \strlen($name) + 10 + \strlen($data) > 512) { $encodingContext->isTruncated(true); } else { $packet->write($name); @@ -320,7 +320,7 @@ private function encodeResourceRecord(EncodingContext $encodingContext, Resource * @param bool $compress Enable message compression * @return string */ - public function encode(Message $message, $compress = true) + public function encode(Message $message, $compress = true): string { $packet = $this->packetFactory->create(); $encodingContext = $this->encodingContextFactory->create($packet, $compress); diff --git a/src/Encoder/EncoderFactory.php b/src/Encoder/EncoderFactory.php index 1101ca5..5bd73f8 100644 --- a/src/Encoder/EncoderFactory.php +++ b/src/Encoder/EncoderFactory.php @@ -1,4 +1,4 @@ -packet = $packet; $this->labelRegistry = $labelRegistry; - $this->compress = (bool) $compress; + $this->compress = $compress; } /** @@ -64,7 +64,7 @@ public function __construct(Packet $packet, LabelRegistry $labelRegistry, $compr * * @return \LibDNS\Packets\Packet */ - public function getPacket() + public function getPacket(): Packet { return $this->packet; } @@ -74,7 +74,7 @@ public function getPacket() * * @return \LibDNS\Packets\LabelRegistry */ - public function getLabelRegistry() + public function getLabelRegistry(): LabelRegistry { return $this->labelRegistry; } @@ -84,7 +84,7 @@ public function getLabelRegistry() * * @return bool */ - public function useCompression() + public function useCompression(): bool { return $this->compress; } @@ -95,12 +95,12 @@ public function useCompression() * @param bool $truncate * @return bool */ - public function isTruncated($truncate = null) + public function isTruncated(bool $truncate = null): bool { $result = $this->truncate; if ($truncate !== null) { - $this->truncate = (bool) $truncate; + $this->truncate = $truncate; } return $result; diff --git a/src/Encoder/EncodingContextFactory.php b/src/Encoder/EncodingContextFactory.php index 76f5abf..8642385 100644 --- a/src/Encoder/EncodingContextFactory.php +++ b/src/Encoder/EncodingContextFactory.php @@ -1,4 +1,4 @@ - */ -class Enumeration +abstract class Enumeration { - final public function __construct() + final protected function __construct() { throw new \LogicException('Enumerations cannot be instantiated'); } diff --git a/src/Messages/Message.php b/src/Messages/Message.php index 1667ebe..673ae3e 100644 --- a/src/Messages/Message.php +++ b/src/Messages/Message.php @@ -1,4 +1,4 @@ -questionRecords = $recordCollectionFactory->create(RecordTypes::QUESTION); $this->answerRecords = $recordCollectionFactory->create(RecordTypes::RESOURCE); @@ -109,7 +110,7 @@ public function __construct(RecordCollectionFactory $recordCollectionFactory, $t * * @return int */ - public function getID() + public function getID(): int { return $this->id; } @@ -120,9 +121,8 @@ public function getID() * @param int $id The new value * @throws \RangeException When the supplied value is outside the valid range 0 - 65535 */ - public function setID($id) + public function setID(int $id) { - $id = (int) $id; if ($id < 0 || $id > 65535) { throw new \RangeException('Message ID must be in the range 0 - 65535'); } @@ -135,7 +135,7 @@ public function setID($id) * * @return int */ - public function getType() + public function getType(): int { return $this->type; } @@ -146,9 +146,8 @@ public function getType() * @param int $type The new value * @throws \RangeException When the supplied value is outside the valid range 0 - 1 */ - public function setType($type) + public function setType(int $type) { - $type = (int) $type; if ($type < 0 || $type > 1) { throw new \RangeException('Message type must be in the range 0 - 1'); } @@ -161,7 +160,7 @@ public function setType($type) * * @return int */ - public function getOpCode() + public function getOpCode(): int { return $this->opCode; } @@ -172,9 +171,8 @@ public function getOpCode() * @param int $opCode The new value * @throws \RangeException When the supplied value is outside the valid range 0 - 15 */ - public function setOpCode($opCode) + public function setOpCode(int $opCode) { - $opCode = (int) $opCode; if ($opCode < 0 || $opCode > 15) { throw new \RangeException('Message opcode must be in the range 0 - 15'); } @@ -188,15 +186,15 @@ public function setOpCode($opCode) * @param bool $newValue The new value * @return bool The old value */ - public function isAuthoritative($newValue = null) + public function isAuthoritative(bool $newValue = null): bool { $result = $this->authoritative; if ($newValue !== null) { - $this->authoritative = (bool) $newValue; + $this->authoritative = $newValue; } - return (bool) $result; + return $result; } /** @@ -205,15 +203,15 @@ public function isAuthoritative($newValue = null) * @param bool $newValue The new value * @return bool The old value */ - public function isTruncated($newValue = null) + public function isTruncated(bool $newValue = null): bool { $result = $this->truncated; if ($newValue !== null) { - $this->truncated = (bool) $newValue; + $this->truncated = $newValue; } - return (bool) $result; + return $result; } /** @@ -222,15 +220,15 @@ public function isTruncated($newValue = null) * @param bool $newValue The new value * @return bool The old value */ - public function isRecursionDesired($newValue = null) + public function isRecursionDesired(bool $newValue = null): bool { $result = $this->recursionDesired; if ($newValue !== null) { - $this->recursionDesired = (bool) $newValue; + $this->recursionDesired = $newValue; } - return (bool) $result; + return $result; } /** @@ -239,15 +237,15 @@ public function isRecursionDesired($newValue = null) * @param bool $newValue The new value * @return bool The old value */ - public function isRecursionAvailable($newValue = null) + public function isRecursionAvailable(bool $newValue = null): bool { $result = $this->recursionAvailable; if ($newValue !== null) { - $this->recursionAvailable = (bool) $newValue; + $this->recursionAvailable = $newValue; } - return (bool) $result; + return $result; } /** @@ -255,7 +253,7 @@ public function isRecursionAvailable($newValue = null) * * @return int */ - public function getResponseCode() + public function getResponseCode(): int { return $this->opCode; } @@ -266,9 +264,8 @@ public function getResponseCode() * @param int $responseCode The new value * @throws \RangeException When the supplied value is outside the valid range 0 - 15 */ - public function setResponseCode($responseCode) + public function setResponseCode(int $responseCode) { - $responseCode = (int) $responseCode; if ($responseCode < 0 || $responseCode > 15) { throw new \RangeException('Message response code must be in the range 0 - 15'); } @@ -281,7 +278,7 @@ public function setResponseCode($responseCode) * * @return \LibDNS\Records\RecordCollection */ - public function getQuestionRecords() + public function getQuestionRecords(): RecordCollection { return $this->questionRecords; } @@ -291,7 +288,7 @@ public function getQuestionRecords() * * @return \LibDNS\Records\RecordCollection */ - public function getAnswerRecords() + public function getAnswerRecords(): RecordCollection { return $this->answerRecords; } @@ -301,7 +298,7 @@ public function getAnswerRecords() * * @return \LibDNS\Records\RecordCollection */ - public function getAuthorityRecords() + public function getAuthorityRecords(): RecordCollection { return $this->authorityRecords; } @@ -311,7 +308,7 @@ public function getAuthorityRecords() * * @return \LibDNS\Records\RecordCollection */ - public function getAdditionalRecords() + public function getAdditionalRecords(): RecordCollection { return $this->additionalRecords; } diff --git a/src/Messages/MessageFactory.php b/src/Messages/MessageFactory.php index e393163..fd6e694 100644 --- a/src/Messages/MessageFactory.php +++ b/src/Messages/MessageFactory.php @@ -1,4 +1,4 @@ - */ -class MessageOpCodes extends Enumeration +final class MessageOpCodes extends Enumeration { const QUERY = 0; const IQUERY = 1; diff --git a/src/Messages/MessageResponseCodes.php b/src/Messages/MessageResponseCodes.php index cf4f271..0faf1dc 100644 --- a/src/Messages/MessageResponseCodes.php +++ b/src/Messages/MessageResponseCodes.php @@ -1,4 +1,4 @@ - */ -class MessageResponseCodes extends Enumeration +final class MessageResponseCodes extends Enumeration { const NO_ERROR = 0; const FORMAT_ERROR = 1; diff --git a/src/Messages/MessageTypes.php b/src/Messages/MessageTypes.php index f36c05a..437a1fd 100644 --- a/src/Messages/MessageTypes.php +++ b/src/Messages/MessageTypes.php @@ -1,4 +1,4 @@ - */ -class MessageTypes extends Enumeration +final class MessageTypes extends Enumeration { const QUERY = 0; const RESPONSE = 1; diff --git a/src/Packets/LabelRegistry.php b/src/Packets/LabelRegistry.php index a836648..3d11852 100644 --- a/src/Packets/LabelRegistry.php +++ b/src/Packets/LabelRegistry.php @@ -1,4 +1,4 @@ -labels[$label]) ? $this->labels[$label] : null; + return $this->labels[$label] ?? null; } /** @@ -73,8 +73,8 @@ public function lookupIndex($label) * @param int $index * @return string[]|null */ - public function lookupLabel($index) + public function lookupLabel(int $index) { - return isset($this->indexes[$index]) ? $this->indexes[$index] : null; + return $this->indexes[$index] ?? null; } } diff --git a/src/Packets/Packet.php b/src/Packets/Packet.php index 3bd0570..19b64b1 100644 --- a/src/Packets/Packet.php +++ b/src/Packets/Packet.php @@ -1,4 +1,4 @@ -data = (string) $data; - $this->length = strlen($this->data); + $this->data = $data; + $this->length = \strlen($this->data); } /** @@ -55,22 +55,21 @@ public function __construct($data = '') * @return string * @throws \OutOfBoundsException When the pointer position is invalid or the supplied length is negative */ - public function read($length = null) + public function read(int $length = null): string { if ($this->pointer > $this->length) { throw new \OutOfBoundsException('Pointer position invalid'); } if ($length === null) { - $result = substr($this->data, $this->pointer); + $result = \substr($this->data, $this->pointer); $this->pointer = $this->length; } else { - $length = (int) $length; if ($length < 0) { throw new \OutOfBoundsException('Length must be a positive integer'); } - $result = substr($this->data, $this->pointer, (int) $length); + $result = \substr($this->data, $this->pointer, $length); $this->pointer += $length; } @@ -83,9 +82,9 @@ public function read($length = null) * @param string $data The data to append * @return int The number of bytes written */ - public function write($data) + public function write(string $data): int { - $length = strlen($data); + $length = \strlen($data); $this->data .= $data; $this->length += $length; @@ -106,7 +105,7 @@ public function reset() * * @return int */ - public function getPointer() + public function getPointer(): int { return $this->pointer; } @@ -116,7 +115,7 @@ public function getPointer() * * @return int */ - public function getLength() + public function getLength(): int { return $this->length; } @@ -126,7 +125,7 @@ public function getLength() * * @return int */ - public function getBytesRemaining() + public function getBytesRemaining(): int { return $this->length - $this->pointer; } diff --git a/src/Packets/PacketFactory.php b/src/Packets/PacketFactory.php index 4e4566c..5ffad14 100644 --- a/src/Packets/PacketFactory.php +++ b/src/Packets/PacketFactory.php @@ -1,4 +1,4 @@ -typeFactory = $typeFactory; $this->type = $type; diff --git a/src/Records/QuestionFactory.php b/src/Records/QuestionFactory.php index edb7ca0..a0e0cb9 100644 --- a/src/Records/QuestionFactory.php +++ b/src/Records/QuestionFactory.php @@ -1,4 +1,4 @@ - */ -class RData implements \Iterator, \Countable +class RData implements \IteratorAggregate, \Countable { /** * @var \LibDNS\Records\Types\Type[] The items that make up the complex type @@ -35,11 +35,6 @@ class RData implements \Iterator, \Countable */ private $typeDef; - /** - * @var bool Whether the iteration pointer has more elements to yield - */ - private $pointerValid = 0; - /** * Constructor * @@ -58,9 +53,9 @@ public function __construct(TypeDefinition $typeDef) public function __toString() { if ($handler = $this->typeDef->getToStringFunction()) { - $result = call_user_func_array($handler, $this->fields); + $result = \call_user_func_array($handler, $this->fields); } else { - $result = implode(',', $this->fields); + $result = \implode(',', $this->fields); } return $result; @@ -73,7 +68,7 @@ public function __toString() * @return \LibDNS\Records\Types\Type * @throws \OutOfBoundsException When the supplied index does not refer to a valid field */ - public function getField($index) + public function getField(int $index) { if (!isset($this->fields[$index])) { throw new \OutOfBoundsException('Index ' . $index . ' does not refer to a valid field'); @@ -89,7 +84,7 @@ public function getField($index) * @param \LibDNS\Records\Types\Type $value * @throws \InvalidArgumentException When the supplied index/value pair does not match the type definition */ - public function setField($index, Type $value) + public function setField(int $index, Type $value) { if (!$this->typeDef->getFieldDefinition($index)->assertDataValid($value)) { throw new \InvalidArgumentException('The supplied value is not valid for the specified index'); @@ -105,7 +100,7 @@ public function setField($index, Type $value) * @return \LibDNS\Records\Types\Type * @throws \OutOfBoundsException When the supplied name does not refer to a valid field */ - public function getFieldByName($name) + public function getFieldByName(string $name): Type { return $this->getField($this->typeDef->getFieldIndexByName($name)); } @@ -118,7 +113,7 @@ public function getFieldByName($name) * @throws \OutOfBoundsException When the supplied name does not refer to a valid field * @throws \InvalidArgumentException When the supplied value does not match the type definition */ - public function setFieldByName($name, Type $value) + public function setFieldByName(string $name, Type $value) { $this->setField($this->typeDef->getFieldIndexByName($name), $value); } @@ -128,56 +123,19 @@ public function setFieldByName($name, Type $value) * * @return \LibDNS\Records\TypeDefinitions\TypeDefinition */ - public function getTypeDefinition() + public function getTypeDefinition(): TypeDefinition { return $this->typeDef; } /** - * Get the field indicated by the iteration pointer (Iterator interface) - * - * @return \LibDNS\Records\Types\Type - */ - public function current() - { - return current($this->fields); - } - - /** - * Get the value of the iteration pointer (Iterator interface) - * - * @return int - */ - public function key() - { - return key($this->fields); - } - - /** - * Increment the iteration pointer (Iterator interface) - */ - public function next() - { - $this->pointerValid = next($this->fields) !== false; - } - - /** - * Reset the iteration pointer to the beginning (Iterator interface) - */ - public function rewind() - { - reset($this->fields); - $this->pointerValid = count($this->fields) > 0; - } - - /** - * Test whether the iteration pointer indicates a valid field (Iterator interface) + * Retrieve an iterator (IteratorAggregate interface) * - * @return bool + * @return \Iterator */ - public function valid() + public function getIterator(): \Iterator { - return $this->pointerValid; + return new \ArrayIterator($this->fields); } /** @@ -185,8 +143,8 @@ public function valid() * * @return int */ - public function count() + public function count(): int { - return count($this->fields); + return \count($this->fields); } } diff --git a/src/Records/RDataBuilder.php b/src/Records/RDataBuilder.php index 6bebf00..b8734b5 100644 --- a/src/Records/RDataBuilder.php +++ b/src/Records/RDataBuilder.php @@ -1,4 +1,4 @@ -rDataFactory->create($typeDefinition); diff --git a/src/Records/RDataFactory.php b/src/Records/RDataFactory.php index 133e869..7b60cda 100644 --- a/src/Records/RDataFactory.php +++ b/src/Records/RDataFactory.php @@ -1,4 +1,4 @@ -name; } @@ -63,7 +63,7 @@ public function getName() public function setName($name) { if (!($name instanceof DomainName)) { - $name = $this->typeFactory->createDomainName($name); + $name = $this->typeFactory->createDomainName((string)$name); } $this->name = $name; @@ -74,7 +74,7 @@ public function setName($name) * * @return int */ - public function getType() + public function getType(): int { return $this->type; } @@ -84,7 +84,7 @@ public function getType() * * @return int */ - public function getClass() + public function getClass(): int { return $this->class; } @@ -95,9 +95,8 @@ public function getClass() * @param int $class The new value, can be indicated using the ResourceClasses/ResourceQClasses enums * @throws \RangeException When the supplied value is outside the valid range 0 - 65535 */ - public function setClass($class) + public function setClass(int $class) { - $class = (int) $class; if ($class < 0 || $class > 65535) { throw new \RangeException('Record class must be in the range 0 - 65535'); } diff --git a/src/Records/RecordCollection.php b/src/Records/RecordCollection.php index a0de99a..c7f434e 100644 --- a/src/Records/RecordCollection.php +++ b/src/Records/RecordCollection.php @@ -1,4 +1,4 @@ - */ -class RecordCollection implements \Iterator, \Countable +class RecordCollection implements \IteratorAggregate, \Countable { /** * @var \LibDNS\Records\Record[] List of records held in the collection @@ -42,11 +42,6 @@ class RecordCollection implements \Iterator, \Countable */ private $type; - /** - * @var int Iteration pointer - */ - private $position = 0; - /** * Constructor * @@ -86,7 +81,7 @@ private function removeFromNameMap(Record $record) if (!empty($this->nameMap[$name = (string) $record->getName()])) { foreach ($this->nameMap[$name] as $key => $item) { if ($item === $record) { - array_splice($this->nameMap[$name], $key, 1); + \array_splice($this->nameMap[$name], $key, 1); break; } } @@ -141,9 +136,9 @@ public function remove(Record $record) * @param bool $sameInstance Whether to perform strict comparisons in search * @return bool */ - public function contains(Record $record, $sameInstance = false) + public function contains(Record $record, bool $sameInstance = false): bool { - return in_array($record, $this->records, (bool) $sameInstance); + return \in_array($record, $this->records, $sameInstance); } /** @@ -152,9 +147,9 @@ public function contains(Record $record, $sameInstance = false) * @param string $name The name to match records against * @return \LibDNS\Records\Record[] */ - public function getRecordsByName($name) + public function getRecordsByName(string $name): array { - return isset($this->nameMap[$name = strtolower($name)]) ? $this->nameMap[$name] : []; + return $this->nameMap[\strtolower($name)] ?? []; } /** @@ -164,7 +159,7 @@ public function getRecordsByName($name) * @return \LibDNS\Records\Record * @throws \OutOfBoundsException When the supplied index does not refer to a valid record */ - public function getRecordByIndex($index) + public function getRecordByIndex(int $index): Record { if (isset($this->records[$index])) { return $this->records[$index]; @@ -179,11 +174,11 @@ public function getRecordByIndex($index) * @param string $name The name to match records against * @return int The number of records removed */ - public function clearRecordsByName($name) + public function clearRecordsByName(string $name): int { $count = 0; - if (isset($this->nameMap[$name = strtolower($name)])) { + if (isset($this->nameMap[$name = \strtolower($name)])) { unset($this->nameMap[$name]); foreach ($this->records as $index => $record) { @@ -193,7 +188,7 @@ public function clearRecordsByName($name) } } - $this->records = array_values($this->records); + $this->records = \array_values($this->records); } return $count; @@ -205,7 +200,7 @@ public function clearRecordsByName($name) public function clear() { $this->records = $this->nameMap = []; - $this->length = $this->position = 0; + $this->length = 0; } /** @@ -213,9 +208,9 @@ public function clear() * * @return string[] */ - public function getNames() + public function getNames(): array { - return array_keys($this->nameMap); + return \array_keys($this->nameMap); } /** @@ -223,60 +218,19 @@ public function getNames() * * @return int */ - public function getType() + public function getType(): int { return $this->type; } /** - * Get the record indicated by the iteration pointer (Iterator interface) - * - * @return \LibDNS\Records\Record - * @throws \OutOfBoundsException When the pointer does not refer to a valid record - */ - public function current() - { - if (!isset($this->records[$this->position])) { - throw new \OutOfBoundsException('The current pointer position is invalid'); - } - - return $this->records[$this->position]; - } - - /** - * Get the value of the iteration pointer (Iterator interface) - * - * @return int - */ - public function key() - { - return $this->position; - } - - /** - * Increment the iteration pointer (Iterator interface) - */ - public function next() - { - $this->position++; - } - - /** - * Reset the iteration pointer to the beginning (Iterator interface) - */ - public function rewind() - { - $this->position = 0; - } - - /** - * Test whether the iteration pointer indicates a valid record (Iterator interface) + * Retrieve an iterator (IteratorAggregate interface) * - * @return bool + * @return \Iterator */ - public function valid() + public function getIterator(): \Iterator { - return isset($this->records[$this->position]); + return new \ArrayIterator($this->records); } /** @@ -284,7 +238,7 @@ public function valid() * * @return int */ - public function count() + public function count(): int { return $this->length; } diff --git a/src/Records/RecordCollectionFactory.php b/src/Records/RecordCollectionFactory.php index 9f18840..95428fe 100644 --- a/src/Records/RecordCollectionFactory.php +++ b/src/Records/RecordCollectionFactory.php @@ -1,4 +1,4 @@ - */ -class RecordTypes extends Enumeration +final class RecordTypes extends Enumeration { const QUESTION = 0; const RESOURCE = 1; diff --git a/src/Records/Resource.php b/src/Records/Resource.php index 1404e0a..471d385 100644 --- a/src/Records/Resource.php +++ b/src/Records/Resource.php @@ -1,4 +1,4 @@ -typeFactory = $typeFactory; $this->type = $type; @@ -53,7 +53,7 @@ public function __construct(TypeFactory $typeFactory, $type, $data) * * @return int */ - public function getTTL() + public function getTTL(): int { return $this->ttl; } @@ -64,9 +64,8 @@ public function getTTL() * @param int $ttl The new value * @throws \RangeException When the supplied value is outside the valid range 0 - 4294967296 */ - public function setTTL($ttl) + public function setTTL(int $ttl) { - $ttl = (int) $ttl; if ($ttl < 0 || $ttl > 4294967296) { throw new \RangeException('Record class must be in the range 0 - 4294967296'); } @@ -79,7 +78,7 @@ public function setTTL($ttl) * * @return \LibDNS\Records\RData */ - public function getData() + public function getData(): RData { return $this->data; } diff --git a/src/Records/ResourceBuilder.php b/src/Records/ResourceBuilder.php index 8addc4a..93e021e 100644 --- a/src/Records/ResourceBuilder.php +++ b/src/Records/ResourceBuilder.php @@ -1,4 +1,4 @@ -typeDefinitionManager->getTypeDefinition($type); $rData = $this->rDataBuilder->build($typeDefinition); diff --git a/src/Records/ResourceBuilderFactory.php b/src/Records/ResourceBuilderFactory.php index 3c7ec1d..1bac743 100644 --- a/src/Records/ResourceBuilderFactory.php +++ b/src/Records/ResourceBuilderFactory.php @@ -1,4 +1,4 @@ - */ -class ResourceClasses extends Enumeration +abstract class ResourceClasses extends Enumeration { const IN = 1; const CS = 2; diff --git a/src/Records/ResourceFactory.php b/src/Records/ResourceFactory.php index 72456aa..627a7f7 100644 --- a/src/Records/ResourceFactory.php +++ b/src/Records/ResourceFactory.php @@ -1,4 +1,4 @@ - */ -class ResourceQClasses extends ResourceClasses +final class ResourceQClasses extends ResourceClasses { const ANY = 255; } diff --git a/src/Records/ResourceQTypes.php b/src/Records/ResourceQTypes.php index 7eccc77..dc00042 100644 --- a/src/Records/ResourceQTypes.php +++ b/src/Records/ResourceQTypes.php @@ -1,4 +1,4 @@ - */ -class ResourceQTypes extends ResourceTypes +final class ResourceQTypes extends ResourceTypes { const AXFR = 252; const MAILB = 253; diff --git a/src/Records/ResourceTypes.php b/src/Records/ResourceTypes.php index ccb7955..60f9717 100644 --- a/src/Records/ResourceTypes.php +++ b/src/Records/ResourceTypes.php @@ -1,4 +1,4 @@ - */ -class ResourceTypes extends Enumeration +abstract class ResourceTypes extends Enumeration { const A = 1; const AAAA = 28; diff --git a/src/Records/TypeDefinitions/FieldDefinition.php b/src/Records/TypeDefinitions/FieldDefinition.php index e15a0a0..df304a5 100644 --- a/src/Records/TypeDefinitions/FieldDefinition.php +++ b/src/Records/TypeDefinitions/FieldDefinition.php @@ -1,4 +1,4 @@ -index = (int) $index; - $this->name = (string) $name; - $this->type = (int) $type; - $this->allowsMultiple = (bool) $allowsMultiple; - $this->minimumValues = (int) $minimumValues; + $this->index = $index; + $this->name = $name; + $this->type = $type; + $this->allowsMultiple = $allowsMultiple; + $this->minimumValues = $minimumValues; } /** @@ -82,7 +82,7 @@ public function __construct($index, $name, $type, $allowsMultiple, $minimumValue * * @return int */ - public function getIndex() + public function getIndex(): int { return $this->index; } @@ -92,7 +92,7 @@ public function getIndex() * * @return string */ - public function getName() + public function getName(): string { return $this->name; } @@ -102,7 +102,7 @@ public function getName() * * @return int */ - public function getType() + public function getType(): int { return $this->type; } @@ -112,7 +112,7 @@ public function getType() * * @return bool */ - public function allowsMultiple() + public function allowsMultiple(): bool { return $this->allowsMultiple; } @@ -122,7 +122,7 @@ public function allowsMultiple() * * @return int */ - public function getMinimumValues() + public function getMinimumValues(): int { return $this->minimumValues; } @@ -133,7 +133,7 @@ public function getMinimumValues() * @param \LibDNS\Records\Types\Type * @return bool */ - public function assertDataValid(Type $value) + public function assertDataValid(Type $value): bool { return (($this->type & Types::ANYTHING) && $value instanceof Anything) || (($this->type & Types::BITMAP) && $value instanceof BitMap) diff --git a/src/Records/TypeDefinitions/FieldDefinitionFactory.php b/src/Records/TypeDefinitions/FieldDefinitionFactory.php index df28529..afd2b1e 100644 --- a/src/Records/TypeDefinitions/FieldDefinitionFactory.php +++ b/src/Records/TypeDefinitions/FieldDefinitionFactory.php @@ -1,4 +1,4 @@ - */ -class TypeDefinition implements \Iterator, \Countable +class TypeDefinition implements \IteratorAggregate, \Countable { /** * @var FieldDefinitionFactory Creates FieldDefinition objects @@ -52,11 +52,6 @@ class TypeDefinition implements \Iterator, \Countable */ private $toStringFunction; - /** - * @var bool Whether the iteration pointer indicates a valid item - */ - private $pointerValid = true; - /** * Constructor * @@ -69,7 +64,7 @@ public function __construct(FieldDefinitionFactory $fieldDefFactory, array $defi $this->fieldDefFactory = $fieldDefFactory; if (isset($definition['__toString'])) { - if (!is_callable($definition['__toString'])) { + if (!\is_callable($definition['__toString'])) { throw new \InvalidArgumentException('Invalid type definition: __toString() implementation is not callable'); } @@ -77,7 +72,7 @@ public function __construct(FieldDefinitionFactory $fieldDefFactory, array $defi unset($definition['__toString']); } - $this->fieldCount = count($definition); + $this->fieldCount = \count($definition); $index = 0; foreach ($definition as $name => $type) { $this->registerField($index++, $name, $type); @@ -92,9 +87,9 @@ public function __construct(FieldDefinitionFactory $fieldDefFactory, array $defi * @param int $type * @throws \InvalidArgumentException When the field definition is invalid */ - private function registerField($index, $name, $type) + private function registerField(int $index, string $name, int $type) { - if (!preg_match('/^(?P[\w\-]+)(?P\+|\*)?(?P(?<=\+)\d+)?$/', strtolower($name), $matches)) { + if (!\preg_match('/^(?P[\w\-]+)(?P\+|\*)?(?P(?<=\+)\d+)?$/', \strtolower($name), $matches)) { throw new \InvalidArgumentException('Invalid field definition ' . $name . ': Syntax error'); } @@ -129,9 +124,8 @@ private function registerField($index, $name, $type) * @return \LibDNS\Records\TypeDefinitions\FieldDefinition * @throws \OutOfBoundsException When the supplied index does not refer to a valid field */ - public function getFieldDefinition($index) + public function getFieldDefinition(int $index): FieldDefinition { - $index = (int) $index; if (isset($this->fieldDefs[$index])) { $fieldDef = $this->fieldDefs[$index]; } else if ($index >= 0 && $this->lastField->allowsMultiple()) { @@ -150,9 +144,9 @@ public function getFieldDefinition($index) * @return int * @throws \OutOfBoundsException When the supplied name does not refer to a valid field */ - public function getFieldIndexByName($name) + public function getFieldIndexByName($name): int { - $fieldName = strtolower($name); + $fieldName = \strtolower($name); if (!isset($this->fieldNameMap[$fieldName])) { throw new \OutOfBoundsException('Name ' . $name . ' does not refer to a valid field'); } @@ -181,50 +175,13 @@ public function setToStringFunction(callable $function) } /** - * Get the field indicated by the iteration pointer (Iterator interface) - * - * @return \LibDNS\Records\TypeDefinitions\FieldDefinition - */ - public function current() - { - return current($this->fieldDefs); - } - - /** - * Get the key indicated by the iteration pointer - * - * @return int - */ - public function key() - { - return key($this->fieldDefs); - } - - /** - * Increment the iteration pointer (Iterator interface) - */ - public function next() - { - $this->pointerValid = next($this->fieldDefs) !== false; - } - - /** - * Reset the iteration pointer to the beginning (Iterator interface) - */ - public function rewind() - { - reset($this->fieldDefs); - $this->pointerValid = count($this->fieldDefs) > 0; - } - - /** - * Test whether the iteration pointer indicates a valid field (Iterator interface) + * Retrieve an iterator (IteratorAggregate interface) * - * @return bool + * @return \Iterator */ - public function valid() + public function getIterator(): \Iterator { - return $this->pointerValid; + return new \ArrayIterator($this->fieldDefs); } /** @@ -232,7 +189,7 @@ public function valid() * * @return int */ - public function count() + public function count(): int { return $this->fieldCount; } diff --git a/src/Records/TypeDefinitions/TypeDefinitionFactory.php b/src/Records/TypeDefinitions/TypeDefinitionFactory.php index 0aede80..18cc00c 100644 --- a/src/Records/TypeDefinitions/TypeDefinitionFactory.php +++ b/src/Records/TypeDefinitions/TypeDefinitionFactory.php @@ -1,4 +1,4 @@ -typeDefs[$recordType])) { $definition = isset($this->definitions[$recordType]) ? $this->definitions[$recordType] : ['data' => Types::ANYTHING]; $this->typeDefs[$recordType] = $this->typeDefFactory->create($this->fieldDefFactory, $definition); @@ -263,16 +261,16 @@ public function getTypeDefinition($recordType) * @param int[]|\LibDNS\Records\TypeDefinitions\TypeDefinition $definition * @throws \InvalidArgumentException When the type definition is invalid */ - public function registerTypeDefinition($recordType, $definition) + public function registerTypeDefinition(int $recordType, $definition) { if (!($definition instanceof TypeDefinition)) { - if (!is_array($definition)) { + if (!\is_array($definition)) { throw new \InvalidArgumentException('Definition must be an array or an instance of ' . __NAMESPACE__ . '\TypeDefinition'); } - $definition = (int) $this->typeDefFactory->create($this->fieldDefFactory, $definition); + $definition = $this->typeDefFactory->create($this->fieldDefFactory, $definition); } - $this->typeDefs[(int) $recordType] = $definition; + $this->typeDefs[$recordType] = $definition; } } diff --git a/src/Records/TypeDefinitions/TypeDefinitionManagerFactory.php b/src/Records/TypeDefinitions/TypeDefinitionManagerFactory.php index d2a219b..4107de7 100644 --- a/src/Records/TypeDefinitions/TypeDefinitionManagerFactory.php +++ b/src/Records/TypeDefinitions/TypeDefinitionManagerFactory.php @@ -1,4 +1,4 @@ - 65535) { + $value = (string)$value; + + if (\strlen($value) > 65535) { throw new \UnexpectedValueException('Untyped string length must be in the range 0 - 65535'); } diff --git a/src/Records/Types/BitMap.php b/src/Records/Types/BitMap.php index 41c237e..a10c72c 100644 --- a/src/Records/Types/BitMap.php +++ b/src/Records/Types/BitMap.php @@ -1,4 +1,4 @@ -value = (string) $value; + $this->value = (string)$value; } /** @@ -44,22 +44,22 @@ public function setValue($value) * @param bool $newValue The new value * @return bool The old value */ - public function isBitSet($index, $newValue = null) + public function isBitSet(int $index, bool $newValue = null): bool { $charIndex = (int)($index / 8); $bitMask = 0b10000000 >> ($index % 8); $result = false; if (isset($this->value[$charIndex])) { - $result = (bool) (ord($this->value[$charIndex]) & $bitMask); + $result = (bool) (\ord($this->value[$charIndex]) & $bitMask); } if (isset($newValue) && $newValue != $result) { if (!isset($this->value[$charIndex])) { - $this->value = str_pad($this->value, $charIndex + 1, "\x00", STR_PAD_RIGHT); + $this->value = \str_pad($this->value, $charIndex + 1, "\x00", STR_PAD_RIGHT); } - $this->value[$charIndex] = chr((ord($this->value[$charIndex]) & ~$bitMask) | ($newValue ? $bitMask : 0)); + $this->value[$charIndex] = \chr((\ord($this->value[$charIndex]) & ~$bitMask) | ($newValue ? $bitMask : 0)); } return $result; diff --git a/src/Records/Types/Char.php b/src/Records/Types/Char.php index 577cdaa..aa0c51e 100644 --- a/src/Records/Types/Char.php +++ b/src/Records/Types/Char.php @@ -1,4 +1,4 @@ - 255) { diff --git a/src/Records/Types/CharacterString.php b/src/Records/Types/CharacterString.php index 2a2b2d7..1b55fc2 100644 --- a/src/Records/Types/CharacterString.php +++ b/src/Records/Types/CharacterString.php @@ -1,4 +1,4 @@ - 255) { + $value = (string)$value; + + if (\strlen($value) > 255) { throw new \UnexpectedValueException('Character string length must be in the range 0 - 255'); } diff --git a/src/Records/Types/DomainName.php b/src/Records/Types/DomainName.php index fba3fbe..d9b1a8f 100644 --- a/src/Records/Types/DomainName.php +++ b/src/Records/Types/DomainName.php @@ -1,4 +1,4 @@ -setLabels($value); - } else { - $this->setValue($value); - } + if (\is_array($value)) { + $this->setLabels($value); + } else { + parent::__construct($value); } } @@ -59,7 +57,7 @@ public function __construct($value = null) */ public function setValue($value) { - $this->setLabels(explode('.', $value)); + $this->setLabels(\explode('.', (string)$value)); } /** @@ -68,9 +66,9 @@ public function setValue($value) * @param bool $tldFirst Whether to return the label list ordered with the TLD label first * @return string[] */ - public function getLabels($tldFirst = false) + public function getLabels($tldFirst = false): array { - return $tldFirst ? array_reverse($this->labels) : $this->labels; + return $tldFirst ? \array_reverse($this->labels) : $this->labels; } /** @@ -89,12 +87,12 @@ public function setLabels(array $labels, $tldFirst = false) $length = $count = 0; foreach ($labels as &$label) { - $labelLength = strlen($label); + $label = \LibDNS\normalize_name($label); + $labelLength = \strlen($label); if ($labelLength > 63) { throw new \InvalidArgumentException('Label list is not a valid domain name: Label ' . $label . ' length exceeds 63 byte limit'); } $length += $labelLength + 1; - $label = strtolower($label); $count++; } @@ -107,7 +105,7 @@ public function setLabels(array $labels, $tldFirst = false) throw new \InvalidArgumentException('Label list is not a valid domain name: Total length exceeds 255 byte limit'); } - $this->labels = $tldFirst ? array_reverse($labels) : $labels; - $this->value = implode('.', $this->labels); + $this->labels = $tldFirst ? \array_reverse($labels) : $labels; + $this->value = \implode('.', $this->labels); } } diff --git a/src/Records/Types/IPv4Address.php b/src/Records/Types/IPv4Address.php index 88c2c67..66657f4 100644 --- a/src/Records/Types/IPv4Address.php +++ b/src/Records/Types/IPv4Address.php @@ -1,4 +1,4 @@ -setOctets($value); - } else { - $this->setValue($value); - } + if (\is_array($value)) { + $this->setOctets($value); + } else { + parent::__construct($value); } } @@ -57,7 +55,7 @@ public function __construct($value = null) */ public function setValue($value) { - $this->setOctets(explode('.', $value)); + $this->setOctets(\explode('.', (string)$value)); } /** @@ -65,7 +63,7 @@ public function setValue($value) * * @return int[] */ - public function getOctets() + public function getOctets(): array { return $this->octets; } @@ -78,19 +76,19 @@ public function getOctets() */ public function setOctets(array $octets) { - if (count($octets) !== 4) { + if (\count($octets) !== 4) { throw new \UnexpectedValueException('Octet list is not a valid IPv4 address: invalid octet count'); } foreach ($octets as &$octet) { - if (strspn((string)$octet, "0123456789") !== strlen($octet) || $octet < 0x00 || $octet > 0xff) { + if (\strspn((string)$octet, "0123456789") !== \strlen((string)$octet) || $octet < 0x00 || $octet > 0xff) { throw new \UnexpectedValueException('Octet list is not a valid IPv4 address: invalid octet value ' . $octet); } $octet = (int) $octet; } - $this->octets = array_values($octets); - $this->value = implode('.', $this->octets); + $this->octets = \array_values($octets); + $this->value = \implode('.', $this->octets); } } diff --git a/src/Records/Types/IPv6Address.php b/src/Records/Types/IPv6Address.php index f6145ed..e2ca5af 100644 --- a/src/Records/Types/IPv6Address.php +++ b/src/Records/Types/IPv6Address.php @@ -1,4 +1,4 @@ -setShorts($value); - } else { - $this->setValue($value); - } + if (\is_array($value)) { + $this->setShorts($value); + } else { + parent::__construct($value); } } @@ -108,20 +106,20 @@ public function __construct($value = null) */ public function setValue($value) { - $shorts = explode(':', (string) $value); + $shorts = \explode(':', (string)$value); - $count = count($shorts); + $count = \count($shorts); if ($count < 3 || $count > 8) { throw new \UnexpectedValueException('Value is not a valid IPv6 address: invalid short count'); } else if ($shorts[0] === '' && $shorts[1] === '') { - $shorts = array_pad($shorts, -8, '0'); + $shorts = \array_pad($shorts, -8, '0'); } else if ($shorts[$count - 2] === '' && $shorts[$count - 1] === '') { - $shorts = array_pad($shorts, 8, '0'); - } else if (false !== $pos = array_search('', $shorts, true)) { - array_splice($shorts, $pos, 1, array_fill(0, 8 - ($count - 1), '0')); + $shorts = \array_pad($shorts, 8, '0'); + } else if (false !== $pos = \array_search('', $shorts, true)) { + \array_splice($shorts, $pos, 1, \array_fill(0, 8 - ($count - 1), '0')); } - $this->setShorts(array_map('hexdec', $shorts)); + $this->setShorts(\array_map('hexdec', $shorts)); } /** @@ -129,7 +127,7 @@ public function setValue($value) * * @return int[] */ - public function getShorts() + public function getShorts(): array { return $this->shorts; } @@ -142,19 +140,19 @@ public function getShorts() */ public function setShorts(array $shorts) { - if (count($shorts) !== 8) { + if (\count($shorts) !== 8) { throw new \UnexpectedValueException('Short list is not a valid IPv6 address: invalid short count'); } foreach ($shorts as &$short) { - if (strspn((string)$short, "0123456789") !== strlen($short) || $short < 0x0000 || $short > 0xffff) { + if (\strspn((string)$short, "0123456789") !== \strlen($short) || $short < 0x0000 || $short > 0xffff) { throw new \UnexpectedValueException('Short list is not a valid IPv6 address: invalid short value ' . $short); } $short = (int) $short; } - $this->shorts = array_values($shorts); + $this->shorts = \array_values($shorts); $this->value = $this->createCompressedString($this->shorts); } } diff --git a/src/Records/Types/Long.php b/src/Records/Types/Long.php index 64bdc05..5ba9361 100644 --- a/src/Records/Types/Long.php +++ b/src/Records/Types/Long.php @@ -1,4 +1,4 @@ - 4294967296) { + } else if ($value > 0xffffffff) { throw new \OverflowException('Long integer value must be in the range 0 - 4294967296'); } diff --git a/src/Records/Types/Short.php b/src/Records/Types/Short.php index 2fb1f99..27a235a 100644 --- a/src/Records/Types/Short.php +++ b/src/Records/Types/Short.php @@ -1,4 +1,4 @@ - 65535) { + } else if ($value > 0xffff) { throw new \OverflowException('Short integer value must be in the range 0 - 65535'); } diff --git a/src/Records/Types/Type.php b/src/Records/Types/Type.php index 736d299..78e56e8 100644 --- a/src/Records/Types/Type.php +++ b/src/Records/Types/Type.php @@ -1,4 +1,4 @@ -setValue($value); @@ -41,11 +41,11 @@ public function __construct($value = null) } /** - * Magic method for type coersion to string + * Magic method for type coercion to string * * @return string */ - public function __toString() + public function __toString(): string { return (string) $this->value; } @@ -63,7 +63,7 @@ public function getValue() /** * Set the internal value * - * @param mixed $value The new value + * @param string $value The new value * @throws \RuntimeException When the supplied value is invalid */ abstract public function setValue($value); diff --git a/src/Records/Types/TypeBuilder.php b/src/Records/Types/TypeBuilder.php index 12c7d21..f5a090f 100644 --- a/src/Records/Types/TypeBuilder.php +++ b/src/Records/Types/TypeBuilder.php @@ -1,4 +1,4 @@ - 'createAnything', diff --git a/src/Records/Types/TypeFactory.php b/src/Records/Types/TypeFactory.php index 5685d9d..6f42359 100644 --- a/src/Records/Types/TypeFactory.php +++ b/src/Records/Types/TypeFactory.php @@ -1,4 +1,4 @@ - */ -class Types extends Enumeration +final class Types extends Enumeration { const ANYTHING = 0b000000001; const BITMAP = 0b000000010; diff --git a/src/functions.php b/src/functions.php new file mode 100644 index 0000000..cdb5ab2 --- /dev/null +++ b/src/functions.php @@ -0,0 +1,26 @@ +