From 3c8412b4a7d3f5528a2e51d82b4bdee5f3126425 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Thu, 22 Jun 2017 20:08:27 +0200 Subject: [PATCH] Remove TYPE_ prefix for record constants --- README.md | 8 ++-- lib/DefaultResolver.php | 35 +++++++-------- lib/HostLoader.php | 10 ++--- lib/Record.php | 94 ++++++++++++++++++++-------------------- lib/Resolver.php | 2 +- test/IntegrationTest.php | 4 +- 6 files changed, 75 insertions(+), 78 deletions(-) diff --git a/README.md b/README.md index 46b9950..ef73e4a 100644 --- a/README.md +++ b/README.md @@ -22,11 +22,11 @@ require __DIR__ . '/vendor/autoload.php'; use Amp\Loop; Loop::run(function () { - $githubIpv4 = (yield Amp\Dns\resolve("github.com", $options = ["types" => Amp\Dns\Record::TYPE_A])); + $githubIpv4 = (yield Amp\Dns\resolve("github.com", $options = ["types" => Amp\Dns\Record::A])); var_dump($githubIpv4); - $googleIpv4 = Amp\Dns\resolve("google.com", $options = ["types" => Amp\Dns\Record::TYPE_A]); - $googleIpv6 = Amp\Dns\resolve("google.com", $options = ["types" => Amp\Dns\Record::TYPE_AAAA]); + $googleIpv4 = Amp\Dns\resolve("google.com", $options = ["types" => Amp\Dns\Record::A]); + $googleIpv6 = Amp\Dns\resolve("google.com", $options = ["types" => Amp\Dns\Record::AAAA]); $firstGoogleResult = (yield Amp\Promise\first([$googleIpv4, $googleIpv6])); var_dump($firstGoogleResult); @@ -34,7 +34,7 @@ Loop::run(function () { $combinedGoogleResult = (yield Amp\Dns\resolve("google.com")); var_dump($combinedGoogleResult); - $googleMx = (yield Amp\Dns\query("google.com", Amp\Dns\Record::TYPE_MX)); + $googleMx = (yield Amp\Dns\query("google.com", Amp\Dns\Record::MX)); var_dump($googleMx); }); ``` diff --git a/lib/DefaultResolver.php b/lib/DefaultResolver.php index 8066cb4..972df33 100644 --- a/lib/DefaultResolver.php +++ b/lib/DefaultResolver.php @@ -76,17 +76,16 @@ class DefaultResolver implements Resolver { } /** @inheritdoc */ - public function resolve(string $name, array $options = []): Promise { + public function resolve(string $name): Promise { if (!$inAddr = @\inet_pton($name)) { try { $name = normalizeDnsName($name); - $types = empty($options["types"]) ? [Record::TYPE_A, Record::TYPE_AAAA] : (array) $options["types"]; - return call(function () use ($name, $types, $options) { - $result = yield from $this->recurseWithHosts($name, $types, $options); + return call(function () use ($name) { + $result = yield from $this->recurseWithHosts($name, [Record::A, Record::AAAA], []); return array_map(function ($record) { return new Record($record[0], $record[1], $record[2]); - }, $this->flattenResult($result, $types)); + }, $this->flattenResult($result, [Record::A, Record::AAAA])); }); } catch (InvalidNameError $e) { return new Failure(new ResolutionException("Cannot resolve invalid host name ({$name})", 0, $e)); @@ -94,12 +93,10 @@ class DefaultResolver implements Resolver { } // It's already a valid IP, don't resolve, immediately return - return new Success([new Record($name, isset($inAddr[4]) ? Record::TYPE_AAAA : Record::TYPE_A, $ttl = null)]); + return new Success([new Record($name, isset($inAddr[4]) ? Record::AAAA : Record::A, $ttl = null)]); } - /** - * {@inheritdoc} - */ + /** @inheritdoc */ public function query(string $name, $type, array $options = []): Promise { $types = (array) $type; @@ -133,7 +130,7 @@ class DefaultResolver implements Resolver { return $promise; } - // flatten $result while preserving order according to $types (append unspecified types for e.g. Record::TYPE_ALL queries) + // flatten $result while preserving order according to $types (append unspecified types for e.g. Record::ALL queries) private function flattenResult(array $result, array $types) { $retval = []; foreach ($types as $type) { @@ -155,11 +152,11 @@ class DefaultResolver implements Resolver { $hosts = $config->getKnownHosts(); } $result = []; - if (\in_array(Record::TYPE_A, $types) && isset($hosts[Record::TYPE_A][$name])) { - $result[Record::TYPE_A] = [[$hosts[Record::TYPE_A][$name], Record::TYPE_A, $ttl = null]]; + if (\in_array(Record::A, $types) && isset($hosts[Record::A][$name])) { + $result[Record::A] = [[$hosts[Record::A][$name], Record::A, $ttl = null]]; } - if (\in_array(Record::TYPE_AAAA, $types) && isset($hosts[Record::TYPE_AAAA][$name])) { - $result[Record::TYPE_AAAA] = [[$hosts[Record::TYPE_AAAA][$name], Record::TYPE_AAAA, $ttl = null]]; + if (\in_array(Record::AAAA, $types) && isset($hosts[Record::AAAA][$name])) { + $result[Record::AAAA] = [[$hosts[Record::AAAA][$name], Record::AAAA, $ttl = null]]; } if ($result) { return $result; @@ -170,21 +167,21 @@ class DefaultResolver implements Resolver { } private function doRecurse($name, array $types, $options) { - if (\array_intersect($types, [Record::TYPE_CNAME, Record::TYPE_DNAME])) { + if (\array_intersect($types, [Record::CNAME, Record::DNAME])) { throw new ResolutionException("Cannot use recursion for CNAME and DNAME records"); } - $types = \array_merge($types, [Record::TYPE_CNAME, Record::TYPE_DNAME]); + $types = \array_merge($types, [Record::CNAME, Record::DNAME]); $lookupName = $name; for ($i = 0; $i < 30; $i++) { $result = yield from $this->doResolve($lookupName, $types, $options); - if (\count($result) > isset($result[Record::TYPE_CNAME]) + isset($result[Record::TYPE_DNAME])) { - unset($result[Record::TYPE_CNAME], $result[Record::TYPE_DNAME]); + if (\count($result) > isset($result[Record::CNAME]) + isset($result[Record::DNAME])) { + unset($result[Record::CNAME], $result[Record::DNAME]); return $result; } // @TODO check for potentially using recursion and iterate over *all* CNAME/DNAME // @FIXME check higher level for CNAME? - foreach ([Record::TYPE_CNAME, Record::TYPE_DNAME] as $type) { + foreach ([Record::CNAME, Record::DNAME] as $type) { if (isset($result[$type])) { list($lookupName) = $result[$type][0]; } diff --git a/lib/HostLoader.php b/lib/HostLoader.php index b1a23dd..483b0f2 100644 --- a/lib/HostLoader.php +++ b/lib/HostLoader.php @@ -41,9 +41,9 @@ class HostLoader { if (!($ip = @\inet_pton($parts[0]))) { continue; } elseif (isset($ip[4])) { - $key = Record::TYPE_AAAA; + $key = Record::AAAA; } else { - $key = Record::TYPE_A; + $key = Record::A; } for ($i = 1, $l = \count($parts); $i < $l; $i++) { @@ -57,13 +57,13 @@ class HostLoader { } // Windows does not include localhost in its host file. Fetch it from the system instead - if (!isset($data[Record::TYPE_A]["localhost"]) && !isset($data[Record::TYPE_AAAA]["localhost"])) { + if (!isset($data[Record::A]["localhost"]) && !isset($data[Record::AAAA]["localhost"])) { // PHP currently provides no way to **resolve** IPv6 hostnames (not even with fallback) $local = \gethostbyname("localhost"); if ($local !== "localhost") { - $data[Record::TYPE_A]["localhost"] = $local; + $data[Record::A]["localhost"] = $local; } else { - $data[Record::TYPE_AAAA]["localhost"] = "::1"; + $data[Record::AAAA]["localhost"] = "::1"; } } diff --git a/lib/Record.php b/lib/Record.php index f80592a..768fa69 100644 --- a/lib/Record.php +++ b/lib/Record.php @@ -6,54 +6,54 @@ use LibDNS\Records\ResourceQTypes; use LibDNS\Records\ResourceTypes; class Record { - const TYPE_A = ResourceTypes::A; - const TYPE_AAAA = ResourceTypes::AAAA; - const TYPE_AFSDB = ResourceTypes::AFSDB; - // const TYPE_APL = ResourceTypes::APL; - const TYPE_CAA = ResourceTypes::CAA; - const TYPE_CERT = ResourceTypes::CERT; - const TYPE_CNAME = ResourceTypes::CNAME; - const TYPE_DHCID = ResourceTypes::DHCID; - const TYPE_DLV = ResourceTypes::DLV; - const TYPE_DNAME = ResourceTypes::DNAME; - const TYPE_DNSKEY = ResourceTypes::DNSKEY; - const TYPE_DS = ResourceTypes::DS; - const TYPE_HINFO = ResourceTypes::HINFO; - // const TYPE_HIP = ResourceTypes::HIP; - // const TYPE_IPSECKEY = ResourceTypes::IPSECKEY; - const TYPE_KEY = ResourceTypes::KEY; - const TYPE_KX = ResourceTypes::KX; - const TYPE_ISDN = ResourceTypes::ISDN; - const TYPE_LOC = ResourceTypes::LOC; - const TYPE_MB = ResourceTypes::MB; - const TYPE_MD = ResourceTypes::MD; - const TYPE_MF = ResourceTypes::MF; - const TYPE_MG = ResourceTypes::MG; - const TYPE_MINFO = ResourceTypes::MINFO; - const TYPE_MR = ResourceTypes::MR; - const TYPE_MX = ResourceTypes::MX; - const TYPE_NAPTR = ResourceTypes::NAPTR; - const TYPE_NS = ResourceTypes::NS; - // const TYPE_NSEC = ResourceTypes::NSEC; - // const TYPE_NSEC3 = ResourceTypes::NSEC3; - // const TYPE_NSEC3PARAM = ResourceTypes::NSEC3PARAM; - const TYPE_NULL = ResourceTypes::NULL; - const TYPE_PTR = ResourceTypes::PTR; - const TYPE_RP = ResourceTypes::RP; - // const TYPE_RRSIG = ResourceTypes::RRSIG; - const TYPE_RT = ResourceTypes::RT; - const TYPE_SIG = ResourceTypes::SIG; - const TYPE_SOA = ResourceTypes::SOA; - const TYPE_SPF = ResourceTypes::SPF; - const TYPE_SRV = ResourceTypes::SRV; - const TYPE_TXT = ResourceTypes::TXT; - const TYPE_WKS = ResourceTypes::WKS; - const TYPE_X25 = ResourceTypes::X25; + const A = ResourceTypes::A; + const AAAA = ResourceTypes::AAAA; + const AFSDB = ResourceTypes::AFSDB; + // const APL = ResourceTypes::APL; + const CAA = ResourceTypes::CAA; + const CERT = ResourceTypes::CERT; + const CNAME = ResourceTypes::CNAME; + const DHCID = ResourceTypes::DHCID; + const DLV = ResourceTypes::DLV; + const DNAME = ResourceTypes::DNAME; + const DNSKEY = ResourceTypes::DNSKEY; + const DS = ResourceTypes::DS; + const HINFO = ResourceTypes::HINFO; + // const HIP = ResourceTypes::HIP; + // const IPSECKEY = ResourceTypes::IPSECKEY; + const KEY = ResourceTypes::KEY; + const KX = ResourceTypes::KX; + const ISDN = ResourceTypes::ISDN; + const LOC = ResourceTypes::LOC; + const MB = ResourceTypes::MB; + const MD = ResourceTypes::MD; + const MF = ResourceTypes::MF; + const MG = ResourceTypes::MG; + const MINFO = ResourceTypes::MINFO; + const MR = ResourceTypes::MR; + const MX = ResourceTypes::MX; + const NAPTR = ResourceTypes::NAPTR; + const NS = ResourceTypes::NS; + // const NSEC = ResourceTypes::NSEC; + // const NSEC3 = ResourceTypes::NSEC3; + // const NSEC3PARAM = ResourceTypes::NSEC3PARAM; + const NULL = ResourceTypes::NULL; + const PTR = ResourceTypes::PTR; + const RP = ResourceTypes::RP; + // const RRSIG = ResourceTypes::RRSIG; + const RT = ResourceTypes::RT; + const SIG = ResourceTypes::SIG; + const SOA = ResourceTypes::SOA; + const SPF = ResourceTypes::SPF; + const SRV = ResourceTypes::SRV; + const TXT = ResourceTypes::TXT; + const WKS = ResourceTypes::WKS; + const X25 = ResourceTypes::X25; - const TYPE_AXFR = ResourceQTypes::AXFR; - const TYPE_MAILB = ResourceQTypes::MAILB; - const TYPE_MAILA = ResourceQTypes::MAILA; - const TYPE_ALL = ResourceQTypes::ALL; + const AXFR = ResourceQTypes::AXFR; + const MAILB = ResourceQTypes::MAILB; + const MAILA = ResourceQTypes::MAILA; + const ALL = ResourceQTypes::ALL; private $value; private $type; diff --git a/lib/Resolver.php b/lib/Resolver.php index 2ad8001..8fe471f 100644 --- a/lib/Resolver.php +++ b/lib/Resolver.php @@ -8,7 +8,7 @@ interface Resolver { /** * @see \Amp\Dns\resolve */ - public function resolve(string $name, array $options = []): Promise; + public function resolve(string $name): Promise; /** * @see \Amp\Dns\query diff --git a/test/IntegrationTest.php b/test/IntegrationTest.php index 7985571..31cf6dc 100644 --- a/test/IntegrationTest.php +++ b/test/IntegrationTest.php @@ -48,12 +48,12 @@ class IntegrationTest extends TestCase { public function testPtrLookup() { Loop::run(function () { - $result = yield Dns\query("8.8.4.4", Record::TYPE_PTR); + $result = yield Dns\query("8.8.4.4", Record::PTR); /** @var Record $record */ $record = $result[0]; $this->assertSame("google-public-dns-b.google.com", $record->getValue()); - $this->assertSame(Record::TYPE_PTR, $record->getType()); + $this->assertSame(Record::PTR, $record->getType()); }); }