Passed
Push — master ( c10b23...a917e3 )
by Alexander
01:41
created

DnsHelper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
c 2
b 0
f 0
dl 0
loc 54
ccs 15
cts 21
cp 0.7143
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A existsMx() 0 14 4
A existsA() 0 13 4
A acceptsEmails() 0 6 3
1
<?php
2
3
namespace Yiisoft\NetworkUtilities;
4
5
class DnsHelper
6
{
7
    /**
8
     * @param string $hostname hostname without dot at end
9
     * @return bool
10
     * @link https://bugs.php.net/bug.php?id=78008
11
     */
12 2
    public static function existsMx(string $hostname): bool
13
    {
14 2
        $hostname = rtrim($hostname, '.') . '.';
15
        try {
16 2
            if (!@dns_check_record($hostname, 'MX')) {
17 2
                return false;
18
            }
19 2
            $result = @dns_get_record($hostname, DNS_MX);
20 2
            return $result !== false && count($result) > 0;
21
        } catch (\Throwable $t) {
22
            assert($t);
23
            // eg. name servers are not found https://github.com/yiisoft/yii2/issues/17602
24
        }
25
        return false;
26
    }
27
28
    /**
29
     * @link https://bugs.php.net/bug.php?id=78008
30
     * @param string $hostname
31
     * @return bool
32
     */
33 2
    public static function existsA(string $hostname): bool
34
    {
35
        try {
36 2
            if (!@dns_check_record($hostname, 'A')) {
37 2
                return false;
38
            }
39 1
            $result = @dns_get_record($hostname, DNS_A);
40 1
            return $result !== false && count($result) > 0;
41
        } catch (\Throwable $t) {
42
            assert($t);
43
            // eg. name servers are not found https://github.com/yiisoft/yii2/issues/17602
44
        }
45
        return false;
46
    }
47
48
    /**
49
     * @link https://tools.ietf.org/html/rfc5321#section-5
50
     * @param string $hostnameOrEmail
51
     * @return bool
52
     */
53 1
    public static function acceptsEmails(string $hostnameOrEmail): bool
54
    {
55 1
        if (strpos($hostnameOrEmail, '@') !== false) {
56 1
            [, $hostnameOrEmail] = explode('@', $hostnameOrEmail, 2);
57
        }
58 1
        return self::existsMx($hostnameOrEmail) || self::existsA($hostnameOrEmail);
59
    }
60
}
61