Completed
Push — 3.0.0-dev ( ac3f65...1cfe87 )
by Eduardo Gulias
02:02
created

LocalPart::parse()   C

Complexity

Conditions 13
Paths 48

Size

Total Lines 61

Duplication

Lines 12
Ratio 19.67 %

Importance

Changes 0
Metric Value
dl 12
loc 61
rs 6.6166
c 0
b 0
f 0
cc 13
nc 48
nop 1

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\Result;
7
use Egulias\EmailValidator\Result\ValidEmail;
8
use Egulias\EmailValidator\Exception\DotAtEnd;
9
use Egulias\EmailValidator\Result\InvalidEmail;
10
use Egulias\EmailValidator\Warning\LocalTooLong;
11
use Egulias\EmailValidator\Exception\ExpectingATEXT;
12
use Egulias\EmailValidator\Result\Reason\DotAtStart;
13
use Egulias\EmailValidator\Exception\UnopenedComment;
14
15
class LocalPart extends Parser
16
{
17
    public function parse($localPart) : Result
18
    {
19
        $parseDQuote = true;
0 ignored issues
show
Unused Code introduced by
$parseDQuote is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
20
        $closingQuote = false;
21
        $openedParenthesis = 0;
22
        $totalLength = 0;
23
24
        while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) {
25
            if ($this->hasDotAtStart()) {
26
                return new InvalidEmail(new DotAtStart(), $this->lexer->token['value']);
27
            }
28
29
            //if ($parseDQuote) {
30
            if ($this->lexer->token['type'] === EmailLexer::S_DQUOTE) {
31
                $dquoteParsingResult = $this->parseDoubleQuote();
32
                $parseDQuote = !$dquoteParsingResult->isValid();
33
34
                //Invalid double quote parsing
35
                if($parseDQuote) {
36
                    return $dquoteParsingResult;
37
                }
38
            }
39
40
            if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
41
                $this->parseComments();
42
                $openedParenthesis += $this->getOpenedParenthesis();
43
            }
44
45 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...
46
                if ($openedParenthesis === 0) {
47
                    throw new UnopenedComment();
48
                }
49
50
                $openedParenthesis--;
51
            }
52
53
            $this->checkConsecutiveDots();
54
55 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...
56
                $this->lexer->isNextToken(EmailLexer::S_AT)
57
            ) {
58
                throw new DotAtEnd();
59
            }
60
61
            $this->warnEscaping();
62
            $this->isInvalidToken($this->lexer->token, $closingQuote);
63
64
            if ($this->isFWS()) {
65
                $this->parseFWS();
66
            }
67
68
            $totalLength += strlen($this->lexer->token['value']);
69
            $this->lexer->moveNext();
70
        }
71
72
        if ($totalLength > LocalTooLong::LOCAL_PART_LENGTH) {
73
            $this->warnings[LocalTooLong::CODE] = new LocalTooLong();
74
        }
75
76
        return new ValidEmail();
77
    }
78
79
    protected function hasDotAtStart() : bool
80
    {
81
            return $this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type'];
82
    }
83
84
    protected function parseDoubleQuote() : Result
85
    {
86
        $dquoteParser = new DoubleQuote($this->lexer);
87
        $parseAgain = $dquoteParser->parse("remove useless arg");
88
        $warns = $dquoteParser->getWarnings();
89
        foreach ($warns as $code => $dWarning) {
90
            $this->warnings[$code] = $dWarning;
91
        }
92
93
        return $parseAgain;
94
    }
95
96
    /**
97
     * @param bool $closingQuote
98
     */
99
    protected function isInvalidToken(array $token, $closingQuote)
100
    {
101
        $forbidden = array(
102
            EmailLexer::S_COMMA,
103
            EmailLexer::S_CLOSEBRACKET,
104
            EmailLexer::S_OPENBRACKET,
105
            EmailLexer::S_GREATERTHAN,
106
            EmailLexer::S_LOWERTHAN,
107
            EmailLexer::S_COLON,
108
            EmailLexer::S_SEMICOLON,
109
            EmailLexer::INVALID
110
        );
111
112
        if (in_array($token['type'], $forbidden) && !$closingQuote) {
113
            throw new ExpectingATEXT();
114
        }
115
    }
116
}