Passed
Pull Request — 3.x (#323)
by Eduardo Gulias
04:35 queued 02:42
created

DoubleQuote   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 90.32%

Importance

Changes 0
Metric Value
eloc 40
c 0
b 0
f 0
dl 0
loc 72
ccs 28
cts 31
cp 0.9032
rs 10
wmc 18

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkDQUOTE() 0 17 4
C parse() 0 51 14
1
<?php
2
namespace Egulias\EmailValidator\Parser;
3
4
use Egulias\EmailValidator\EmailLexer;
5
use Egulias\EmailValidator\Parser\Parser;
0 ignored issues
show
Bug introduced by
The type Egulias\EmailValidator\Parser\Parser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
        $special = [
23
            EmailLexer::S_CR => true,
24
            EmailLexer::S_HTAB => true,
25
            EmailLexer::S_LF => true
26
        ];
27
28
        $invalid = [
29
            EmailLexer::C_NUL => true,
30
            EmailLexer::S_HTAB => true,
31
            EmailLexer::S_CR => true,
32
            EmailLexer::S_LF => true
33
        ];
34
        
35 21
        $setSpecialsWarning = true;
36
37 21
        $this->lexer->moveNext();
38
39 21
        while ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE && null !== $this->lexer->token['type']) {
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['type'] === EmailLexer::S_BACKSLASH && $this->lexer->isNextToken(EmailLexer::S_DQUOTE)) {
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['type'] === 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['type'] !== 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['type'] === EmailLexer::GENERIC) {
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
}