Completed
Push — master ( afde3d...770123 )
by Eduardo Gulias
02:48
created

DNSCheckValidation   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 9
c 3
b 1
f 0
lcom 1
cbo 2
dl 0
loc 55
ccs 21
cts 21
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getError() 0 4 1
A getWarnings() 0 4 1
A isValid() 0 12 2
B checkDNS() 0 14 5
1
<?php
2
3
namespace Egulias\EmailValidator\Validation;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\Warning\NoDNSMXRecord;
7
use Egulias\EmailValidator\Exception\NoDNSRecord;
8
9
class DNSCheckValidation implements EmailValidation
10
{
11
    /**
12
     * @var EmailParser
13
     */
14
    private $parser;
15
16
    /**
17
     * @var array
18
     */
19
    private $warnings = [];
20
21
    /**
22
     * @var InvalidEmail
23
     */
24
    private $error;
25
26 11
    public function isValid($email, EmailLexer $emailLexer)
27
    {
28
        // use the input to check DNS if we cannot extract something similar to a domain
29 11
        $host = $email;
30
31
        // Arguable pattern to extract the domain. Not aiming to validate the domain nor the email
32 11
        if (false !== $lastAtPos = strrpos($email, '@')) {
33 11
            $host = substr($email, $lastAtPos + 1);
34 11
        }
35
36 11
        return $this->checkDNS($host);
37
    }
38
39 1
    public function getError()
40
    {
41 1
        return $this->error;
42
    }
43
44 1
    public function getWarnings()
45
    {
46 1
        return $this->warnings;
47
    }
48
49 11
    protected function checkDNS($host)
50
    {
51 11
        $Aresult = true;
52 11
        $MXresult = checkdnsrr($host, 'MX');
53
        
54 11
        if (!$MXresult) {
55 11
            $this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord();
56 11
            $Aresult = checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA');
57 11
            if (!$Aresult) {
58 3
                $this->error = new NoDNSRecord();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Egulias\EmailValida...Exception\NoDNSRecord() of type object<Egulias\EmailVali...\Exception\NoDNSRecord> is incompatible with the declared type object<Egulias\EmailVali...alidation\InvalidEmail> of property $error.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59 3
            }
60 11
        }
61 11
        return $MXresult || $Aresult;
62
    }
63
}
64