1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Egulias\EmailValidator\Parser; |
4
|
|
|
|
5
|
|
|
use Egulias\EmailValidator\EmailLexer; |
6
|
|
|
use Egulias\EmailValidator\Result\ValidEmail; |
7
|
|
|
use Egulias\EmailValidator\Warning\CFWSNearAt; |
8
|
|
|
use Egulias\EmailValidator\Result\InvalidEmail; |
9
|
|
|
use Egulias\EmailValidator\Parser\CommentStrategy; |
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
|
|
|
class Comment extends Parser |
16
|
|
|
{ |
17
|
|
|
//change to private when removed from parent parser |
18
|
|
|
protected $openedParenthesis = 0; |
19
|
|
|
private $commentStrategy; |
20
|
|
|
|
21
|
|
|
public function __construct(EmailLexer $lexer, CommentStrategy $commentStrategy) |
22
|
|
|
{ |
23
|
|
|
$this->lexer = $lexer; |
24
|
|
|
$this->commentStrategy = $commentStrategy; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function parse($str) |
28
|
|
|
{ |
29
|
|
|
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
30
|
|
|
$this->openedParenthesis++; |
31
|
|
|
if($this->noClosingParenthesis()) { |
32
|
|
|
return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
View Code Duplication |
if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
|
|
|
37
|
|
|
return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->warnings[WarningComment::CODE] = new WarningComment(); |
41
|
|
|
|
42
|
|
|
$moreTokens = true; |
43
|
|
|
while ($this->commentStrategy->exitCondition($this->lexer, $this->openedParenthesis) && $moreTokens){ |
44
|
|
|
|
45
|
|
|
if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) { |
46
|
|
|
$this->openedParenthesis++; |
47
|
|
|
} |
48
|
|
|
$this->warnEscaping(); |
49
|
|
|
if($this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) { |
50
|
|
|
$this->openedParenthesis--; |
51
|
|
|
} |
52
|
|
|
$moreTokens = $this->lexer->moveNext(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if($this->openedParenthesis >= 1) { |
56
|
|
|
return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']); |
57
|
|
View Code Duplication |
} else if ($this->openedParenthesis < 0) { |
|
|
|
|
58
|
|
|
return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$finalValidations = $this->commentStrategy->endOfLoopValidations($this->lexer); |
62
|
|
|
|
63
|
|
|
$this->warnings = array_merge($this->warnings, $this->commentStrategy->getWarnings()); |
|
|
|
|
64
|
|
|
|
65
|
|
|
return $finalValidations; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function noClosingParenthesis() : bool |
69
|
|
|
{ |
70
|
|
|
try { |
71
|
|
|
$this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS); |
72
|
|
|
return false; |
73
|
|
|
} catch (\RuntimeException $e) { |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
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.