Passed
Push — master ( 886a7d...b4be93 )
by Alexander
02:14 queued 20s
created

DnsHelper::acceptsEmails()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 3
rs 10
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 existsMx(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
     * @link https://bugs.php.net/bug.php?id=78008
28
     */
29 2
    public static function existsA(string $hostname): bool
30
    {
31
        try {
32 2
            if (!@dns_check_record($hostname, 'A')) {
33 2
                return false;
34
            }
35 1
            $result = @dns_get_record($hostname, DNS_A);
36 1
            return $result !== false && count($result) > 0;
37
        } catch (\Throwable $t) {
38
            // eg. name servers are not found https://github.com/yiisoft/yii2/issues/17602
39
        }
40
        return false;
41
    }
42
43
    /**
44
     * @link https://tools.ietf.org/html/rfc5321#section-5
45
     */
46 1
    public static function acceptsEmails(string $hostnameOrEmail): bool
47
    {
48 1
        if (strpos($hostnameOrEmail, '@') !== false) {
49 1
            [$void, $hostnameOrEmail] = explode('@', $hostnameOrEmail, 2);
50
        }
51 1
        return self::existsMx($hostnameOrEmail) || self::existsA($hostnameOrEmail);
52
    }
53
}
54