Completed
Push — 1.2 ( cbfe3e...b8bb14 )
by Eduardo Gulias
02:54
created

Parser   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 190
Duplicated Lines 5.26 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 87.5%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 48
c 5
b 0
f 0
lcom 1
cbo 1
dl 10
loc 190
ccs 91
cts 104
cp 0.875
rs 8.4864

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getWarnings() 0 4 1
parse() 0 1 ?
A getOpenedParenthesis() 0 4 1
A validateQuotedPair() 4 9 3
B parseComments() 0 22 5
A isUnclosedComment() 0 9 2
C parseFWS() 3 24 8
A checkConsecutiveDots() 3 6 3
B isFWS() 0 17 7
A escaped() 0 13 3
A warnEscaping() 0 18 4
B checkDQUOTE() 0 23 6
A checkCRLFInFWS() 0 12 4

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Parser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Parser, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Egulias\EmailValidator\Parser;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\EmailValidator;
7
8
abstract class Parser
9
{
10
    protected $warnings = array();
11
    protected $lexer;
12
    protected $openedParenthesis = 0;
13
14 151
    public function __construct(EmailLexer $lexer)
15
    {
16 151
        $this->lexer = $lexer;
17 151
    }
18
19 75
    public function getWarnings()
20
    {
21 75
        return $this->warnings;
22
    }
23
24
    abstract public function parse($str);
25
26
    /** @return int */
27 9
    public function getOpenedParenthesis()
28
    {
29 9
        return $this->openedParenthesis;
30
    }
31
32
    /**
33
     * validateQuotedPair
34
     */
35
    protected function validateQuotedPair()
36
    {
37 View Code Duplication
        if (!($this->lexer->token['type'] === EmailLexer::INVALID
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...
38
            || $this->lexer->token['type'] === EmailLexer::C_DEL)) {
39
            throw new \InvalidArgumentException('ERR_EXPECTING_QPAIR');
40
        }
41
42
        $this->warnings[] = EmailValidator::DEPREC_QP;
43
    }
44
45 11
    protected function parseComments()
46
    {
47 11
        $this->openedParenthesis = 1;
48 11
        $this->isUnclosedComment();
49 10
        $this->warnings[] = EmailValidator::CFWS_COMMENT;
50 10
        while (!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) {
51 10
            if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) {
52 1
                $this->openedParenthesis++;
53 1
            }
54 10
            $this->warnEscaping();
55 10
            $this->lexer->moveNext();
56 10
        }
57
58 10
        $this->lexer->moveNext();
59 10
        if ($this->lexer->isNextTokenAny(array(EmailLexer::GENERIC, EmailLexer::S_EMPTY))) {
60 1
            throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT');
61
        }
62
63 9
        if ($this->lexer->isNextToken(EmailLexer::S_AT)) {
64 2
            $this->warnings[] = EmailValidator::DEPREC_CFWS_NEAR_AT;
65 2
        }
66 9
    }
67
68 14
    protected function isUnclosedComment()
69
    {
70
        try {
71 14
            $this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS);
72 12
            return true;
73 2
        } catch (\RuntimeException $e) {
74 2
            throw new \InvalidArgumentException('ERR_UNCLOSEDCOMMENT');
75
        }
76
    }
77
78 18
    protected function parseFWS()
79
    {
80 18
        $previous = $this->lexer->getPrevious();
81
82 18
        $this->checkCRLFInFWS();
83
84 18
        if ($this->lexer->token['type'] === EmailLexer::S_CR) {
85 2
            throw new \InvalidArgumentException('ERR_CR_NO_LF');
86
        }
87
88 16
        if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type']  !== EmailLexer::S_AT) {
89 7
            throw new \InvalidArgumentException('ERR_ATEXT_AFTER_CFWS');
90
        }
91
92 12 View Code Duplication
        if ($this->lexer->token['type'] === EmailLexer::S_LF || $this->lexer->token['type'] === EmailLexer::C_NUL) {
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...
93
            throw new \InvalidArgumentException('ERR_EXPECTING_CTEXT');
94
        }
95
96 12
        if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type']  === EmailLexer::S_AT) {
97 5
            $this->warnings[] = EmailValidator::DEPREC_CFWS_NEAR_AT;
98 5
        } else {
99 8
            $this->warnings[] = EmailValidator::CFWS_FWS;
100
        }
101 12
    }
102
103 138
    protected function checkConsecutiveDots()
104
    {
105 138 View Code Duplication
        if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(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...
106 3
            throw new \InvalidArgumentException('ERR_CONSECUTIVEDOTS');
107
        }
108 138
    }
109
110 132
    protected function isFWS()
111
    {
112 132
        if ($this->escaped()) {
113 2
            return false;
114
        }
115
116 132
        if ($this->lexer->token['type'] === EmailLexer::S_SP ||
117 132
            $this->lexer->token['type'] === EmailLexer::S_HTAB ||
118 132
            $this->lexer->token['type'] === EmailLexer::S_CR ||
119 132
            $this->lexer->token['type'] === EmailLexer::S_LF ||
120 132
            $this->lexer->token['type'] === EmailLexer::CRLF
121 132
        ) {
122 18
            return true;
123
        }
124
125 132
        return false;
126
    }
127
128 139
    protected function escaped()
129
    {
130 139
        $previous = $this->lexer->getPrevious();
131
132 139
        if ($previous['type'] === EmailLexer::S_BACKSLASH
133 139
            &&
134 6
            $this->lexer->token['type'] !== EmailLexer::GENERIC
135 139
        ) {
136 5
            return true;
137
        }
138
139 138
        return false;
140
    }
141
142 138
    protected function warnEscaping()
143
    {
144 138
        if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) {
145 133
            return false;
146
        }
147
148 7
        if ($this->lexer->isNextToken(EmailLexer::GENERIC)) {
149 6
            throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT');
150
        }
151
152 1
        if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
153
            return false;
154
        }
155
156 1
        $this->warnings[] = EmailValidator::DEPREC_QP;
157 1
        return true;
158
159
    }
160
161 148
    protected function checkDQUOTE($hasClosingQuote)
162
    {
163 148
        if ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE) {
164 124
            return $hasClosingQuote;
165
        }
166 25
        if ($hasClosingQuote) {
167 1
            return $hasClosingQuote;
168
        }
169 25
        $previous = $this->lexer->getPrevious();
170 25
        if ($previous['type'] === EmailLexer::GENERIC && $this->lexer->isNextToken(EmailLexer::GENERIC)) {
171 1
            throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT');
172
        }
173
174 24
        $this->warnings[] = EmailValidator::RFC5321_QUOTEDSTRING;
175
        try {
176 24
            $this->lexer->find(EmailLexer::S_DQUOTE);
177 23
            $hasClosingQuote = true;
178 24
        } catch (\Exception $e) {
179 3
            throw new \InvalidArgumentException('ERR_UNCLOSEDQUOTEDSTR');
180
        }
181
182 23
        return $hasClosingQuote;
183
    }
184
185 18
    protected function checkCRLFInFWS()
186
    {
187 18
        if ($this->lexer->token['type'] !== EmailLexer::CRLF) {
188 18
            return;
189
        }
190
        if ($this->lexer->isNextToken(EmailLexer::CRLF)) {
191
            throw new \InvalidArgumentException('ERR_FWS_CRLF_X2');
192
        }
193
        if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) {
194
            throw new \InvalidArgumentException('ERR_FWS_CRLF_END');
195
        }
196
    }
197
}
198