Completed
Pull Request — 3.x (#290)
by Eduardo Gulias
05:36 queued 03:41
created

Parser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 63
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
parseRightFromAt() 0 1 ?
parseLeftFromAt() 0 1 ?
preLeftParsing() 0 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 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