| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Egulias\EmailValidator\Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Egulias\EmailValidator\EmailLexer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Egulias\EmailValidator\EmailValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | abstract class Parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     protected $warnings = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     protected $lexer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected $openedParenthesis = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 151 |  |     public function __construct(EmailLexer $lexer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 151 |  |         $this->lexer = $lexer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 151 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 75 |  |     public function getWarnings() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 75 |  |         return $this->warnings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     abstract public function parse($str); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @return int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 9 |  |     public function getOpenedParenthesis() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 9 |  |         return $this->openedParenthesis; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * validateQuotedPair | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected function validateQuotedPair() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 37 |  | View Code Duplication |         if (!($this->lexer->token['type'] === EmailLexer::INVALID | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             || $this->lexer->token['type'] === EmailLexer::C_DEL)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             throw new \InvalidArgumentException('ERR_EXPECTING_QPAIR'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->warnings[] = EmailValidator::DEPREC_QP; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 11 |  |     protected function parseComments() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 11 |  |         $this->openedParenthesis = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 11 |  |         $this->isUnclosedComment(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 10 |  |         $this->warnings[] = EmailValidator::CFWS_COMMENT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 10 |  |         while (!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 10 |  |             if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |                 $this->openedParenthesis++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 10 |  |             $this->warnEscaping(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 10 |  |             $this->lexer->moveNext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 10 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 10 |  |         $this->lexer->moveNext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 10 |  |         if ($this->lexer->isNextTokenAny(array(EmailLexer::GENERIC, EmailLexer::S_EMPTY))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |             throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 9 |  |         if ($this->lexer->isNextToken(EmailLexer::S_AT)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |             $this->warnings[] = EmailValidator::DEPREC_CFWS_NEAR_AT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 9 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 14 |  |     protected function isUnclosedComment() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 14 |  |             $this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 12 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 2 |  |         } catch (\RuntimeException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 2 |  |             throw new \InvalidArgumentException('ERR_UNCLOSEDCOMMENT'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 18 |  |     protected function parseFWS() | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 18 |  |         $previous = $this->lexer->getPrevious(); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 18 |  |         $this->checkCRLFInFWS(); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 18 |  |         if ($this->lexer->token['type'] === EmailLexer::S_CR) { | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 2 |  |             throw new \InvalidArgumentException('ERR_CR_NO_LF'); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 16 |  |         if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type']  !== EmailLexer::S_AT) { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 7 |  |             throw new \InvalidArgumentException('ERR_ATEXT_AFTER_CFWS'); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 92 | 12 | View Code Duplication |         if ($this->lexer->token['type'] === EmailLexer::S_LF || $this->lexer->token['type'] === EmailLexer::C_NUL) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |             throw new \InvalidArgumentException('ERR_EXPECTING_CTEXT'); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 12 |  |         if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type']  === EmailLexer::S_AT) { | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 5 |  |             $this->warnings[] = EmailValidator::DEPREC_CFWS_NEAR_AT; | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 5 |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 8 |  |             $this->warnings[] = EmailValidator::CFWS_FWS; | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 101 | 12 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 138 |  |     protected function checkConsecutiveDots() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 105 | 138 | View Code Duplication |         if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_DOT)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 3 |  |             throw new \InvalidArgumentException('ERR_CONSECUTIVEDOTS'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 138 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 132 |  |     protected function isFWS() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 132 |  |         if ($this->escaped()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 132 |  |         if ($this->lexer->token['type'] === EmailLexer::S_SP || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 132 |  |             $this->lexer->token['type'] === EmailLexer::S_HTAB || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 132 |  |             $this->lexer->token['type'] === EmailLexer::S_CR || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 132 |  |             $this->lexer->token['type'] === EmailLexer::S_LF || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 132 |  |             $this->lexer->token['type'] === EmailLexer::CRLF | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 132 |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 18 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 132 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 139 |  |     protected function escaped() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 139 |  |         $previous = $this->lexer->getPrevious(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 139 |  |         if ($previous['type'] === EmailLexer::S_BACKSLASH | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 139 |  |             && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 6 |  |             $this->lexer->token['type'] !== EmailLexer::GENERIC | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 139 |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 5 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 138 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 138 |  |     protected function warnEscaping() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 138 |  |         if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 133 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 7 |  |         if ($this->lexer->isNextToken(EmailLexer::GENERIC)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 6 |  |             throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 1 |  |         if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 1 |  |         $this->warnings[] = EmailValidator::DEPREC_QP; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 1 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 148 |  |     protected function checkDQUOTE($hasClosingQuote) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 148 |  |         if ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 124 |  |             return $hasClosingQuote; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 25 |  |         if ($hasClosingQuote) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 1 |  |             return $hasClosingQuote; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 25 |  |         $previous = $this->lexer->getPrevious(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 25 |  |         if ($previous['type'] === EmailLexer::GENERIC && $this->lexer->isNextToken(EmailLexer::GENERIC)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 1 |  |             throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 24 |  |         $this->warnings[] = EmailValidator::RFC5321_QUOTEDSTRING; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 24 |  |             $this->lexer->find(EmailLexer::S_DQUOTE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 23 |  |             $hasClosingQuote = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 24 |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 3 |  |             throw new \InvalidArgumentException('ERR_UNCLOSEDQUOTEDSTR'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 23 |  |         return $hasClosingQuote; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 18 |  |     protected function checkCRLFInFWS() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 | 18 |  |         if ($this->lexer->token['type'] !== EmailLexer::CRLF) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 | 18 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         if ($this->lexer->isNextToken(EmailLexer::CRLF)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |             throw new \InvalidArgumentException('ERR_FWS_CRLF_X2'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |             throw new \InvalidArgumentException('ERR_FWS_CRLF_END'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 197 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 198 |  |  |  | 
            
                        
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.