Code Duplication    Length = 94-95 lines in 2 locations

src/EmailParser.php 1 location

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

src/MessageIDParser.php 1 location

@@ 15-109 (lines=95) @@
12
use Egulias\EmailValidator\Warning\EmailTooLong;
13
use Egulias\EmailValidator\Result\Reason\NoLocalPart;
14
15
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
    public function __construct(EmailLexer $lexer)
31
    {
32
        $this->lexer = $lexer;
33
    }
34
35
    public function parse(string $str) : Result
36
    {
37
        $result = parent::parse($str);
38
39
        $this->addLongEmailWarning($this->localPart, $this->domainPart);
40
41
        return $result;
42
    }
43
    
44
    protected function preRightParsing(): Result
45
    {
46
        if (!$this->hasAtToken()) {
47
            return new InvalidEmail(new NoLocalPart(), $this->lexer->token["value"]);
48
        }
49
        return new ValidEmail();
50
    }
51
52
    protected function parseRightFromAt(): Result
53
    {
54
        return $this->processIDRight();
55
    }
56
57
    protected function parseLeftFromAt(): Result
58
    {
59
        return $this->processIDLeft();
60
    }
61
62
    private function processIDRight() : Result
63
    {
64
        $localPartParser = new LocalPart($this->lexer);
65
        $localPartResult = $localPartParser->parse();
66
        $this->localPart = $localPartParser->localPart();
67
        $this->warnings = array_merge($localPartParser->getWarnings(), $this->warnings);
68
69
        return $localPartResult;
70
    }
71
72
    private function processIDLeft() : Result
73
    {
74
        $domainPartParser = new IDLeftPart($this->lexer);
75
        $domainPartResult = $domainPartParser->parse();
76
        $this->domainPart = $domainPartParser->domainPart();
77
        $this->warnings = array_merge($domainPartParser->getWarnings(), $this->warnings);
78
        
79
        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
    private function hasAtToken() : bool
93
    {
94
        $this->lexer->moveNext();
95
        $this->lexer->moveNext();
96
        if ($this->lexer->token['type'] === EmailLexer::S_AT) {
97
            return false;
98
        }
99
100
        return true;
101
    }
102
103
    private function addLongEmailWarning(string $localPart, string $parsedDomainPart) : void
104
    {
105
        if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAILID_MAX_LENGTH) {
106
            $this->warnings[EmailTooLong::CODE] = new EmailTooLong();
107
        }
108
    }
109
}