PartParser::escaped()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Egulias\EmailValidator\Parser;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\Result\InvalidEmail;
7
use Egulias\EmailValidator\Result\Reason\ConsecutiveDot;
8
use Egulias\EmailValidator\Result\Result;
9
use Egulias\EmailValidator\Result\ValidEmail;
10
use Egulias\EmailValidator\Warning\Warning;
11
12
abstract class PartParser
13
{
14
    /**
15
     * @var Warning[]
16
     */
17
    protected $warnings = [];
18
19
    /**
20
     * @var EmailLexer
21
     */
22
    protected $lexer;
23 186
24
    public function __construct(EmailLexer $lexer)
25 186
    {
26
        $this->lexer = $lexer;
27
    }
28
29
    abstract public function parse(): Result;
30
31
    /**
32
     * @return Warning[]
33 186
     */
34
    public function getWarnings()
35 186
    {
36
        return $this->warnings;
37
    }
38 114
39
    protected function parseFWS(): Result
40 114
    {
41 114
        $foldingWS = new FoldingWhiteSpace($this->lexer);
42 114
        $resultFWS = $foldingWS->parse();
43 114
        $this->warnings = array_merge($this->warnings, $foldingWS->getWarnings());
44
        return $resultFWS;
45
    }
46 132
47
    protected function checkConsecutiveDots(): Result
48 132
    {
49 2
        if ($this->lexer->current->isA(EmailLexer::S_DOT) && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
0 ignored issues
show
Bug introduced by
Egulias\EmailValidator\EmailLexer::S_DOT 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

49
        if ($this->lexer->current->isA(/** @scrutinizer ignore-type */ EmailLexer::S_DOT) && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
Loading history...
50
            return new InvalidEmail(new ConsecutiveDot(), $this->lexer->current->value);
51
        }
52 132
53
        return new ValidEmail();
54
    }
55 174
56
    protected function escaped(): bool
57 174
    {
58
        $previous = $this->lexer->getPrevious();
59 174
60 174
        return $previous->isA(EmailLexer::S_BACKSLASH)
0 ignored issues
show
Bug introduced by
Egulias\EmailValidator\EmailLexer::S_BACKSLASH 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

60
        return $previous->isA(/** @scrutinizer ignore-type */ EmailLexer::S_BACKSLASH)
Loading history...
61 174
            && !$this->lexer->current->isA(EmailLexer::GENERIC);
62
    }
63
}
64