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

Comment::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
}