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

DnsHelper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 78.95%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkMx() 0 12 4
A checkForEmail() 0 6 3
A checkA() 0 11 4
1
<?php
2
3
namespace Yiisoft\NetworkUtilities;
4
5
class DnsHelper
6
{
7
    /**
8
     * @param string $hostname hostname without dot at end
9
     */
10 2
    public static function checkMx(string $hostname): bool
11
    {
12 2
        $hostname .= '.';
13
        try {
14 2
            if (!@dns_check_record($hostname, 'MX')) {
15 2
                return false;
16
            }
17 2
            $result = @dns_get_record($hostname, DNS_MX);
18 2
            return $result !== false && count($result) > 0;
19
        } catch (\Throwable $t) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
20
        }
21
        return false;
22
    }
23
24 2
    public static function checkA(string $hostname): bool
25
    {
26
        try {
27 2
            if (!@dns_check_record($hostname, 'A')) {
28 2
                return false;
29
            }
30 1
            $result = @dns_get_record($hostname, DNS_A);
31 1
            return $result !== false && count($result) > 0;
32
        } catch (\Throwable $t) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
33
        }
34
        return false;
35
    }
36
37 1
    public static function checkForEmail(string $hostnameOrEmail): bool
38
    {
39 1
        if (strpos($hostnameOrEmail, '@') !== false) {
40 1
            [$void, $hostnameOrEmail] = explode('@', $hostnameOrEmail, 2);
41
        }
42 1
        return self::checkMx($hostnameOrEmail) || self::checkA($hostnameOrEmail);
43
    }
44
}