Test Failed
Pull Request — master (#203)
by
unknown
02:43
created

LocalPart   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 123
Duplicated Lines 12.2 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 56.63%

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 10
dl 15
loc 123
ccs 47
cts 83
cp 0.5663
rs 9.84
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isInvalidToken() 0 17 3
C parse() 12 51 15
C parseDoubleQuote() 3 50 14

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\Exception\DotAtEnd;
6
use Egulias\EmailValidator\Exception\DotAtStart;
7
use Egulias\EmailValidator\EmailLexer;
8
use Egulias\EmailValidator\EmailValidator;
9
use Egulias\EmailValidator\Exception\ExpectingAT;
10
use Egulias\EmailValidator\Exception\ExpectingATEXT;
11
use Egulias\EmailValidator\Exception\UnclosedQuotedString;
12
use Egulias\EmailValidator\Exception\UnopenedComment;
13
use Egulias\EmailValidator\Warning\CFWSWithFWS;
14
use Egulias\EmailValidator\Warning\LocalTooLong;
15
16
class LocalPart extends Parser
17
{
18 129
    public function parse($localPart)
19
    {
20 129
        $parseDQuote = true;
21 129
        $closingQuote = false;
22 129
        $openedParenthesis = 0;
23
24 129
        while ($this->lexer->token['type'] !== EmailLexer::S_AT && $this->lexer->token) {
25 128
            if ($this->lexer->token['type'] === EmailLexer::S_DOT && !$this->lexer->getPrevious()) {
26 2
                throw new DotAtStart();
27
            }
28
29 126
            $closingQuote = $this->checkDQUOTE($closingQuote);
30 105
            if ($closingQuote && $parseDQuote) {
31
                $parseDQuote = $this->parseDoubleQuote();
32
            }
33
34 105
            if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
35 7
                $this->parseComments();
36 3
                $openedParenthesis += $this->getOpenedParenthesis();
37 3
            }
38 102 View Code Duplication
            if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) {
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...
39 4
                if ($openedParenthesis === 0) {
40 2
                    throw new UnopenedComment();
41
                } else {
42 3
                    $openedParenthesis--;
43
                }
44 3
            }
45
46 102
            $this->checkConsecutiveDots();
47
48 102 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...
49 3
                $this->lexer->isNextToken(EmailLexer::S_AT)
50 102
            ) {
51 2
                throw new DotAtEnd();
52
            }
53
54 102
            $this->warnEscaping();
55 97
            $this->isInvalidToken($this->lexer->token, $closingQuote);
56
57 96
            if ($this->isFWS()) {
58 7
                $this->parseFWS();
59 3
            }
60
61 96
            $this->lexer->moveNext();
62 96
        }
63
64 80
        $prev = $this->lexer->getPrevious() ?: ['value' => ''];
65 80
        if (strlen($prev['value']) > LocalTooLong::LOCAL_PART_LENGTH) {
66 2
            $this->warnings[LocalTooLong::CODE] = new LocalTooLong();
67 2
        }
68 80
    }
69
70
    protected function parseDoubleQuote()
71
    {
72
        $parseAgain = true;
73
        $special = array(
74
            EmailLexer::S_CR => true,
75
            EmailLexer::S_HTAB => true,
76
            EmailLexer::S_LF => true
77
        );
78
79
        $invalid = array(
80
            EmailLexer::C_NUL => true,
81
            EmailLexer::S_HTAB => true,
82
            EmailLexer::S_CR => true,
83
            EmailLexer::S_LF => true
84
        );
85
        $setSpecialsWarning = true;
86
87
        $this->lexer->moveNext();
88
89
        while ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE && $this->lexer->token) {
90
            $parseAgain = false;
91
            if (isset($special[$this->lexer->token['type']]) && $setSpecialsWarning) {
92
                $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
93
                $setSpecialsWarning = false;
94
            }
95 View Code Duplication
            if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH && $this->lexer->isNextToken(EmailLexer::S_DQUOTE)) {
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...
96
                $this->lexer->moveNext();
97
            }
98
99
            $this->lexer->moveNext();
100
101
            if (!$this->escaped() && isset($invalid[$this->lexer->token['type']])) {
102
                throw new ExpectingATEXT();
103
            }
104
        }
105
106
        $prev = $this->lexer->getPrevious() ?: ['type' => null];
107
108
        if ($prev['type'] === EmailLexer::S_BACKSLASH) {
109
            if (!$this->checkDQUOTE(false)) {
110
                throw new UnclosedQuotedString();
111
            }
112
        }
113
114
        if (!$this->lexer->isNextToken(EmailLexer::S_AT) && $prev['type'] !== EmailLexer::S_BACKSLASH) {
115
            throw new ExpectingAT();
116
        }
117
118
        return $parseAgain;
119
    }
120
121 97
    protected function isInvalidToken($token, $closingQuote)
122
    {
123
        $forbidden = array(
124 97
            EmailLexer::S_COMMA,
125 97
            EmailLexer::S_CLOSEBRACKET,
126 97
            EmailLexer::S_OPENBRACKET,
127 97
            EmailLexer::S_GREATERTHAN,
128 97
            EmailLexer::S_LOWERTHAN,
129 97
            EmailLexer::S_COLON,
130 97
            EmailLexer::S_SEMICOLON,
131
            EmailLexer::INVALID
132 97
        );
133
134 97
        if (in_array($token['type'], $forbidden) && !$closingQuote) {
135 4
            throw new ExpectingATEXT();
136
        }
137 96
    }
138
}
139