| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Egulias\EmailValidator\Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Egulias\EmailValidator\EmailLexer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Egulias\EmailValidator\EmailValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class LocalPart extends Parser | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 10 | 150 |  |     public function parse($localPart) | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 12 | 150 |  |         $parseDQuote = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 13 | 150 |  |         $closingQuote = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 | 150 |  |         $openedParenthesis = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 | 150 |  |         while ($this->lexer->token['type'] !== EmailLexer::S_AT && $this->lexer->token) { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 | 150 |  |             if ($this->lexer->token['type'] === EmailLexer::S_DOT && !$this->lexer->getPrevious()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 | 2 |  |                 throw new \InvalidArgumentException('ERR_DOT_START'); | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 148 |  |             $closingQuote = $this->checkDQUOTE($closingQuote); | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 147 |  |             if ($closingQuote && $parseDQuote) { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 23 |  |                 $parseDQuote = $this->parseDoubleQuote(); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 15 |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 139 |  |             if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 7 |  |                 $this->parseComments(); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 5 |  |                 $openedParenthesis += $this->getOpenedParenthesis(); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 5 |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 138 |  |             if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 6 |  |                 if ($openedParenthesis === 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 2 |  |                     throw new \InvalidArgumentException('ERR_UNOPENEDCOMMENT'); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |                 } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 5 |  |                     $openedParenthesis--; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 5 |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 138 |  |             $this->checkConsecutiveDots(); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 40 | 138 | View Code Duplication |             if ($this->lexer->token['type'] === EmailLexer::S_DOT && | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 3 |  |                 $this->lexer->isNextToken(EmailLexer::S_AT) | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 138 |  |             ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 2 |  |                 throw new \InvalidArgumentException('ERR_DOT_END'); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 138 |  |             $this->warnEscaping(); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 133 |  |             $this->isInvalidToken($this->lexer->token, $closingQuote); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 132 |  |             if ($this->isFWS()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 8 |  |                 $this->parseFWS(); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 4 |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 132 |  |             $this->lexer->moveNext(); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 132 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 115 |  |         $prev = $this->lexer->getPrevious(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 115 |  |         if (strlen($prev['value']) > EmailValidator::RFC5322_LOCAL_TOOLONG) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 2 |  |             $this->warnings[] = EmailValidator::RFC5322_LOCAL_TOOLONG; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 2 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 115 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 23 |  |     protected function parseDoubleQuote() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 23 |  |         $parseAgain = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $special = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 23 |  |             EmailLexer::S_CR => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 23 |  |             EmailLexer::S_HTAB => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 23 |  |             EmailLexer::S_LF => true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 23 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $invalid = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 23 |  |             EmailLexer::C_NUL => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 23 |  |             EmailLexer::S_HTAB => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 23 |  |             EmailLexer::S_CR => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 23 |  |             EmailLexer::S_LF => true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 23 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 23 |  |         $setSpecialsWarning = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 23 |  |         $this->lexer->moveNext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 23 |  |         while ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE && $this->lexer->token) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 21 |  |             $parseAgain = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 21 |  |             if (isset($special[$this->lexer->token['type']]) && $setSpecialsWarning) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 4 |  |                 $this->warnings[] = EmailValidator::CFWS_FWS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 4 |  |                 $setSpecialsWarning = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 4 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 21 |  |             $this->lexer->moveNext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 21 |  |             if (!$this->escaped() && isset($invalid[$this->lexer->token['type']])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 throw new \InvalidArgumentException('ERR_EXPECTED_ATEXT'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 21 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 23 |  |         $prev = $this->lexer->getPrevious(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 23 |  |         if ($prev['type'] === EmailLexer::S_BACKSLASH) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 3 |  |             if (!$this->checkDQUOTE(false)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 throw new \InvalidArgumentException('ERR_UNCLOSED_DQUOTE'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 21 |  |         if (!$this->lexer->isNextToken(EmailLexer::S_AT) && $prev['type'] !== EmailLexer::S_BACKSLASH) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 6 |  |             throw new \InvalidArgumentException('ERR_EXPECED_AT'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 15 |  |         return $parseAgain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 133 |  |     protected function isInvalidToken($token, $closingQuote) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $forbidden = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 133 |  |             EmailLexer::S_COMMA, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 133 |  |             EmailLexer::S_CLOSEBRACKET, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 133 |  |             EmailLexer::S_OPENBRACKET, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 133 |  |             EmailLexer::S_GREATERTHAN, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 133 |  |             EmailLexer::S_LOWERTHAN, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 133 |  |             EmailLexer::S_COLON, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 133 |  |             EmailLexer::S_SEMICOLON, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             EmailLexer::INVALID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 133 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 133 |  |         if (in_array($token['type'], $forbidden) && !$closingQuote) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 4 |  |             throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 132 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 127 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |  | 
            
                        
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.