1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Egulias\EmailValidator\Parser; |
4
|
|
|
|
5
|
|
|
use Egulias\EmailValidator\EmailLexer; |
6
|
|
|
use Egulias\EmailValidator\Result\Result; |
7
|
|
|
use Egulias\EmailValidator\Result\ValidEmail; |
8
|
|
|
use Egulias\EmailValidator\Warning\CFWSNearAt; |
9
|
|
|
use Egulias\EmailValidator\Result\InvalidEmail; |
10
|
|
|
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT; |
11
|
|
|
use Egulias\EmailValidator\Result\Reason\UnclosedComment; |
12
|
|
|
use Egulias\EmailValidator\Result\Reason\UnOpenedComment; |
13
|
|
|
use Egulias\EmailValidator\Warning\Comment as WarningComment; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class Comment extends Parser |
17
|
|
|
{ |
18
|
|
|
private $MopenedParenthesis = 0; |
19
|
|
|
|
20
|
|
|
public function parse($str) |
21
|
|
|
{ |
22
|
|
|
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
23
|
|
|
$this->MopenedParenthesis++; |
24
|
|
|
if($this->noClosingParenthesis()) { |
25
|
|
|
return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
View Code Duplication |
if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
|
|
|
30
|
|
|
return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->warnings[WarningComment::CODE] = new WarningComment(); |
34
|
|
|
while (!$this->lexer->isNextToken(EmailLexer::S_AT)) {//!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) { |
35
|
|
|
if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) { |
36
|
|
|
$this->MopenedParenthesis++; |
37
|
|
|
} |
38
|
|
|
$this->warnEscaping(); |
39
|
|
|
if($this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) { |
40
|
|
|
$this->MopenedParenthesis--; |
41
|
|
|
} |
42
|
|
|
$this->lexer->moveNext(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if($this->MopenedParenthesis >= 1) { |
46
|
|
|
return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']); |
47
|
|
View Code Duplication |
} else if ($this->MopenedParenthesis < 0) { |
|
|
|
|
48
|
|
|
return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (!$this->lexer->isNextToken(EmailLexer::S_AT)) { |
52
|
|
|
return new InvalidEmail(new ExpectingATEXT('ATEX is not expected after closing comments'), $this->lexer->token['value']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
//You should always end at @ |
56
|
|
|
$this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); |
57
|
|
|
return new ValidEmail(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function noClosingParenthesis() : bool |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
|
|
$this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS); |
64
|
|
|
return false; |
65
|
|
|
} catch (\RuntimeException $e) { |
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
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.