Test Setup Failed
Push — 3.0.0-dev ( 61798f...c8e7f0 )
by Eduardo Gulias
01:58
created

Comment::parse()   B

Complexity

Conditions 10
Paths 33

Size

Total Lines 40

Duplication

Lines 6
Ratio 15 %

Importance

Changes 0
Metric Value
dl 6
loc 40
rs 7.6666
c 0
b 0
f 0
cc 10
nc 33
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\ValidEmail;
7
use Egulias\EmailValidator\Warning\CFWSNearAt;
8
use Egulias\EmailValidator\Result\InvalidEmail;
9
use Egulias\EmailValidator\Parser\CommentStrategy;
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
class Comment extends Parser
16
{
17
    //change to private when removed from parent parser
18
    protected $openedParenthesis = 0;
19
    private $commentStrategy;
20
21
    public function __construct(EmailLexer $lexer, CommentStrategy $commentStrategy)
22
    {
23
        $this->lexer = $lexer;
24
        $this->commentStrategy = $commentStrategy;
25
    }
26
27
    public function parse($str)
28
    {
29
        if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
30
            $this->openedParenthesis++;
31
            if($this->noClosingParenthesis()) {
32
                return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']);
33
            }
34
        }
35
36 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...
37
            return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']);
38
        }
39
40
        $this->warnings[WarningComment::CODE] = new WarningComment();
41
42
        $moreTokens = true;
43
        while ($this->commentStrategy->exitCondition($this->lexer, $this->openedParenthesis) && $moreTokens){
44
45
            if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) {
46
                $this->openedParenthesis++;
47
            }
48
            $this->warnEscaping();
49
            if($this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) {
50
                $this->openedParenthesis--;
51
            }
52
            $moreTokens = $this->lexer->moveNext();
53
        }
54
55
        if($this->openedParenthesis >= 1) {
56
            return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']);
57 View Code Duplication
        } else if ($this->openedParenthesis < 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...
58
            return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']);
59
        }
60
61
        $finalValidations = $this->commentStrategy->endOfLoopValidations($this->lexer);
62
63
        $this->warnings = array_merge($this->warnings, $this->commentStrategy->getWarnings());
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->warni...trategy->getWarnings()) of type array is incompatible with the declared type array<integer,object<Egu...dator\Warning\Warning>> of property $warnings.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
64
65
        return $finalValidations;
66
    }
67
68
    private function noClosingParenthesis() : bool 
69
    {
70
        try {
71
            $this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS);
72
            return false;
73
        } catch (\RuntimeException $e) {
74
            return true;
75
        }
76
    }
77
}