Passed
Push — message-id-validator ( 07464f...b79cd8 )
by Eduardo Gulias
01:46
created

EmailParser   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 94
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 97.56%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 8
dl 94
loc 94
ccs 40
cts 41
cp 0.9756
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A parse() 8 8 1
A processLocalPart() 9 9 1
A processDomainPart() 9 9 1
A getDomainPart() 4 4 1
A getLocalPart() 4 4 1
A hasAtToken() 10 10 2
A addLongEmailWarning() 6 6 2
A preLeftParsing() 7 7 2
A parseLeftFromAt() 4 4 1
A parseRightFromAt() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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