Test Setup Failed
Push — 3.0.0-dev ( e2e234...b76c11 )
by Eduardo Gulias
01:56
created

Parser::warnEscaping()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 3
Ratio 15 %

Importance

Changes 0
Metric Value
dl 3
loc 20
rs 9.6
c 0
b 0
f 0
cc 4
nc 4
nop 0
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 Parser
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $warnings = [];
17
18
    /**
19
     * @var EmailLexer
20
     */
21
    protected $lexer;
22
23
    public function __construct(EmailLexer $lexer)
24
    {
25
        $this->lexer = $lexer;
26
    }
27
28
    /**
29
     * @return \Egulias\EmailValidator\Warning\Warning[]
30
     */
31
    public function getWarnings()
32
    {
33
        return $this->warnings;
34
    }
35
36
    abstract public function parse() : Result;
37
38 View Code Duplication
    protected function parseFWS() : Result
0 ignored issues
show
Duplication introduced by
This method 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...
39
    {
40
        $foldingWS = new FoldingWhiteSpace($this->lexer);
41
        $resultFWS = $foldingWS->parse();
42
        $this->warnings = array_merge($this->warnings, $foldingWS->getWarnings());
43
        return $resultFWS;
44
    }
45
46
    protected function checkConsecutiveDots() : Result
47
    {
48 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
            return new InvalidEmail(new ConsecutiveDot(), $this->lexer->token['value']);
50
        }
51
52
        return new ValidEmail();
53
    }
54
55
    /**
56
     * @return bool
57
     */
58
    protected function escaped() : bool
59
    {
60
        $previous = $this->lexer->getPrevious();
61
62
        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...
63
            &&
64
            $this->lexer->token['type'] !== EmailLexer::GENERIC;
65
    }
66
}