Test Setup Failed
Push — 3.0.0-dev ( e2e234...b76c11 )
by Eduardo Gulias
01:56
created

Comment::warnEscaping()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 3
Ratio 18.75 %

Importance

Changes 0
Metric Value
dl 3
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Egulias\EmailValidator\Parser;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\Result\Result;
7
use Egulias\EmailValidator\Warning\QuotedPart;
8
use Egulias\EmailValidator\Result\InvalidEmail;
9
use Egulias\EmailValidator\Parser\CommentStrategy;
10
use Egulias\EmailValidator\Result\Reason\UnclosedComment;
11
use Egulias\EmailValidator\Result\Reason\UnOpenedComment;
12
use Egulias\EmailValidator\Warning\Comment as WarningComment;
13
14
class Comment extends Parser
15
{
16
    /**
17
     * @var int
18
     */
19
    private $openedParenthesis = 0;
20
21
    /**
22
     * @var CommentStrategy
23
     */
24
    private $commentStrategy;
25
26
    public function __construct(EmailLexer $lexer, CommentStrategy $commentStrategy)
27
    {
28
        $this->lexer = $lexer;
29
        $this->commentStrategy = $commentStrategy;
30
    }
31
32
    public function parse() : Result
33
    {
34
        if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
35
            $this->openedParenthesis++;
36
            if($this->noClosingParenthesis()) {
37
                return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']);
38
            }
39
        }
40
41
        if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) {
42
            return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']);
43
        }
44
45
        $this->warnings[WarningComment::CODE] = new WarningComment();
46
47
        $moreTokens = true;
48
        while ($this->commentStrategy->exitCondition($this->lexer, $this->openedParenthesis) && $moreTokens){
49
50
            if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) {
51
                $this->openedParenthesis++;
52
            }
53
            $this->warnEscaping();
54
            if($this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) {
55
                $this->openedParenthesis--;
56
            }
57
            $moreTokens = $this->lexer->moveNext();
58
        }
59
60
        if($this->openedParenthesis >= 1) {
61
            return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']);
62
        } else if ($this->openedParenthesis < 0) {
63
            return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']);
64
        }
65
66
        $finalValidations = $this->commentStrategy->endOfLoopValidations($this->lexer);
67
68
        $this->warnings = array_merge($this->warnings, $this->commentStrategy->getWarnings());
69
70
        return $finalValidations;
71
    }
72
73
74
    /**
75
     * @return bool
76
     */
77
    private function warnEscaping() : bool
78
    {
79
        //Backslash found
80
        if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) {
81
            return false;
82
        }
83
84 View Code Duplication
        if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
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...
85
            return false;
86
        }
87
88
        $this->warnings[QuotedPart::CODE] =
89
            new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']);
90
        return true;
91
92
    }
93
94
    private function noClosingParenthesis() : bool 
95
    {
96
        try {
97
            $this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS);
98
            return false;
99
        } catch (\RuntimeException $e) {
100
            return true;
101
        }
102
    }
103
}