Comment   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 84.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 87
ccs 32
cts 38
cp 0.8421
rs 10
c 1
b 0
f 0
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B parse() 0 40 10
A warnEscaping() 0 14 3
A noClosingParenthesis() 0 7 2
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\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 PartParser
15
{
16
    /**
17
     * @var int
18
     */
19
    private $openedParenthesis = 0;
20
21
    /**
22
     * @var CommentStrategy
23
     */
24
    private $commentStrategy;
25
26 15
    public function __construct(EmailLexer $lexer, CommentStrategy $commentStrategy)
27
    {
28 15
        $this->lexer = $lexer;
29 15
        $this->commentStrategy = $commentStrategy;
30
    }
31
32 15
    public function parse(): Result
33
    {
34 15
        if ($this->lexer->current->isA(EmailLexer::S_OPENPARENTHESIS)) {
0 ignored issues
show
Bug introduced by
Egulias\EmailValidator\E...exer::S_OPENPARENTHESIS of type integer is incompatible with the type Doctrine\Common\Lexer\T expected by parameter $types of Doctrine\Common\Lexer\Token::isA(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        if ($this->lexer->current->isA(/** @scrutinizer ignore-type */ EmailLexer::S_OPENPARENTHESIS)) {
Loading history...
35 13
            $this->openedParenthesis++;
36 13
            if ($this->noClosingParenthesis()) {
37 5
                return new InvalidEmail(new UnclosedComment(), $this->lexer->current->value);
38
            }
39
        }
40
41 10
        if ($this->lexer->current->isA(EmailLexer::S_CLOSEPARENTHESIS)) {
42 2
            return new InvalidEmail(new UnOpenedComment(), $this->lexer->current->value);
43
        }
44
45 8
        $this->warnings[WarningComment::CODE] = new WarningComment();
46
47 8
        $moreTokens = true;
48 8
        while ($this->commentStrategy->exitCondition($this->lexer, $this->openedParenthesis) && $moreTokens) {
49
50 8
            if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) {
51 1
                $this->openedParenthesis++;
52
            }
53 8
            $this->warnEscaping();
54 8
            if ($this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) {
55 8
                $this->openedParenthesis--;
56
            }
57 8
            $moreTokens = $this->lexer->moveNext();
58
        }
59
60 8
        if ($this->openedParenthesis >= 1) {
61
            return new InvalidEmail(new UnclosedComment(), $this->lexer->current->value);
62
        }
63 8
        if ($this->openedParenthesis < 0) {
64 3
            return new InvalidEmail(new UnOpenedComment(), $this->lexer->current->value);
65
        }
66
67 5
        $finalValidations = $this->commentStrategy->endOfLoopValidations($this->lexer);
68
69 5
        $this->warnings = array_merge($this->warnings, $this->commentStrategy->getWarnings());
70
71 5
        return $finalValidations;
72
    }
73
74
75
    /**
76
     * @return bool
77
     */
78 8
    private function warnEscaping(): bool
79
    {
80
        //Backslash found
81 8
        if (!$this->lexer->current->isA(EmailLexer::S_BACKSLASH)) {
0 ignored issues
show
Bug introduced by
Egulias\EmailValidator\EmailLexer::S_BACKSLASH of type integer is incompatible with the type Doctrine\Common\Lexer\T expected by parameter $types of Doctrine\Common\Lexer\Token::isA(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
        if (!$this->lexer->current->isA(/** @scrutinizer ignore-type */ EmailLexer::S_BACKSLASH)) {
Loading history...
82 8
            return false;
83
        }
84
85
        if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
86
            return false;
87
        }
88
89
        $this->warnings[QuotedPart::CODE] =
90
            new QuotedPart($this->lexer->getPrevious()->type, $this->lexer->current->type);
91
        return true;
92
    }
93
94
    private function noClosingParenthesis(): bool
95 13
    {
96
        try {
97
            $this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS);
98 13
            return false;
99 8
        } catch (\RuntimeException $e) {
100 5
            return true;
101 5
        }
102
    }
103
}
104