Completed
Pull Request — 1.2 (#222)
by Graham
05:50
created

LocalPart   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 123
Duplicated Lines 4.07 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.59%

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 2
dl 5
loc 123
ccs 81
cts 83
cp 0.9759
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C parse() 5 54 14
B parseDoubleQuote() 0 47 11
A isInvalidToken() 0 17 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Egulias\EmailValidator\Parser;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\EmailValidator;
7
8
class LocalPart extends Parser
9
{
10 150
    public function parse($localPart)
11
    {
12 150
        $parseDQuote = true;
13 150
        $closingQuote = false;
14 150
        $openedParenthesis = 0;
15
16 150
        while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) {
17 150
            if ($this->lexer->token['type'] === EmailLexer::S_DOT) {
18 6
                $previous = $this->lexer->getPrevious();
19 6
                if (null === $previous['type']) {
20 2
                    throw new \InvalidArgumentException('ERR_DOT_START');
21
                }
22 4
            }
23
24 148
            $closingQuote = $this->checkDQUOTE($closingQuote);
25 147
            if ($closingQuote && $parseDQuote) {
26 23
                $parseDQuote = $this->parseDoubleQuote();
27 15
            }
28
29 139
            if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
30 7
                $this->parseComments();
31 5
                $openedParenthesis += $this->getOpenedParenthesis();
32 5
            }
33 138
            if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) {
34 6
                if ($openedParenthesis === 0) {
35 2
                    throw new \InvalidArgumentException('ERR_UNOPENEDCOMMENT');
36
                } else {
37 5
                    $openedParenthesis--;
38
                }
39 5
            }
40
41 138
            $this->checkConsecutiveDots();
42
43 138 View Code Duplication
            if ($this->lexer->token['type'] === EmailLexer::S_DOT &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44 3
                $this->lexer->isNextToken(EmailLexer::S_AT)
45 138
            ) {
46 2
                throw new \InvalidArgumentException('ERR_DOT_END');
47
            }
48
49 138
            $this->warnEscaping();
50 133
            $this->isInvalidToken($this->lexer->token, $closingQuote);
51
52 132
            if ($this->isFWS()) {
53 8
                $this->parseFWS();
54 4
            }
55
56 132
            $this->lexer->moveNext();
57 132
        }
58
59 115
        $prev = $this->lexer->getPrevious();
60 115
        if (strlen($prev['value']) > EmailValidator::RFC5322_LOCAL_TOOLONG) {
61 2
            $this->warnings[] = EmailValidator::RFC5322_LOCAL_TOOLONG;
62 2
        }
63 115
    }
64
65 23
    protected function parseDoubleQuote()
66
    {
67 23
        $parseAgain = true;
68
        $special = array(
69 23
            EmailLexer::S_CR => true,
70 23
            EmailLexer::S_HTAB => true,
71 23
            EmailLexer::S_LF => true
72 23
        );
73
74
        $invalid = array(
75 23
            EmailLexer::C_NUL => true,
76 23
            EmailLexer::S_HTAB => true,
77 23
            EmailLexer::S_CR => true,
78 23
            EmailLexer::S_LF => true
79 23
        );
80 23
        $setSpecialsWarning = true;
81
82 23
        $this->lexer->moveNext();
83
84 23
        while ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE && null !== $this->lexer->token['type']) {
85 21
            $parseAgain = false;
86 21
            if (isset($special[$this->lexer->token['type']]) && $setSpecialsWarning) {
87 4
                $this->warnings[] = EmailValidator::CFWS_FWS;
88 4
                $setSpecialsWarning = false;
89 4
            }
90
91 21
            $this->lexer->moveNext();
92
93 21
            if (!$this->escaped() && isset($invalid[$this->lexer->token['type']])) {
94
                throw new \InvalidArgumentException('ERR_EXPECTED_ATEXT');
95
            }
96 21
        }
97
98 23
        $prev = $this->lexer->getPrevious();
99
100 23
        if ($prev['type'] === EmailLexer::S_BACKSLASH) {
101 3
            if (!$this->checkDQUOTE(false)) {
102
                throw new \InvalidArgumentException('ERR_UNCLOSED_DQUOTE');
103
            }
104 1
        }
105
106 21
        if (!$this->lexer->isNextToken(EmailLexer::S_AT) && $prev['type'] !== EmailLexer::S_BACKSLASH) {
107 6
            throw new \InvalidArgumentException('ERR_EXPECED_AT');
108
        }
109
110 15
        return $parseAgain;
111
    }
112
113 133
    protected function isInvalidToken($token, $closingQuote)
114
    {
115
        $forbidden = array(
116 133
            EmailLexer::S_COMMA,
117 133
            EmailLexer::S_CLOSEBRACKET,
118 133
            EmailLexer::S_OPENBRACKET,
119 133
            EmailLexer::S_GREATERTHAN,
120 133
            EmailLexer::S_LOWERTHAN,
121 133
            EmailLexer::S_COLON,
122 133
            EmailLexer::S_SEMICOLON,
123
            EmailLexer::INVALID
124 133
        );
125
126 133
        if (in_array($token['type'], $forbidden) && !$closingQuote) {
127 4
            throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT');
128
        }
129 132
    }
130
}
131