Passed
Push — 3.x ( 451b43...8e526a )
by Eduardo Gulias
02:05
created

MessageIDParser::preLeftParsing()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 7
loc 7
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0625
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\IDLeftPart;
9
use Egulias\EmailValidator\Parser\IDRightPart;
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
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
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 9
    public function parse(string $str) : Result
31
    {
32 9
        $result = parent::parse($str);
33
34 9
        $this->addLongEmailWarning($this->idLeft, $this->idRight);
35
36 9
        return $result;
37
    }
38
    
39 9
    protected function preLeftParsing(): Result
40
    {
41 9
        if (!$this->hasAtToken()) {
42
            return new InvalidEmail(new NoLocalPart(), $this->lexer->token["value"]);
43
        }
44 9
        return new ValidEmail();
45
    }
46
47 9
    protected function parseLeftFromAt(): Result
48
    {
49 9
        return $this->processIDLeft();
50
    }
51
52 7
    protected function parseRightFromAt(): Result
53
    {
54 7
        return $this->processIDRight();
55
    }
56
57 9
    private function processIDLeft() : Result
58
    {
59 9
        $localPartParser = new IDLeftPart($this->lexer);
60 9
        $localPartResult = $localPartParser->parse();
61 9
        $this->idLeft = $localPartParser->localPart();
62 9
        $this->warnings = array_merge($localPartParser->getWarnings(), $this->warnings);
63
64 9
        return $localPartResult;
65
    }
66
67 7
    private function processIDRight() : Result
68
    {
69 7
        $domainPartParser = new IDRightPart($this->lexer);
70 7
        $domainPartResult = $domainPartParser->parse();
71 7
        $this->idRight = $domainPartParser->domainPart();
72 7
        $this->warnings = array_merge($domainPartParser->getWarnings(), $this->warnings);
73
        
74 7
        return $domainPartResult;
75
    }
76
77
    public function getLeftPart() : string
78
    {
79
        return $this->idLeft;
80
    }
81
82
    public function getRightPart() : string
83
    {
84
        return $this->idRight;
85
    }
86
87 9
    private function addLongEmailWarning(string $localPart, string $parsedDomainPart) : void
88
    {
89 9
        if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAILID_MAX_LENGTH) {
90
            $this->warnings[EmailTooLong::CODE] = new EmailTooLong();
91
        }
92
    }
93
}