Passed
Push — message-id-validator ( 07464f...b79cd8 )
by Eduardo Gulias
01:46
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\LocalPart;
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 7
    public function __construct(EmailLexer $lexer)
31
    {
32 7
        $this->lexer = $lexer;
33 7
    }
34
35 7
    public function parse(string $str) : Result
36
    {
37 7
        $result = parent::parse($str);
38
39 7
        $this->addLongEmailWarning($this->idLeft, $this->idRight);
40
41 7
        return $result;
42
    }
43
    
44 7
    protected function preLeftParsing(): Result
45
    {
46 7
        if (!$this->hasAtToken()) {
47
            return new InvalidEmail(new NoLocalPart(), $this->lexer->token["value"]);
48
        }
49 7
        return new ValidEmail();
50
    }
51
52 7
    protected function parseLeftFromAt(): Result
53
    {
54 7
        return $this->processIDLeft();
55
    }
56
57 7
    protected function parseRightFromAt(): Result
58
    {
59 7
        return $this->processIDRight();
60
    }
61
62 7
    private function processIDLeft() : Result
63
    {
64 7
        $localPartParser = new LocalPart($this->lexer);
65 7
        $localPartResult = $localPartParser->parse();
66 7
        $this->localPart = $localPartParser->localPart();
0 ignored issues
show
Bug introduced by
The property localPart does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
67 7
        $this->warnings = array_merge($localPartParser->getWarnings(), $this->warnings);
68
69 7
        return $localPartResult;
70
    }
71
72 7
    private function processIDRight() : Result
73
    {
74 7
        $domainPartParser = new IDRightPart($this->lexer);
75 7
        $domainPartResult = $domainPartParser->parse();
76 7
        $this->domainPart = $domainPartParser->domainPart();
0 ignored issues
show
Bug introduced by
The property domainPart does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
77 7
        $this->warnings = array_merge($domainPartParser->getWarnings(), $this->warnings);
78
        
79 7
        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 7
    private function hasAtToken() : bool
93
    {
94 7
        $this->lexer->moveNext();
95 7
        $this->lexer->moveNext();
96 7
        if ($this->lexer->token['type'] === EmailLexer::S_AT) {
97
            return false;
98
        }
99
100 7
        return true;
101
    }
102
103 7
    private function addLongEmailWarning(string $localPart, string $parsedDomainPart) : void
104
    {
105 7
        if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAILID_MAX_LENGTH) {
106
            $this->warnings[EmailTooLong::CODE] = new EmailTooLong();
107
        }
108
    }
109
}