Passed
Push — 3.x ( 451b43...8e526a )
by Eduardo Gulias
02:05
created

Parser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 69
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
parseRightFromAt() 0 1 ?
parseLeftFromAt() 0 1 ?
preLeftParsing() 0 1 ?
A __construct() 0 4 1
A parse() 0 27 5
A getWarnings() 0 4 1
A hasAtToken() 0 7 1
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
30 190
    public function __construct(EmailLexer $lexer)
31
    {
32 190
        $this->lexer = $lexer;   
33 190
    }
34
35 190
    public function parse(string $str) : Result
36
    {
37 190
        $this->lexer->setInput($str);
38
39 190
        if ($this->lexer->hasInvalidTokens()) {
40 4
            return new InvalidEmail(new ExpectingATEXT("Invalid tokens found"), $this->lexer->token["value"]);
41
        }
42
43 186
        $preParsingResult = $this->preLeftParsing();
44 186
        if ($preParsingResult->isInvalid()) {
45 1
            return $preParsingResult;
46
        }
47
48 185
        $localPartResult = $this->parseLeftFromAt();
49
50 185
        if ($localPartResult->isInvalid()) {
51 38
            return $localPartResult;
52
        }
53
54 147
        $domainPartResult = $this->parseRightFromAt(); 
55
56 147
        if ($domainPartResult->isInvalid()) {
57 84
            return $domainPartResult;
58
        }
59
60 63
        return new ValidEmail();
61
    }
62
63
    /**
64
     * @return Warning\Warning[]
65
     */
66 186
    public function getWarnings() : array
67
    {
68 186
        return $this->warnings;
69
    }
70
71 186
    protected function hasAtToken() : bool
72
    {
73 186
        $this->lexer->moveNext();
74 186
        $this->lexer->moveNext();
75
76 186
        return $this->lexer->token['type'] !== EmailLexer::S_AT;
77
    }
78
}
79