Passed
Push — message-id-validator ( ab2232...8edbe4 )
by Eduardo Gulias
04:06 queued 02:03
created

MessageIDParser   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 79
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 81.25%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 8
dl 79
loc 79
ccs 26
cts 32
cp 0.8125
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A parseRightFromAt() 4 4 1
A parse() 8 8 1
A preLeftParsing() 7 7 2
A parseLeftFromAt() 4 4 1
A processIDLeft() 9 9 1
A processIDRight() 9 9 1
A getLeftPart() 4 4 1
A getRightPart() 4 4 1
A addLongEmailWarning() 6 6 2

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\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
}