Passed
Push — message-id-validator ( 1cb5b2...07464f )
by Eduardo Gulias
02:11
created

Parser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 53
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
parseRightFromAt() 0 1 ?
parseLeftFromAt() 0 1 ?
preRightParsing() 0 1 ?
A parse() 0 27 5
A getWarnings() 0 4 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 array
14
     */
15
16
    protected $warnings = [];
17
18
    /**
19
     * @var EmailLexer
20
     */
21
    protected $lexer;
22
23
    abstract protected function parseRightFromAt() : Result;
24
    abstract protected function parseLeftFromAt() : Result;
25
    abstract protected function preRightParsing() : Result;
26
27 185
    public function parse(string $str) : Result
28
    {
29 185
        $this->lexer->setInput($str);
30
31 185
        if ($this->lexer->hasInvalidTokens()) {
32 4
            return new InvalidEmail(new ExpectingATEXT("Invalid tokens found"), $this->lexer->token["value"]);
33
        }
34
35 181
        $preParsingResult = $this->preRightParsing();
36 181
        if ($preParsingResult->isInvalid()) {
37 1
            return $preParsingResult;
38
        }
39
40 180
        $localPartResult = $this->parseRightFromAt();
41
42 180
        if ($localPartResult->isInvalid()) {
43 36
            return $localPartResult;
44
        }
45
46 144
        $domainPartResult = $this->parseLeftFromAt();
47
48 144
        if ($domainPartResult->isInvalid()) {
49 81
            return $domainPartResult;
50
        }
51
52 63
        return new ValidEmail();
53
    }
54
55
    /**
56
     * @return Warning\Warning[]
57
     */
58 181
    public function getWarnings() : array
59
    {
60 181
        return $this->warnings;
61
    }
62
}