Completed
Pull Request — 3.x (#290)
by Eduardo Gulias
05:36 queued 03:41
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 Warning\Warning[]
14
     */
15
    protected $warnings = [];
16
17
    /**
18
     * @var EmailLexer
19
     */
20
    protected $lexer;
21
22
    /**
23
     * id-left "@" id-right
24
     */
25
    abstract protected function parseRightFromAt() : Result;
26
    abstract protected function parseLeftFromAt() : Result;
27
    abstract protected function preLeftParsing() : Result;
28
29 190
    public function parse(string $str) : Result
30
    {
31 190
        $this->lexer->setInput($str);
32
33 190
        if ($this->lexer->hasInvalidTokens()) {
34 4
            return new InvalidEmail(new ExpectingATEXT("Invalid tokens found"), $this->lexer->token["value"]);
35
        }
36
37 186
        $preParsingResult = $this->preLeftParsing();
38 186
        if ($preParsingResult->isInvalid()) {
39 1
            return $preParsingResult;
40
        }
41
42 185
        $localPartResult = $this->parseLeftFromAt();
43
44 185
        if ($localPartResult->isInvalid()) {
45 38
            return $localPartResult;
46
        }
47
48 147
        $domainPartResult = $this->parseRightFromAt(); 
49
50 147
        if ($domainPartResult->isInvalid()) {
51 84
            return $domainPartResult;
52
        }
53
54 63
        return new ValidEmail();
55
    }
56
57
    /**
58
     * @return Warning\Warning[]
59
     */
60 186
    public function getWarnings() : array
61
    {
62 186
        return $this->warnings;
63
    }
64
65 186
    protected function hasAtToken() : bool
66
    {
67 186
        $this->lexer->moveNext();
68 186
        $this->lexer->moveNext();
69
70 186
        return $this->lexer->token['type'] !== EmailLexer::S_AT;
71
    }
72
}
73