Test Setup Failed
Push — 3.0.0-dev ( 7e0ee1...302b98 )
by Eduardo Gulias
01:56
created

FoldingWhiteSpace::isFWS()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.2222
c 0
b 0
f 0
cc 6
nc 6
nop 0
1
<?php
2
namespace Egulias\EmailValidator\Parser;
3
4
use Egulias\EmailValidator\EmailLexer;
5
use Egulias\EmailValidator\Warning\CFWSNearAt;
6
use Egulias\EmailValidator\Result\InvalidEmail;
7
use Egulias\EmailValidator\Warning\CFWSWithFWS;
8
use Egulias\EmailValidator\Result\Reason\CRNoLF;
9
use Egulias\EmailValidator\Result\Reason\AtextAfterCFWS;
10
use Egulias\EmailValidator\Result\Reason\CRLFAtTheEnd;
11
use Egulias\EmailValidator\Result\Reason\CRLFX2;
12
use Egulias\EmailValidator\Result\Reason\ExpectingCTEXT;
13
use Egulias\EmailValidator\Result\ValidEmail;
14
15
class  FoldingWhiteSpace extends Parser
16
{
17
    public function parse($str)
18
    {
19
        if (!$this->isFWS()) {
20
            return new ValidEmail();
21
        }
22
23
        $previous = $this->lexer->getPrevious();
24
25
        $this->checkCRLFInFWS();
26
27 View Code Duplication
        if ($this->lexer->token['type'] === EmailLexer::S_CR) {
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...
28
            return new InvalidEmail(new CRNoLF(), $this->lexer->token['value']);
29
        }
30
31 View Code Duplication
        if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type']  !== EmailLexer::S_AT) {
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...
32
            return new InvalidEmail(new AtextAfterCFWS(), $this->lexer->token['value']);
33
        }
34
35 View Code Duplication
        if ($this->lexer->token['type'] === EmailLexer::S_LF || $this->lexer->token['type'] === EmailLexer::C_NUL) {
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...
36
            return new InvalidEmail(new ExpectingCTEXT(), $this->lexer->token['value']);
37
        }
38
39 View Code Duplication
        if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type']  === EmailLexer::S_AT) {
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...
40
            $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt();
41
        } else {
42
            $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
43
        }
44
45
        return new ValidEmail();
46
    }
47
48
    protected function checkCRLFInFWS()
49
    {
50
        if ($this->lexer->token['type'] !== EmailLexer::CRLF) {
51
            return new ValidEmail();
52
        }
53
54 View Code Duplication
        if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) {
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...
55
            return new InvalidEmail(new CRLFX2(), $this->lexer->token['value']);
56
        }
57
58
        //this has no coverage. Condition is repeated from above one
59 View Code Duplication
        if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) {
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...
60
            return new InvalidEmail(new CRLFAtTheEnd(), $this->lexer->token['value']);
61
        }
62
    }
63
     
64
    /**
65
     * @return bool
66
     */
67 View Code Duplication
    protected function isFWS()
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...
68
    {
69
        if ($this->escaped()) {
70
            return false;
71
        }
72
73
        return $this->lexer->token['type'] === EmailLexer::S_SP ||
74
            $this->lexer->token['type'] === EmailLexer::S_HTAB ||
75
            $this->lexer->token['type'] === EmailLexer::S_CR ||
76
            $this->lexer->token['type'] === EmailLexer::S_LF ||
77
            $this->lexer->token['type'] === EmailLexer::CRLF;
78
    }
79
80
    /**
81
     * @return bool
82
     */
83 View Code Duplication
    protected function escaped()
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...
84
    {
85
        $previous = $this->lexer->getPrevious();
86
87
        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...
88
            &&
89
            $this->lexer->token['type'] !== EmailLexer::GENERIC;
90
    }
91
92
}