Parser::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
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 191
    public function __construct(EmailLexer $lexer)
31
    {
32 191
        $this->lexer = $lexer;
33
    }
34
35 191
    public function parse(string $str): Result
36
    {
37 191
        $this->lexer->setInput($str);
38
39 191
        if ($this->lexer->hasInvalidTokens()) {
40 4
            return new InvalidEmail(new ExpectingATEXT("Invalid tokens found"), $this->lexer->current->value);
41
        }
42
43 187
        $preParsingResult = $this->preLeftParsing();
44 187
        if ($preParsingResult->isInvalid()) {
45 1
            return $preParsingResult;
46
        }
47
48 186
        $localPartResult = $this->parseLeftFromAt();
49
50 186
        if ($localPartResult->isInvalid()) {
51 38
            return $localPartResult;
52
        }
53
54 148
        $domainPartResult = $this->parseRightFromAt();
55
56 148
        if ($domainPartResult->isInvalid()) {
57 84
            return $domainPartResult;
58
        }
59
60 64
        return new ValidEmail();
61
    }
62
63
    /**
64
     * @return Warning\Warning[]
65
     */
66 187
    public function getWarnings(): array
67
    {
68 187
        return $this->warnings;
69
    }
70
71 187
    protected function hasAtToken(): bool
72
    {
73 187
        $this->lexer->moveNext();
74 187
        $this->lexer->moveNext();
75
76 187
        return !$this->lexer->current->isA(EmailLexer::S_AT);
0 ignored issues
show
Bug introduced by
Egulias\EmailValidator\EmailLexer::S_AT of type integer is incompatible with the type Doctrine\Common\Lexer\T expected by parameter $types of Doctrine\Common\Lexer\Token::isA(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        return !$this->lexer->current->isA(/** @scrutinizer ignore-type */ EmailLexer::S_AT);
Loading history...
77
    }
78
}
79