| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Egulias\EmailValidator\Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Egulias\EmailValidator\EmailLexer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Egulias\EmailValidator\Result\Result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Egulias\EmailValidator\Result\ValidEmail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Egulias\EmailValidator\Result\InvalidEmail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Egulias\EmailValidator\Warning\LocalTooLong; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Egulias\EmailValidator\Result\Reason\DotAtEnd; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Egulias\EmailValidator\Result\Reason\DotAtStart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Egulias\EmailValidator\Result\Reason\ConsecutiveDot; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Egulias\EmailValidator\Result\Reason\ExpectingATEXT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Egulias\EmailValidator\Parser\CommentStrategy\LocalComment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class LocalPart extends Parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $localPart = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 180 |  |     public function parse() : Result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 180 |  |         $this->lexer->startRecording(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 180 |  |         while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 180 |  |             if ($this->hasDotAtStart()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 2 |  |                 return new InvalidEmail(new DotAtStart(), $this->lexer->token['value']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 178 |  |             if ($this->lexer->token['type'] === EmailLexer::S_DQUOTE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 23 |  |                 $dquoteParsingResult = $this->parseDoubleQuote(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 //Invalid double quote parsing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 23 |  |                 if($dquoteParsingResult->isInvalid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 10 |  |                     return $dquoteParsingResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 42 | 169 | View Code Duplication |             if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS ||  | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 169 |  |                 $this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 8 |  |                 $commentsResult = $this->parseComments(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 //Invalid comment parsing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 8 |  |                 if($commentsResult->isInvalid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 5 |  |                     return $commentsResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 52 | 167 | View Code Duplication |             if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_DOT)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |                 return new InvalidEmail(new ConsecutiveDot(), $this->lexer->token['value']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 56 | 167 | View Code Duplication |             if ($this->lexer->token['type'] === EmailLexer::S_DOT && | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 167 |  |                 $this->lexer->isNextToken(EmailLexer::S_AT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2 |  |                 return new InvalidEmail(new DotAtEnd(), $this->lexer->token['value']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 167 |  |             $resultEscaping = $this->validateEscaping(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 167 |  |             if ($resultEscaping->isInvalid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 6 |  |                 return $resultEscaping; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 162 |  |             $resultToken = $this->validateTokens(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 162 |  |             if ($resultToken->isInvalid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 4 |  |                 return $resultToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 161 |  |             $resultFWS = $this->parseLocalFWS(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 161 |  |             if($resultFWS->isInvalid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 6 |  |                 return $resultFWS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 161 |  |             $this->lexer->moveNext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 144 |  |         $this->lexer->stopRecording(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 144 |  |         $this->localPart = rtrim($this->lexer->getAccumulatedValues(), '@'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 144 |  |         if (strlen($this->localPart) > LocalTooLong::LOCAL_PART_LENGTH) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 2 |  |             $this->warnings[LocalTooLong::CODE] = new LocalTooLong(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 144 |  |         return new ValidEmail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 162 |  |     protected function validateTokens(bool $hasComments) : Result | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         $invalidTokens = array( | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 162 |  |             EmailLexer::S_COMMA => EmailLexer::S_COMMA, | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |             EmailLexer::S_CLOSEBRACKET => EmailLexer::S_CLOSEBRACKET, | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |             EmailLexer::S_OPENBRACKET => EmailLexer::S_OPENBRACKET, | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             EmailLexer::S_GREATERTHAN => EmailLexer::S_GREATERTHAN, | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |             EmailLexer::S_LOWERTHAN => EmailLexer::S_LOWERTHAN, | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             EmailLexer::S_COLON => EmailLexer::S_COLON, | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             EmailLexer::S_SEMICOLON => EmailLexer::S_SEMICOLON, | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             EmailLexer::INVALID => EmailLexer::INVALID | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 | 162 |  |         if (isset($invalidTokens[$this->lexer->token['type']])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 | 4 |  |             return new InvalidEmail(new ExpectingATEXT('Invalid token found'), $this->lexer->token['value']); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 | 161 |  |         return new ValidEmail(); | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 180 |  |     public function localPart() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 180 |  |         return $this->localPart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 161 |  |     private function parseLocalFWS() : Result  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 161 |  |         $foldingWS = new FoldingWhiteSpace($this->lexer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 161 |  |         $resultFWS = $foldingWS->parse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 161 |  |         if ($resultFWS->isValid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 161 |  |             $this->warnings = array_merge($this->warnings, $foldingWS->getWarnings()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 161 |  |         return $resultFWS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 180 |  |     private function hasDotAtStart() : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 180 |  |             return $this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 23 |  |     private function parseDoubleQuote() : Result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 23 |  |         $dquoteParser = new DoubleQuote($this->lexer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 23 |  |         $parseAgain = $dquoteParser->parse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 23 |  |         $this->warnings = array_merge($this->warnings, $dquoteParser->getWarnings()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 23 |  |         return $parseAgain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 8 |  |     private function parseComments(): Result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 8 |  |         $commentParser = new Comment($this->lexer, new LocalComment()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 8 |  |         $result = $commentParser->parse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 8 |  |         $this->warnings = array_merge($this->warnings, $commentParser->getWarnings()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 8 |  |         if($result->isInvalid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 5 |  |             return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 3 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 167 |  |     private function validateEscaping() : Result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         //Backslash found | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 167 |  |         if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 162 |  |             return new ValidEmail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 7 |  |         if ($this->lexer->isNextToken(EmailLexer::GENERIC)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 6 |  |             return new InvalidEmail(new ExpectingATEXT('Found ATOM after escaping'), $this->lexer->token['value']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 158 | 1 | View Code Duplication |         if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             return new ValidEmail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 1 |  |         return new ValidEmail(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 164 |  |  | } | 
            
                        
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.