Passed
Push — 3.x ( 451b43...8e526a )
by Eduardo Gulias
02:05
created

PartParser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 5.66 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 3
loc 53
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
parse() 0 1 ?
A getWarnings() 0 4 1
A parseFWS() 0 7 1
A checkConsecutiveDots() 3 8 3
A escaped() 0 8 3

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\Parser;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\Result\InvalidEmail;
7
use Egulias\EmailValidator\Result\Reason\ConsecutiveDot;
8
use Egulias\EmailValidator\Result\Result;
9
use Egulias\EmailValidator\Result\ValidEmail;
10
11
abstract class PartParser
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $warnings = [];
17
18
    /**
19
     * @var EmailLexer
20
     */
21
    protected $lexer;
22
23 185
    public function __construct(EmailLexer $lexer)
24
    {
25 185
        $this->lexer = $lexer;
26 185
    }
27
28
    abstract public function parse() : Result;
29
30
    /**
31
     * @return \Egulias\EmailValidator\Warning\Warning[]
32
     */
33 185
    public function getWarnings()
34
    {
35 185
        return $this->warnings;
36
    }
37
38 113
    protected function parseFWS() : Result
39
    {
40 113
        $foldingWS = new FoldingWhiteSpace($this->lexer);
41 113
        $resultFWS = $foldingWS->parse();
42 113
        $this->warnings = array_merge($this->warnings, $foldingWS->getWarnings());
43 113
        return $resultFWS;
44
    }
45
46 131
    protected function checkConsecutiveDots() : Result
47
    {
48 131 View Code Duplication
        if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
49 2
            return new InvalidEmail(new ConsecutiveDot(), $this->lexer->token['value']);
50
        }
51
52 131
        return new ValidEmail();
53
    }
54
55 173
    protected function escaped() : bool
56
    {
57 173
        $previous = $this->lexer->getPrevious();
58
59 173
        return $previous && $previous['type'] === EmailLexer::S_BACKSLASH
0 ignored issues
show
Bug Best Practice introduced by
The expression $previous of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
60
            &&
61 173
            $this->lexer->token['type'] !== EmailLexer::GENERIC;
62
    }
63
}