Test Setup Failed
Push — 3.0.0-dev ( aa0eea...4a38c4 )
by Eduardo Gulias
07:15
created

Comment::parse()   B

Complexity

Conditions 10
Paths 43

Size

Total Lines 39

Duplication

Lines 6
Ratio 15.38 %

Importance

Changes 0
Metric Value
dl 6
loc 39
rs 7.6666
c 0
b 0
f 0
cc 10
nc 43
nop 1

How to fix   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\Warning\CFWSNearAt;
9
use Egulias\EmailValidator\Result\InvalidEmail;
10
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
11
use Egulias\EmailValidator\Result\Reason\UnclosedComment;
12
use Egulias\EmailValidator\Result\Reason\UnOpenedComment;
13
use Egulias\EmailValidator\Warning\Comment as WarningComment;
14
15
16
class Comment extends Parser
17
{
18
    private $MopenedParenthesis = 0;
19
20
    public function parse($str)
21
    {
22
        if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
23
            $this->MopenedParenthesis++;
24
            if($this->noClosingParenthesis()) {
25
                return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']);
26
            }
27
        }
28
29 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...
30
            return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']);
31
        }
32
33
        $this->warnings[WarningComment::CODE] = new WarningComment();
34
        while (!$this->lexer->isNextToken(EmailLexer::S_AT)) {//!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) {
35
            if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) {
36
                $this->MopenedParenthesis++;
37
            }
38
            $this->warnEscaping();
39
            if($this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) {
40
                $this->MopenedParenthesis--;
41
            }
42
            $this->lexer->moveNext();
43
        }
44
45
        if($this->MopenedParenthesis >= 1) {
46
            return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']);
47 View Code Duplication
        } else if ($this->MopenedParenthesis < 0) {
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...
48
            return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']);
49
        }
50
51
        if (!$this->lexer->isNextToken(EmailLexer::S_AT)) {
52
            return new InvalidEmail(new ExpectingATEXT('ATEX is not expected after closing comments'), $this->lexer->token['value']);
53
        }
54
55
        //You should always end at @
56
        $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt();
57
        return new ValidEmail();
58
    }
59
60
    private function noClosingParenthesis() : bool 
61
    {
62
        try {
63
            $this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS);
64
            return false;
65
        } catch (\RuntimeException $e) {
66
            return true;
67
        }
68
    }
69
}