Comment::parse()   B
last analyzed

Complexity

Conditions 10
Paths 33

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 10.0082

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 22
c 1
b 0
f 0
nc 33
nop 0
dl 0
loc 40
ccs 22
cts 23
cp 0.9565
crap 10.0082
rs 7.6666

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