Passed
Pull Request — 3.x (#348)
by
unknown
01:48
created

DoubleQuote::parse()   C

Complexity

Conditions 14
Paths 30

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 14.1612

Importance

Changes 0
Metric Value
cc 14
eloc 29
c 0
b 0
f 0
nc 30
nop 0
dl 0
loc 51
ccs 29
cts 32
cp 0.9063
crap 14.1612
rs 6.2666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Egulias\EmailValidator\Parser;
3
4
use Egulias\EmailValidator\EmailLexer;
5
use Egulias\EmailValidator\Result\ValidEmail;
6
use Egulias\EmailValidator\Result\InvalidEmail;
7
use Egulias\EmailValidator\Warning\CFWSWithFWS;
8
use Egulias\EmailValidator\Warning\QuotedString;
9
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
10
use Egulias\EmailValidator\Result\Reason\UnclosedQuotedString;
11
use Egulias\EmailValidator\Result\Result;
12
13
class DoubleQuote extends PartParser
14
{
15 23
    public function parse() : Result
16
    {
17
18 23
        $validQuotedString = $this->checkDQUOTE();
19 23
        if($validQuotedString->isInvalid()) return $validQuotedString;
20
21 21
        $special = [
22 21
            EmailLexer::S_CR => true,
23 21
            EmailLexer::S_HTAB => true,
24 21
            EmailLexer::S_LF => true
25 21
        ];
26
27 21
        $invalid = [
28 21
            EmailLexer::C_NUL => true,
29 21
            EmailLexer::S_HTAB => true,
30 21
            EmailLexer::S_CR => true,
31 21
            EmailLexer::S_LF => true
32 21
        ];
33
        
34 21
        $setSpecialsWarning = true;
35
36 21
        $this->lexer->moveNext();
37
38 21
        while ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE && null !== $this->lexer->token['type']) {
39 19
            if (isset($special[$this->lexer->token['type']]) && $setSpecialsWarning) {
40 2
                $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
41 2
                $setSpecialsWarning = false;
42
            }
43 19
            if ($this->lexer->token['type'] === 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

43
            if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH && $this->lexer->isNextToken(/** @scrutinizer ignore-type */ EmailLexer::S_DQUOTE)) {
Loading history...
44 4
                $this->lexer->moveNext();
45
            }
46
47 19
            $this->lexer->moveNext();
48
49 19
            if (!$this->escaped() && isset($invalid[$this->lexer->token['type']])) {
50
                return new InvalidEmail(new ExpectingATEXT("Expecting ATEXT between DQUOTE"), $this->lexer->token['value']);
51
            }
52
        }
53
54 21
        $prev = $this->lexer->getPrevious();
55
56 21
        if ($prev['type'] === EmailLexer::S_BACKSLASH) {
57
            $validQuotedString = $this->checkDQUOTE();
58
            if($validQuotedString->isInvalid()) return $validQuotedString;
59
        }
60
61 21
        if (!$this->lexer->isNextToken(EmailLexer::S_AT) && $prev['type'] !== EmailLexer::S_BACKSLASH) {
62 8
            return new InvalidEmail(new ExpectingATEXT("Expecting ATEXT between DQUOTE"), $this->lexer->token['value']);
63
        }
64
65 13
        return new ValidEmail();
66
    }
67
68 23
    protected function checkDQUOTE() : Result
69
    {
70 23
        $previous = $this->lexer->getPrevious();
71
72 23
        if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] === 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

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