Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/PHP-8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ jobs:

- name: Test Coverage
run: vendor/bin/phpunit --coverage-text

- name: PHP Coding Standards
run: vendor/bin/php-cs-fixer check
1 change: 1 addition & 0 deletions CHANGELOG-4.x.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CHANGELOG for 4.x
=================
## 4.4.0
* [Pull #121](https://github.com/Badcow/DNS/pull/121/) Fix parsing of empty names with multiple $ORIGINS. (Thank you, [@not-implemented](https://github.com/not-implemented))
* [Pull #128](https://github.com/Badcow/DNS/pull/128/) Fix handling empty OPT. (Thank you, [@yeganemehr](https://github.com/yeganemehr))
* [Pull #130](https://github.com/Badcow/DNS/pull/130/) Fix encoding TXT records as length-prefixed character-strings. (Thank you, [@yeganemehr](https://github.com/yeganemehr))
* [Pull #131](https://github.com/Badcow/DNS/pull/131/) Fix encoding ResourceRecord with underscore on their name. (Thank you, [@yeganemehr](https://github.com/yeganemehr))
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,10 @@ $zone = Badcow\DNS\Parser\Parser::parse('example.com.', $file); //Badcow Zone Ob
Simple as that.

More examples can be found in the [The Docs](docs/Parser)

## Contributing

When making new contributions, please ensure you write corresponding tests. Also be sure to run the code standards compliance tools. The GitHub workflow will fail if you haven't fixed code standard issues.
```
$ vendor/bin/php-cs-fixer check
```
12 changes: 6 additions & 6 deletions lib/AlignedBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,31 +179,31 @@ public function compareResourceRecords(ResourceRecord $a, ResourceRecord $b): in
$a_rdata = (null === $a->getRdata()) ? '' : $a->getRdata()->toText();
$b_rdata = (null === $b->getRdata()) ? '' : $b->getRdata()->toText();

//If the types are the same, do a simple alphabetical comparison.
// If the types are the same, do a simple alphabetical comparison.
if ($a->getType() === $b->getType()) {
return strcmp($a->getName().$a_rdata, $b->getName().$b_rdata);
}

//Find the precedence (if any) for the two types.
// Find the precedence (if any) for the two types.
$_a = array_search($a->getType(), $this->order);
$_b = array_search($b->getType(), $this->order);

//If neither types have defined precedence.
// If neither types have defined precedence.
if (!is_int($_a) && !is_int($_b)) {
return strcmp($a->getType() ?? '', $b->getType() ?? '');
}

//If both types have defined precedence.
// If both types have defined precedence.
if (is_int($_a) && is_int($_b)) {
return $_a - $_b;
}

//If only $b has defined precedence.
// If only $b has defined precedence.
if (false === $_a) {
return 1;
}

//If only $a has defined precedence.
// If only $a has defined precedence.
return -1;
}

Expand Down
2 changes: 0 additions & 2 deletions lib/AlignedRdataFormatters.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ public static function LOC(LOC $loc, int $padding): string

/**
* Returns a padded line with comment.
*
* @param string $comment
*/
public static function makeLine(string $text, ?string $comment, int $longestVarLength, int $padding): string
{
Expand Down
2 changes: 1 addition & 1 deletion lib/Classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Classes
];

/**
* @const string[]
* @var string[]
*/
public const IDS_CLASSES = [
1 => 'IN',
Expand Down
6 changes: 0 additions & 6 deletions lib/Edns/Option/UnknownOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,11 @@ public function setName(string $name): void
$this->name = $name;
}

/**
* @return string
*/
public function getData(): ?string
{
return $this->data;
}

/**
* @param string $data
*/
public function setData(?string $data): void
{
$this->data = $data;
Expand Down
14 changes: 7 additions & 7 deletions lib/Parser/Normaliser.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Normaliser
*/
public function __construct(string $zone, int $commentOptions = Comments::NONE)
{
//Remove Windows line feeds and tabs
// Remove Windows line feeds and tabs
$zone = str_replace([Tokens::CARRIAGE_RETURN, Tokens::TAB], ['', Tokens::SPACE], $zone);

$this->string = new StringIterator($zone);
Expand Down Expand Up @@ -123,7 +123,7 @@ private function handleTxt(): void
throw new ParseException('Unbalanced double quotation marks. End of file reached.');
}

//If escape character
// If escape character
if ($this->string->is(Tokens::BACKSLASH)) {
$this->append();
}
Expand Down Expand Up @@ -197,9 +197,9 @@ private function removeWhitespace(): void
*/
private function append(): void
{
if (($this->string->is(Tokens::LINE_FEED) || !$this->string->valid()) &&
$this->commentOptions &&
('' !== $this->comment || '' !== $this->multilineComments)) {
if (($this->string->is(Tokens::LINE_FEED) || !$this->string->valid())
&& $this->commentOptions
&& ('' !== $this->comment || '' !== $this->multilineComments)) {
$this->appendComment();
}

Expand All @@ -211,8 +211,8 @@ private function appendComment(): void
{
$zone = rtrim($this->normalisedString, Tokens::SPACE);

//If there is no Resource Record on the line
if ((Tokens::LINE_FEED === substr($zone, -1, 1) || 0 === strlen($zone))) {
// If there is no Resource Record on the line
if (Tokens::LINE_FEED === substr($zone, -1, 1) || 0 === strlen($zone)) {
if ($this->commentOptions & Comments::ORPHAN) {
$this->normalisedString = sprintf('%s;%s', $zone, trim($this->comment));
}
Expand Down
19 changes: 9 additions & 10 deletions lib/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Badcow\DNS\Rdata\Types;
use Badcow\DNS\ResourceRecord;
use Badcow\DNS\Zone;
use Exception;

class Parser
{
Expand Down Expand Up @@ -313,14 +312,14 @@ private function includeFile(ResourceRecordIterator $iterator): void

list($path, $domain) = $this->extractIncludeArguments($iterator->getRemainingAsString());

//Copy the state of the parser so as to revert back once included file has been parsed.
// Copy the state of the parser so as to revert back once included file has been parsed.
$_lastStatedDomain = $this->lastStatedDomain;
$_lastStatedClass = $this->lastStatedClass;
$_lastStatedTtl = $this->lastStatedTtl;
$_origin = $this->origin;
$_ttl = $this->ttl;

//Parse the included record.
// Parse the included record.
$this->origin = $domain ?? $_origin;
$childRecord = $this->fetcher->fetch($path);

Expand All @@ -330,7 +329,7 @@ private function includeFile(ResourceRecordIterator $iterator): void

$this->processZone($childRecord);

//Revert the parser.
// Revert the parser.
$this->lastStatedDomain = $_lastStatedDomain;
$this->lastStatedClass = $_lastStatedClass;
$this->lastStatedTtl = $_lastStatedTtl;
Expand Down Expand Up @@ -379,9 +378,9 @@ private function isResourceName(ResourceRecordIterator $iterator): bool
return false;
}

$isName = $this->isTTL($iterator) ||
$this->isClass($iterator, 'DOMAIN') ||
$this->isType($iterator);
$isName = $this->isTTL($iterator)
|| $this->isClass($iterator, 'DOMAIN')
|| $this->isType($iterator);
$iterator->prev();

if (!$isName) {
Expand Down Expand Up @@ -483,7 +482,7 @@ private function extractComment(string $rr): array
$comment = null;

while ($string->valid()) {
//If a semicolon is within double quotes, it will not be treated as the beginning of a comment.
// If a semicolon is within double quotes, it will not be treated as the beginning of a comment.
$entry .= $this->extractDoubleQuotedText($string);

if ($string->is(Tokens::SEMICOLON)) {
Expand Down Expand Up @@ -513,7 +512,7 @@ private function extractDoubleQuotedText(StringIterator $string): string
$string->next();

while ($string->isNot(Tokens::DOUBLE_QUOTES)) {
//If the current char is a backslash, treat the next char as being escaped.
// If the current char is a backslash, treat the next char as being escaped.
if ($string->is(Tokens::BACKSLASH)) {
$entry .= $string->current();
$string->next();
Expand All @@ -539,7 +538,7 @@ private function extractRdata(ResourceRecordIterator $iterator): RdataInterface

try {
return Factory::textToRdataType($type, $iterator->getRemainingAsString());
} catch (Exception $exception) {
} catch (\Exception $exception) {
throw new ParseException(sprintf('Could not extract Rdata from resource record "%s".', (string) $iterator), null, $exception);
}
}
Expand Down
10 changes: 4 additions & 6 deletions lib/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
use Badcow\DNS\Rdata\DecodeException;
use Badcow\DNS\Rdata\Types;
use Badcow\DNS\Rdata\UnsupportedTypeException;
use InvalidArgumentException;
use UnexpectedValueException;

class Question
{
Expand All @@ -44,12 +42,12 @@ public function getName(): string
/**
* @param string $name
*
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
public function setName($name): void
{
if (!Validator::fullyQualifiedDomainName($name, false)) {
throw new InvalidArgumentException(sprintf('"%s" is not a fully qualified domain name.', $name));
throw new \InvalidArgumentException(sprintf('"%s" is not a fully qualified domain name.', $name));
}

$this->name = $name;
Expand Down Expand Up @@ -103,7 +101,7 @@ public function getClass(): string
public function setClassId(int $classId): void
{
if (!Validator::isUnsignedInteger($classId, 16)) {
throw new InvalidArgumentException(sprintf('Invalid class: "%s".', $classId));
throw new \InvalidArgumentException(sprintf('Invalid class: "%s".', $classId));
}

$this->classId = $classId;
Expand All @@ -120,7 +118,7 @@ public function toWire(): string
}

/**
* @throws UnexpectedValueException
* @throws \UnexpectedValueException
* @throws UnsupportedTypeException
*/
public static function fromWire(string $encoded, int &$offset = 0): Question
Expand Down
3 changes: 0 additions & 3 deletions lib/Rdata/A.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ public function setAddress(string $address): void
$this->address = $address;
}

/**
* @return string
*/
public function getAddress(): ?string
{
return $this->address;
Expand Down
6 changes: 0 additions & 6 deletions lib/Rdata/CAA.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ class CAA implements RdataInterface
*/
private $value;

/**
* @return int
*/
public function getFlag(): ?int
{
return $this->flag;
Expand All @@ -80,9 +77,6 @@ public function setFlag(int $flag): void
$this->flag = $flag;
}

/**
* @return string
*/
public function getTag(): ?string
{
return $this->tag;
Expand Down
15 changes: 7 additions & 8 deletions lib/Rdata/CERT.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Badcow\DNS\Algorithms;
use Badcow\DNS\Parser\Tokens;
use InvalidArgumentException;

/*
* {@link https://tools.ietf.org/html/rfc4398#section-2.1}.
Expand Down Expand Up @@ -79,7 +78,7 @@ public function getCertificateType(): int
/**
* @param int|string $certificateType
*
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
public function setCertificateType($certificateType): void
{
Expand Down Expand Up @@ -110,7 +109,7 @@ public function getAlgorithm(): int
/**
* @param string|int $algorithm
*
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
public function setAlgorithm($algorithm): void
{
Expand All @@ -126,7 +125,7 @@ public function setAlgorithm($algorithm): void
/**
* @param string $certificate Base64 encoded string
*
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
public function setCertificate(string $certificate): void
{
Expand Down Expand Up @@ -179,12 +178,12 @@ public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null)
}

/**
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
public static function getKeyTypeValue(string $keyTypeMnemonic): int
{
if (false === $keyTypeValue = array_search($keyTypeMnemonic, self::MNEMONICS, true)) {
throw new InvalidArgumentException(sprintf('"%s" is not a valid key type mnemonic.', $keyTypeMnemonic));
throw new \InvalidArgumentException(sprintf('"%s" is not a valid key type mnemonic.', $keyTypeMnemonic));
}

return (int) $keyTypeValue;
Expand All @@ -193,12 +192,12 @@ public static function getKeyTypeValue(string $keyTypeMnemonic): int
/**
* Get the associated mnemonic of a key type.
*
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
public static function getKeyTypeMnemonic(int $keyTypeValue): string
{
if (!array_key_exists($keyTypeValue, self::MNEMONICS)) {
throw new InvalidArgumentException(sprintf('"%d" is not a valid key type.', $keyTypeValue));
throw new \InvalidArgumentException(sprintf('"%d" is not a valid key type.', $keyTypeValue));
}

return self::MNEMONICS[$keyTypeValue];
Expand Down
3 changes: 0 additions & 3 deletions lib/Rdata/CNAME.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ public function setTarget(string $target): void
$this->target = $target;
}

/**
* @return string
*/
public function getTarget(): ?string
{
return $this->target;
Expand Down
5 changes: 2 additions & 3 deletions lib/Rdata/DS.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Badcow\DNS\Message;
use Badcow\DNS\Parser\Tokens;
use InvalidArgumentException;

class DS implements RdataInterface
{
Expand Down Expand Up @@ -105,7 +104,7 @@ public function setDigest(string $digest): void
public function calculateDigest(string $owner, DNSKEY $dnskey): void
{
if (static::DIGEST_SHA1 !== $this->digestType) {
throw new InvalidArgumentException('Can only calculate SHA-1 digests.');
throw new \InvalidArgumentException('Can only calculate SHA-1 digests.');
}

$this->digest = sha1(Message::encodeName(strtolower($owner)).$dnskey->toWire(), true);
Expand Down Expand Up @@ -134,7 +133,7 @@ public function fromText(string $text): void
$this->setAlgorithm((int) array_shift($rdata));
$this->setDigestType((int) array_shift($rdata));
if (false === $digest = hex2bin((string) array_shift($rdata))) {
throw new InvalidArgumentException(sprintf('The digest is not a valid hexadecimal string.'));
throw new \InvalidArgumentException(sprintf('The digest is not a valid hexadecimal string.'));
}
$this->setDigest($digest);
}
Expand Down
Loading
Loading