DoubleQuote::parse()   C
last analyzed

Complexity

Conditions 14
Paths 30

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 14.1777

Importance

Changes 0
Metric Value
cc 14
eloc 29
nc 30
nop 0
dl 0
loc 51
ccs 28
cts 31
cp 0.9032
crap 14.1777
rs 6.2666
c 0
b 0
f 0

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
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 23
{
16
    public function parse(): Result
17
    {
18 23
19 23
        $validQuotedString = $this->checkDQUOTE();
20
        if ($validQuotedString->isInvalid()) return $validQuotedString;
21 21
22 21
        $special = [
23 21
            EmailLexer::S_CR => true,
24 21
            EmailLexer::S_HTAB => true,
25 21
            EmailLexer::S_LF => true
26
        ];
27 21
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
        ];
34 21
35
        $setSpecialsWarning = true;
36 21
37
        $this->lexer->moveNext();
38 21
39 19
        while (!$this->lexer->current->isA(EmailLexer::S_DQUOTE) && !$this->lexer->current->isA(EmailLexer::S_EMPTY)) {
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->current->isA(/** @scrutinizer ignore-type */ EmailLexer::S_DQUOTE) && !$this->lexer->current->isA(EmailLexer::S_EMPTY)) {
Loading history...
40 2
            if (isset($special[$this->lexer->current->type]) && $setSpecialsWarning) {
41 2
                $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
42
                $setSpecialsWarning = false;
43 19
            }
44 4
            if ($this->lexer->current->isA(EmailLexer::S_BACKSLASH) && $this->lexer->isNextToken(EmailLexer::S_DQUOTE)) {
45
                $this->lexer->moveNext();
46
            }
47 19
48
            $this->lexer->moveNext();
49 19
50
            if (!$this->escaped() && isset($invalid[$this->lexer->current->type])) {
51
                return new InvalidEmail(new ExpectingATEXT("Expecting ATEXT between DQUOTE"), $this->lexer->current->value);
52
            }
53
        }
54 21
55
        $prev = $this->lexer->getPrevious();
56 21
57
        if ($prev->isA(EmailLexer::S_BACKSLASH)) {
58
            $validQuotedString = $this->checkDQUOTE();
59
            if ($validQuotedString->isInvalid()) return $validQuotedString;
60
        }
61 21
62 8
        if (!$this->lexer->isNextToken(EmailLexer::S_AT) && !$prev->isA(EmailLexer::S_BACKSLASH)) {
63
            return new InvalidEmail(new ExpectingATEXT("Expecting ATEXT between DQUOTE"), $this->lexer->current->value);
64
        }
65 13
66
        return new ValidEmail();
67
    }
68 23
69
    protected function checkDQUOTE(): Result
70 23
    {
71
        $previous = $this->lexer->getPrevious();
72 23
73 1
        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 $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
            return new InvalidEmail(new ExpectingATEXT($description), $this->lexer->current->value);
76
        }
77
78 22
        try {
79 1
            $this->lexer->find(EmailLexer::S_DQUOTE);
80 1
        } catch (\Exception $e) {
81
            return new InvalidEmail(new UnclosedQuotedString(), $this->lexer->current->value);
82 21
        }
83
        $this->warnings[QuotedString::CODE] = new QuotedString($previous->value, $this->lexer->current->value);
84 21
85
        return new ValidEmail();
86
    }
87
}
88