Email   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A checkHost() 0 3 3
A isValid() 0 17 6
A checkMX() 0 3 1
1
<?php
2
3
namespace Kontrolio\Rules\Core;
4
5
use Kontrolio\Rules\AbstractRule;
6
7
/**
8
 * Email validation rule.
9
 *
10
 * @package Kontrolio\Rules\Core
11
 */
12
class Email extends AbstractRule
13
{
14
    /**
15
     * Whether to check MX record.
16
     *
17
     * @var bool
18
     */
19
    private $mx;
20
21
    /**
22
     * Whether to check host.
23
     *
24
     * @var bool
25
     */
26
    private $host;
27
28
    /**
29
     * Email validation rule constructor.
30
     *
31
     * @param bool $mx
32
     * @param bool $host
33
     */
34
    public function __construct($mx = false, $host = false)
35
    {
36
        $this->mx = $mx;
37
        $this->host = $host;
38
    }
39
40
    /**
41
     * Validates input.
42
     *
43
     * @param mixed $input
44
     *
45
     * @return bool
46
     */
47
    public function isValid($input = null)
48
    {
49
        if (!preg_match('/^.+\@\S+\.\S+$/', $input)) {
0 ignored issues
show
Bug introduced by
It seems like $input can also be of type null; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        if (!preg_match('/^.+\@\S+\.\S+$/', /** @scrutinizer ignore-type */ $input)) {
Loading history...
50
            return false;
51
        }
52
53
        $host = substr($input, strrpos($input, '@') + 1);
0 ignored issues
show
Bug introduced by
It seems like $input can also be of type null; however, parameter $haystack of strrpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $host = substr($input, strrpos(/** @scrutinizer ignore-type */ $input, '@') + 1);
Loading history...
Bug introduced by
It seems like $input can also be of type null; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $host = substr(/** @scrutinizer ignore-type */ $input, strrpos($input, '@') + 1);
Loading history...
54
55
        if ($this->mx && !$this->checkMX($host)) {
56
            $this->violations[] = 'mx';
57
        }
58
59
        if ($this->host && !$this->checkHost($host)) {
60
            $this->violations[] = 'host';
61
        }
62
63
        return !$this->hasViolations();
64
    }
65
66
    /**
67
     * Checks DNS records for MX type.
68
     *
69
     * @param string $host
70
     *
71
     * @return bool
72
     */
73
    private function checkMX($host)
74
    {
75
        return checkdnsrr($host, 'MX');
76
    }
77
78
    /**
79
     * Check if one of MX, A or AAAA DNS RR exists.
80
     *
81
     * @param string $host
82
     *
83
     * @return bool
84
     */
85
    private function checkHost($host)
86
    {
87
        return $this->checkMX($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA'));
88
    }
89
}
90