Passed
Push — 3.x ( 4cccb8...037594 )
by Eduardo Gulias
02:37
created

PartParser::escaped()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 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 PartParser
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $warnings = [];
17
18
    /**
19
     * @var EmailLexer
20
     */
21
    protected $lexer;
22
23 186
    public function __construct(EmailLexer $lexer)
24
    {
25 186
        $this->lexer = $lexer;
26 186
    }
27
28
    abstract public function parse() : Result;
29
30
    /**
31
     * @return \Egulias\EmailValidator\Warning\Warning[]
32
     */
33 186
    public function getWarnings()
34
    {
35 186
        return $this->warnings;
36
    }
37
38 114
    protected function parseFWS() : Result
39
    {
40 114
        $foldingWS = new FoldingWhiteSpace($this->lexer);
41 114
        $resultFWS = $foldingWS->parse();
42 114
        $this->warnings = array_merge($this->warnings, $foldingWS->getWarnings());
43 114
        return $resultFWS;
44
    }
45
46 132
    protected function checkConsecutiveDots() : Result
47
    {
48 132
        if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
49 2
            return new InvalidEmail(new ConsecutiveDot(), $this->lexer->token['value']);
50
        }
51
52 132
        return new ValidEmail();
53
    }
54
55 174
    protected function escaped() : bool
56
    {
57 174
        $previous = $this->lexer->getPrevious();
58
59 174
        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 174
            $this->lexer->token['type'] !== EmailLexer::GENERIC;
62
    }
63
}