Passed
Pull Request — master (#11)
by
unknown
01:38
created

DnsHelper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 78.95%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 19
c 2
b 0
f 1
dl 0
loc 45
ccs 15
cts 19
cp 0.7895
rs 10
wmc 11

3 Methods

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