| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Egulias\EmailValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Egulias\EmailValidator\Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Egulias\EmailValidator\EmailLexer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Egulias\EmailValidator\Result\Result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Egulias\EmailValidator\Parser\LocalPart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Egulias\EmailValidator\Parser\IDLeftPart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Egulias\EmailValidator\Result\ValidEmail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Egulias\EmailValidator\Result\InvalidEmail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Egulias\EmailValidator\Warning\EmailTooLong; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Egulias\EmailValidator\Result\Reason\NoLocalPart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 15 |  | View Code Duplication | class MessageIDParser extends Parser | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     const EMAILID_MAX_LENGTH = 254; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $idLeft = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $idRight = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 4 |  |     public function __construct(EmailLexer $lexer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 4 |  |         $this->lexer = $lexer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 4 |  |     public function parse(string $str) : Result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 4 |  |         $result = parent::parse($str); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 4 |  |         $this->addLongEmailWarning($this->localPart, $this->domainPart); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 4 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 4 |  |     protected function preRightParsing(): Result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 4 |  |         if (!$this->hasAtToken()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             return new InvalidEmail(new NoLocalPart(), $this->lexer->token["value"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 4 |  |         return new ValidEmail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 4 |  |     protected function parseRightFromAt(): Result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 4 |  |         return $this->processIDRight(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 4 |  |     protected function parseLeftFromAt(): Result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 4 |  |         return $this->processIDLeft(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 4 |  |     private function processIDRight() : Result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 4 |  |         $localPartParser = new LocalPart($this->lexer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 4 |  |         $localPartResult = $localPartParser->parse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 4 |  |         $this->localPart = $localPartParser->localPart(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 4 |  |         $this->warnings = array_merge($localPartParser->getWarnings(), $this->warnings); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 4 |  |         return $localPartResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 4 |  |     private function processIDLeft() : Result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 4 |  |         $domainPartParser = new IDLeftPart($this->lexer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 4 |  |         $domainPartResult = $domainPartParser->parse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 4 |  |         $this->domainPart = $domainPartParser->domainPart(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 4 |  |         $this->warnings = array_merge($domainPartParser->getWarnings(), $this->warnings); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 4 |  |         return $domainPartResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     public function getLeftPart() : string | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         return $this->idLeft; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     public function getRightPart() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         return $this->idRight; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 4 |  |     private function hasAtToken() : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 4 |  |         $this->lexer->moveNext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 4 |  |         $this->lexer->moveNext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 4 |  |         if ($this->lexer->token['type'] === EmailLexer::S_AT) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 4 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 4 |  |     private function addLongEmailWarning(string $localPart, string $parsedDomainPart) : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 4 |  |         if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAILID_MAX_LENGTH) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             $this->warnings[EmailTooLong::CODE] = new EmailTooLong(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  | } | 
            
                        
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.