Passed
Push — 3.x ( 312b65...a5ed8d )
by Eduardo Gulias
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 18
CRAP Score 14.5719

Importance

Changes 0
Metric Value
cc 14
eloc 29
c 0
b 0
f 0
nc 30
nop 0
dl 0
loc 51
ccs 18
cts 21
cp 0.8571
crap 14.5719
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\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
}