Passed
Pull Request — 3.x (#290)
by Eduardo Gulias
01:49
created

Parser::parse()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 14
cts 14
cp 1
rs 9.1768
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5
1
<?php
2
3
namespace Egulias\EmailValidator;
4
5
use Egulias\EmailValidator\Result\Result;
6
use Egulias\EmailValidator\Result\ValidEmail;
7
use Egulias\EmailValidator\Result\InvalidEmail;
8
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
9
10
abstract class Parser
11
{
12
    /**
13
     * @var array
14
     */
15
16
    protected $warnings = [];
17
18
    /**
19
     * @var EmailLexer
20
     */
21
    protected $lexer;
22
23
    /**
24
     * id-left "@" id-right
25
     */
26
    abstract protected function parseRightFromAt() : Result;
27
    abstract protected function parseLeftFromAt() : Result;
28
    abstract protected function preLeftParsing() : Result;
29
30 190
    public function parse(string $str) : Result
31
    {
32 190
        $this->lexer->setInput($str);
33
34 190
        if ($this->lexer->hasInvalidTokens()) {
35 4
            return new InvalidEmail(new ExpectingATEXT("Invalid tokens found"), $this->lexer->token["value"]);
36
        }
37
38 186
        $preParsingResult = $this->preLeftParsing();
39 186
        if ($preParsingResult->isInvalid()) {
40 1
            return $preParsingResult;
41
        }
42
43 185
        $localPartResult = $this->parseLeftFromAt();
44
45 185
        if ($localPartResult->isInvalid()) {
46 38
            return $localPartResult;
47
        }
48
49 147
        $domainPartResult = $this->parseRightFromAt(); 
50
51 147
        if ($domainPartResult->isInvalid()) {
52 84
            return $domainPartResult;
53
        }
54
55 63
        return new ValidEmail();
56
    }
57
58
    /**
59
     * @return Warning\Warning[]
60
     */
61 186
    public function getWarnings() : array
62
    {
63 186
        return $this->warnings;
64
    }
65
66 186
    protected function hasAtToken() : bool
67
    {
68 186
        $this->lexer->moveNext();
69 186
        $this->lexer->moveNext();
70 186
        if ($this->lexer->token['type'] === EmailLexer::S_AT) {
71 1
            return false;
72
        }
73
74 185
        return true;
75
    }
76
}