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

Comment   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 54
Duplicated Lines 11.11 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 9
dl 6
loc 54
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 6 39 10
A noClosingParenthesis() 0 9 2

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\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
}