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

Parser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
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 67
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 27 5
A getWarnings() 0 4 1
parseRightFromAt() 0 1 ?
parseLeftFromAt() 0 1 ?
preLeftParsing() 0 1 ?
A hasAtToken() 0 10 2
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
}