RFCValidation   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 17
c 1
b 1
f 0
dl 0
loc 47
ccs 12
cts 15
cp 0.8
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 17 3
A getError() 0 3 1
A getWarnings() 0 3 1
1
<?php
2
3
namespace Egulias\EmailValidator\Validation;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\EmailParser;
7
use Egulias\EmailValidator\Result\InvalidEmail;
8
use Egulias\EmailValidator\Result\Reason\ExceptionFound;
9
use Egulias\EmailValidator\Warning\Warning;
10
11
class RFCValidation implements EmailValidation
12
{
13
    /**
14
     * @var EmailParser|null
15
     */
16
    private $parser;
17
18
    /**
19
     * @var Warning[]
20
     */
21
    private array $warnings = [];
22
23
    /**
24
     * @var ?InvalidEmail
25
     */
26
    private $error;
27 178
28
    public function isValid(string $email, EmailLexer $emailLexer): bool
29 178
    {
30
        $this->parser = new EmailParser($emailLexer);
31 178
        try {
32 178
            $result = $this->parser->parse($email);
33 178
            $this->warnings = $this->parser->getWarnings();
34
            if ($result->isInvalid()) {
35 121
                /** @psalm-suppress PropertyTypeCoercion */
36 178
                $this->error = $result;
37
                return false;
38
            }
39
        } catch (\Exception $invalid) {
40
            $this->error = new InvalidEmail(new ExceptionFound($invalid), '');
41
            return false;
42
        }
43 57
44
        return true;
45
    }
46 37
47
    public function getError(): ?InvalidEmail
48 37
    {
49
        return $this->error;
50
    }
51 24
52
    /**
53 24
     * @return Warning[]
54
     */
55
    public function getWarnings(): array
56
    {
57
        return $this->warnings;
58
    }
59
}
60