Test Setup Failed
Push — 3.0.0-dev ( 7e0ee1...302b98 )
by Eduardo Gulias
01:56
created

Parser::warnEscaping()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 3
Ratio 15 %

Importance

Changes 0
Metric Value
dl 3
loc 20
rs 9.6
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
3
namespace Egulias\EmailValidator\Parser;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\Exception\AtextAfterCFWS;
7
use Egulias\EmailValidator\Exception\ConsecutiveDot;
8
use Egulias\EmailValidator\Exception\CRLFAtTheEnd;
9
use Egulias\EmailValidator\Exception\CRLFX2;
10
use Egulias\EmailValidator\Exception\CRNoLF;
11
use Egulias\EmailValidator\Exception\ExpectingQPair;
12
use Egulias\EmailValidator\Exception\ExpectingATEXT;
13
use Egulias\EmailValidator\Exception\ExpectingCTEXT;
14
use Egulias\EmailValidator\Exception\UnclosedComment;
15
use Egulias\EmailValidator\Result\InvalidEmail;
16
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT as ReasonExpectingATEXT;
17
use Egulias\EmailValidator\Result\Result;
18
use Egulias\EmailValidator\Result\ValidEmail;
19
use Egulias\EmailValidator\Warning\CFWSNearAt;
20
use Egulias\EmailValidator\Warning\CFWSWithFWS;
21
use Egulias\EmailValidator\Warning\Comment;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Egulias\EmailValidator\Parser\Comment.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
22
use Egulias\EmailValidator\Warning\QuotedPart;
23
24
abstract class Parser
25
{
26
    /**
27
     * @var \Egulias\EmailValidator\Warning\Warning[]
28
     */
29
    protected $warnings = [];
30
31
    /**
32
     * @var EmailLexer
33
     */
34
    protected $lexer;
35
36
    /**
37
     * @var int
38
     */
39
    protected $openedParenthesis = 0;
40
41
    public function __construct(EmailLexer $lexer)
42
    {
43
        $this->lexer = $lexer;
44
    }
45
46
    /**
47
     * @return \Egulias\EmailValidator\Warning\Warning[]
48
     */
49
    public function getWarnings()
50
    {
51
        return $this->warnings;
52
    }
53
54
    /**
55
     * @param string $str
56
     */
57
    abstract public function parse($str);
58
59
    /** @return int */
60
    public function getOpenedParenthesis()
61
    {
62
        return $this->openedParenthesis;
63
    }
64
65
    /**
66
     * validateQuotedPair
67
     */
68
    protected function validateQuotedPair()
69
    {
70 View Code Duplication
        if (!($this->lexer->token['type'] === EmailLexer::INVALID
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...
71
            || $this->lexer->token['type'] === EmailLexer::C_DEL)) {
72
            throw new ExpectingQPair();
73
        }
74
75
        $this->warnings[QuotedPart::CODE] =
76
            new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']);
77
    }
78
79
    protected function parseComments()
80
    {
81
        $this->openedParenthesis = 1;
82
        $this->isUnclosedComment();
83
        $this->warnings[Comment::CODE] = new Comment();
84
        while (!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) {
85
            if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) {
86
                $this->openedParenthesis++;
87
            }
88
            $this->warnEscaping();
89
            $this->lexer->moveNext();
90
        }
91
92
        $this->lexer->moveNext();
93
        if ($this->lexer->isNextTokenAny(array(EmailLexer::GENERIC, EmailLexer::S_EMPTY))) {
94
            throw new ExpectingATEXT();
95
        }
96
97
        if ($this->lexer->isNextToken(EmailLexer::S_AT)) {
98
            $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt();
99
        }
100
    }
101
102
    /**
103
     * @return bool
104
     */
105
    protected function isUnclosedComment()
106
    {
107
        try {
108
            $this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS);
109
            return true;
110
        } catch (\RuntimeException $e) {
111
            throw new UnclosedComment();
112
        }
113
    }
114
115
    protected function parseFWS()
116
    {
117
        $previous = $this->lexer->getPrevious();
118
119
        $this->checkCRLFInFWS();
120
121
        if ($this->lexer->token['type'] === EmailLexer::S_CR) {
122
            throw new CRNoLF();
123
        }
124
125
        if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type']  !== EmailLexer::S_AT) {
126
            throw new AtextAfterCFWS();
127
        }
128
129 View Code Duplication
        if ($this->lexer->token['type'] === EmailLexer::S_LF || $this->lexer->token['type'] === EmailLexer::C_NUL) {
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...
130
            throw new ExpectingCTEXT();
131
        }
132
133 View Code Duplication
        if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type']  === EmailLexer::S_AT) {
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...
134
            $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt();
135
        } else {
136
            $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
137
        }
138
    }
139
140
    protected function checkConsecutiveDots()
141
    {
142 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...
143
            throw new ConsecutiveDot();
144
        }
145
    }
146
147
    /**
148
     * @return bool
149
     */
150 View Code Duplication
    protected function isFWS()
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...
151
    {
152
        if ($this->escaped()) {
153
            return false;
154
        }
155
156
        return $this->lexer->token['type'] === EmailLexer::S_SP ||
157
            $this->lexer->token['type'] === EmailLexer::S_HTAB ||
158
            $this->lexer->token['type'] === EmailLexer::S_CR ||
159
            $this->lexer->token['type'] === EmailLexer::S_LF ||
160
            $this->lexer->token['type'] === EmailLexer::CRLF;
161
    }
162
163
    /**
164
     * @return bool
165
     */
166 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...
167
    {
168
        $previous = $this->lexer->getPrevious();
169
170
        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...
171
            &&
172
            $this->lexer->token['type'] !== EmailLexer::GENERIC;
173
    }
174
175
    /**
176
     * @return bool
177
     */
178
    protected function warnEscaping() : bool
179
    {
180
        //Backslash found
181
        if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) {
182
            return false;
183
        }
184
185
        if ($this->lexer->isNextToken(EmailLexer::GENERIC)) {
186
            throw new ExpectingATEXT();
187
        }
188
189 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...
190
            return false;
191
        }
192
193
        $this->warnings[QuotedPart::CODE] =
194
            new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']);
195
        return true;
196
197
    }
198
199
    protected function validateEscaping() : Result
200
    {
201
        //Backslash found
202
        if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) {
203
            return new ValidEmail();
204
        }
205
206
        if ($this->lexer->isNextToken(EmailLexer::GENERIC)) {
207
            return new InvalidEmail(new ReasonExpectingATEXT('Found ATOM after escaping'), $this->lexer->token['value']);
208
        }
209
210 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...
211
            return new ValidEmail();
212
        }
213
214
        $this->warnings[QuotedPart::CODE] =
215
            new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']);
216
217
        return new ValidEmail();
218
219
    }
220
221
    protected function checkCRLFInFWS()
222
    {
223
        if ($this->lexer->token['type'] !== EmailLexer::CRLF) {
224
            return;
225
        }
226
227
        if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) {
228
            throw new CRLFX2();
229
        }
230
231
        if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) {
232
            throw new CRLFAtTheEnd();
233
        }
234
    }
235
}
236