Passed
Push — message-id-validator ( 07464f...b79cd8 )
by Eduardo Gulias
01:46
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 getLeftPart() 4 4 1
A getRightPart() 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
A processIDLeft() 9 9 1
A processIDRight() 9 9 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\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
}