Test Failed
Pull Request — 3.x (#339)
by
unknown
01:56
created

DoubleQuote   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
eloc 40
c 0
b 0
f 0
dl 0
loc 72
ccs 39
cts 42
cp 0.9286
rs 10
wmc 18

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkDQUOTE() 0 17 4
C parse() 0 51 14
1
<?php
2
3
namespace Egulias\EmailValidator\Parser;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\Result\ValidEmail;
7
use Egulias\EmailValidator\Result\InvalidEmail;
8
use Egulias\EmailValidator\Warning\CFWSWithFWS;
9
use Egulias\EmailValidator\Warning\QuotedString;
10
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
11
use Egulias\EmailValidator\Result\Reason\UnclosedQuotedString;
12
use Egulias\EmailValidator\Result\Result;
13
14
class DoubleQuote extends PartParser
15
{
16 23
    public function parse(): Result
17
    {
18
19 23
        $validQuotedString = $this->checkDQUOTE();
20 23
        if ($validQuotedString->isInvalid()) return $validQuotedString;
21
22 21
        $special = [
23 21
            EmailLexer::S_CR => true,
24 21
            EmailLexer::S_HTAB => true,
25 21
            EmailLexer::S_LF => true
26 21
        ];
27
28 21
        $invalid = [
29 21
            EmailLexer::C_NUL => true,
30 21
            EmailLexer::S_HTAB => true,
31 21
            EmailLexer::S_CR => true,
32 21
            EmailLexer::S_LF => true
33 21
        ];
34
35 21
        $setSpecialsWarning = true;
36
37 21
        $this->lexer->moveNext();
38
39 21
        while (!$this->lexer->token->isA(EmailLexer::S_DQUOTE) && null !== $this->lexer->token->type) {
0 ignored issues
show
Bug introduced by
Egulias\EmailValidator\EmailLexer::S_DQUOTE 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

39
        while (!$this->lexer->token->isA(/** @scrutinizer ignore-type */ EmailLexer::S_DQUOTE) && null !== $this->lexer->token->type) {
Loading history...
40 19
            if (isset($special[$this->lexer->token->type]) && $setSpecialsWarning) {
41 2
                $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
42 2
                $setSpecialsWarning = false;
43
            }
44 19
            if ($this->lexer->token->isA(EmailLexer::S_BACKSLASH) && $this->lexer->isNextToken(EmailLexer::S_DQUOTE)) {
0 ignored issues
show
Bug introduced by
Egulias\EmailValidator\EmailLexer::S_DQUOTE of type integer is incompatible with the type Doctrine\Common\Lexer\T expected by parameter $type of Doctrine\Common\Lexer\AbstractLexer::isNextToken(). ( Ignorable by Annotation )

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

44
            if ($this->lexer->token->isA(EmailLexer::S_BACKSLASH) && $this->lexer->isNextToken(/** @scrutinizer ignore-type */ EmailLexer::S_DQUOTE)) {
Loading history...
45 4
                $this->lexer->moveNext();
46
            }
47
48 19
            $this->lexer->moveNext();
49
50 19
            if (!$this->escaped() && isset($invalid[$this->lexer->token->type])) {
51
                return new InvalidEmail(new ExpectingATEXT("Expecting ATEXT between DQUOTE"), $this->lexer->token->value);
52
            }
53
        }
54
55 21
        $prev = $this->lexer->getPrevious();
56
57 21
        if ($prev->isA(EmailLexer::S_BACKSLASH)) {
58
            $validQuotedString = $this->checkDQUOTE();
59
            if ($validQuotedString->isInvalid()) return $validQuotedString;
60
        }
61
62 21
        if (!$this->lexer->isNextToken(EmailLexer::S_AT) && !$prev->isA(EmailLexer::S_BACKSLASH)) {
63 8
            return new InvalidEmail(new ExpectingATEXT("Expecting ATEXT between DQUOTE"), $this->lexer->token->value);
64
        }
65
66 13
        return new ValidEmail();
67
    }
68
69 23
    protected function checkDQUOTE(): Result
70
    {
71 23
        $previous = $this->lexer->getPrevious();
72
73 23
        if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous->isA(EmailLexer::GENERIC)) {
0 ignored issues
show
Bug introduced by
Egulias\EmailValidator\EmailLexer::GENERIC of type integer is incompatible with the type Doctrine\Common\Lexer\T expected by parameter $type of Doctrine\Common\Lexer\AbstractLexer::isNextToken(). ( Ignorable by Annotation )

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

73
        if ($this->lexer->isNextToken(/** @scrutinizer ignore-type */ EmailLexer::GENERIC) && $previous->isA(EmailLexer::GENERIC)) {
Loading history...
Bug introduced by
Egulias\EmailValidator\EmailLexer::GENERIC 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

73
        if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous->isA(/** @scrutinizer ignore-type */ EmailLexer::GENERIC)) {
Loading history...
74 1
            $description = 'https://tools.ietf.org/html/rfc5322#section-3.2.4 - quoted string should be a unit';
75 1
            return new InvalidEmail(new ExpectingATEXT($description), $this->lexer->token->value);
76
        }
77
78
        try {
79 22
            $this->lexer->find(EmailLexer::S_DQUOTE);
80 1
        } catch (\Exception $e) {
81 1
            return new InvalidEmail(new UnclosedQuotedString(), $this->lexer->token->value);
82
        }
83 21
        $this->warnings[QuotedString::CODE] = new QuotedString($previous->value, $this->lexer->token->value);
84
85 21
        return new ValidEmail();
86
    }
87
}
88