Passed
Push — message-id-validator ( 1cb5b2...07464f )
by Eduardo Gulias
02:11
created

MessageIDParser   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 95
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 82.93%

Importance

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