Test Setup Failed
Push — 3.0.0-dev ( 6abede...16ae24 )
by Eduardo Gulias
01:43
created

Parser::escaped()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 10
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\Exception\CRLFAtTheEnd;
7
use Egulias\EmailValidator\Exception\CRLFX2;
8
use Egulias\EmailValidator\Exception\ExpectingATEXT;
9
use Egulias\EmailValidator\Result\InvalidEmail;
10
use Egulias\EmailValidator\Result\Reason\ConsecutiveDot;
11
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT as ReasonExpectingATEXT;
12
use Egulias\EmailValidator\Result\Result;
13
use Egulias\EmailValidator\Result\ValidEmail;
14
use Egulias\EmailValidator\Warning\QuotedPart;
15
16
abstract class Parser
17
{
18
    /**
19
     * @var \Egulias\EmailValidator\Warning\Warning[]
20
     */
21
    protected $warnings = [];
22
23
    /**
24
     * @var EmailLexer
25
     */
26
    protected $lexer;
27
28
    public function __construct(EmailLexer $lexer)
29
    {
30
        $this->lexer = $lexer;
31
    }
32
33
    /**
34
     * @return \Egulias\EmailValidator\Warning\Warning[]
35
     */
36
    public function getWarnings()
37
    {
38
        return $this->warnings;
39
    }
40
41
    /**
42
     * @param string $str
43
     */
44
    abstract public function parse($str);
45
46 View Code Duplication
    protected function parseFWS() : Result
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
47
    {
48
        $foldingWS = new FoldingWhiteSpace($this->lexer);
49
        $resultFWS = $foldingWS->parse('remove');
50
        $this->warnings = array_merge($this->warnings, $foldingWS->getWarnings());
51
        return $resultFWS;
52
    }
53
54
    protected function checkConsecutiveDots() : Result
55
    {
56 View Code Duplication
        if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
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...
57
            return new InvalidEmail(new ConsecutiveDot(), $this->lexer->token['value']);
58
        }
59
60
        return new ValidEmail();
61
    }
62
63
    /**
64
     * @return bool
65
     */
66 View Code Duplication
    protected function escaped()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
67
    {
68
        $previous = $this->lexer->getPrevious();
69
70
        return $previous && $previous['type'] === EmailLexer::S_BACKSLASH
0 ignored issues
show
Bug Best Practice introduced by
The expression $previous of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
71
            &&
72
            $this->lexer->token['type'] !== EmailLexer::GENERIC;
73
    }
74
75
    /**
76
     * @return bool
77
     */
78
    protected function warnEscaping() : bool
79
    {
80
        //Backslash found
81
        if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) {
82
            return false;
83
        }
84
85
        if ($this->lexer->isNextToken(EmailLexer::GENERIC)) {
86
            throw new ExpectingATEXT();
87
        }
88
89 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...
90
            return false;
91
        }
92
93
        $this->warnings[QuotedPart::CODE] =
94
            new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']);
95
        return true;
96
97
    }
98
99
    protected function validateEscaping() : Result
100
    {
101
        //Backslash found
102
        if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) {
103
            return new ValidEmail();
104
        }
105
106
        if ($this->lexer->isNextToken(EmailLexer::GENERIC)) {
107
            return new InvalidEmail(new ReasonExpectingATEXT('Found ATOM after escaping'), $this->lexer->token['value']);
108
        }
109
110 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...
111
            return new ValidEmail();
112
        }
113
114
        $this->warnings[QuotedPart::CODE] =
115
            new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']);
116
117
        return new ValidEmail();
118
119
    }
120
}