Passed
Pull Request — 3.x (#348)
by
unknown
01:48
created

DomainPart::checkDomainPartExceptions()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 7.5375

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 8
nc 4
nop 2
dl 0
loc 16
ccs 7
cts 9
cp 0.7778
crap 7.5375
rs 8.8333
c 2
b 0
f 0
1
<?php
2
3
namespace Egulias\EmailValidator\Parser;
4
5
use Doctrine\Common\Lexer\Token;
6
use Egulias\EmailValidator\EmailLexer;
7
use Egulias\EmailValidator\Warning\TLD;
8
use Egulias\EmailValidator\Result\Result;
9
use Egulias\EmailValidator\Result\ValidEmail;
10
use Egulias\EmailValidator\Result\InvalidEmail;
11
use Egulias\EmailValidator\Result\Reason\DotAtEnd;
12
use Egulias\EmailValidator\Result\Reason\DotAtStart;
13
use Egulias\EmailValidator\Warning\DeprecatedComment;
14
use Egulias\EmailValidator\Result\Reason\CRLFAtTheEnd;
15
use Egulias\EmailValidator\Result\Reason\LabelTooLong;
16
use Egulias\EmailValidator\Result\Reason\NoDomainPart;
17
use Egulias\EmailValidator\Result\Reason\ConsecutiveAt;
18
use Egulias\EmailValidator\Result\Reason\DomainTooLong;
19
use Egulias\EmailValidator\Result\Reason\CharNotAllowed;
20
use Egulias\EmailValidator\Result\Reason\DomainHyphened;
21
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
22
use Egulias\EmailValidator\Parser\CommentStrategy\DomainComment;
23
use Egulias\EmailValidator\Result\Reason\ExpectingDomainLiteralClose;
24
use Egulias\EmailValidator\Parser\DomainLiteral as DomainLiteralParser;
25
26
class DomainPart extends PartParser
27
{
28
    public const DOMAIN_MAX_LENGTH = 253;
29
    public const LABEL_MAX_LENGTH = 63;
30
31
    /**
32
     * @var string
33
     */
34
    protected $domainPart = '';
35
36
    /**
37
     * @var string
38
     */
39
    protected $label = '';
40
41 148
    public function parse() : Result
42
    {
43 148
        $this->lexer->clearRecorded();
44 148
        $this->lexer->startRecording();
45
46 148
        $this->lexer->moveNext();
47
48 148
        $domainChecks = $this->performDomainStartChecks();
49 148
        if ($domainChecks->isInvalid()) {
50 12
            return $domainChecks;
51
        }
52
53 136
        if ($this->lexer->token['type'] === EmailLexer::S_AT) {
54 1
            return new InvalidEmail(new ConsecutiveAt(), $this->lexer->token['value']);
55
        }
56
57 135
        $result = $this->doParseDomainPart();
58 135
        if ($result->isInvalid()) {
59 67
            return $result;
60
        }
61
62 68
        $end = $this->checkEndOfDomain();
63 68
        if ($end->isInvalid()) {
64 4
            return $end;
65
        }
66
67 64
        $this->lexer->stopRecording();
68 64
        $this->domainPart = $this->lexer->getAccumulatedValues();
69
70 64
        $length = strlen($this->domainPart);
71 64
        if ($length > self::DOMAIN_MAX_LENGTH) {
72
            return new InvalidEmail(new DomainTooLong(), $this->lexer->token['value']);
73
        }
74
75 64
        return new ValidEmail();
76
    }
77
78 68
    private function checkEndOfDomain() : Result
79
    {
80 68
        $prev = $this->lexer->getPrevious();
81 68
        if ($prev['type'] === EmailLexer::S_DOT) {
82 3
            return new InvalidEmail(new DotAtEnd(), $this->lexer->token['value']);
83
        }
84 65
        if ($prev['type'] === EmailLexer::S_HYPHEN) {
85 1
            return new InvalidEmail(new DomainHyphened('Hypen found at the end of the domain'), $prev['value']);
86
        }
87
88 64
        if ($this->lexer->token['type'] === EmailLexer::S_SP) {
89
            return new InvalidEmail(new CRLFAtTheEnd(), $prev['value']);
90
        }
91 64
        return new ValidEmail();
92
93
    }
94
95 148
    private function performDomainStartChecks() : Result
96
    {
97 148
        $invalidTokens = $this->checkInvalidTokensAfterAT();
98 148
        if ($invalidTokens->isInvalid()) {
99 2
            return $invalidTokens;
100
        }
101
        
102 146
        $missingDomain = $this->checkEmptyDomain();
103 146
        if ($missingDomain->isInvalid()) {
104 10
            return $missingDomain;
105
        }
106
107 136
        if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
108 3
            $this->warnings[DeprecatedComment::CODE] = new DeprecatedComment();
109
        }
110 136
        return new ValidEmail();
111
    }
112
113 146
    private function checkEmptyDomain() : Result
114
    {
115 146
        $thereIsNoDomain = $this->lexer->token['type'] === EmailLexer::S_EMPTY ||
116 146
            ($this->lexer->token['type'] === EmailLexer::S_SP &&
117 146
            !$this->lexer->isNextToken(EmailLexer::GENERIC));
0 ignored issues
show
Bug introduced by
Egulias\EmailValidator\EmailLexer::GENERIC of type integer is incompatible with the type Doctrine\Common\Lexer\T expected by parameter $type of Doctrine\Common\Lexer\AbstractLexer::isNextToken(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
            !$this->lexer->isNextToken(/** @scrutinizer ignore-type */ EmailLexer::GENERIC));
Loading history...
118
119 146
        if ($thereIsNoDomain) {
120 10
            return new InvalidEmail(new NoDomainPart(), $this->lexer->token['value']);
121
        }
122
123 136
        return new ValidEmail();
124
    }
125
126 148
    private function checkInvalidTokensAfterAT() : Result
127
    {
128 148
        if ($this->lexer->token['type'] === EmailLexer::S_DOT) {
129 1
            return new InvalidEmail(new DotAtStart(), $this->lexer->token['value']);
130
        }
131 147
        if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN) {
132 1
            return new InvalidEmail(new DomainHyphened('After AT'), $this->lexer->token['value']);
133
        }
134 146
        return new ValidEmail();
135
    }
136
137 7
    protected function parseComments(): Result
138
    {
139 7
        $commentParser = new Comment($this->lexer, new DomainComment());
140 7
        $result = $commentParser->parse();
141 7
        $this->warnings = array_merge($this->warnings, $commentParser->getWarnings());
142
143 7
        return $result;
144
    }
145
146 135
    protected function doParseDomainPart() : Result
147
    {
148 135
        $tldMissing = true;
149 135
        $hasComments = false;
150 135
        $domain = '';
151
        do {
152 135
            $prev = $this->lexer->getPrevious();
153
154 135
            $notAllowedChars = $this->checkNotAllowedChars($this->lexer->token);
155 135
            if ($notAllowedChars->isInvalid()) {
156 4
                return $notAllowedChars;
157
            }
158
159 135
            if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS || 
160 135
                $this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS ) {
161 7
                $hasComments = true;
162 7
                $commentsResult = $this->parseComments();
163
164
                //Invalid comment parsing
165 7
                if($commentsResult->isInvalid()) {
166 5
                    return $commentsResult;
167
                }
168
            }
169
170 132
            $dotsResult = $this->checkConsecutiveDots();
171 132
            if ($dotsResult->isInvalid()) {
172 2
                return $dotsResult;
173
            }
174
175 132
            if ($this->lexer->token['type'] === EmailLexer::S_OPENBRACKET) {
176 18
                $literalResult = $this->parseDomainLiteral();
177
178 18
                $this->addTLDWarnings($tldMissing);
179 18
                return $literalResult;
180
            }
181
182 114
                $labelCheck = $this->checkLabelLength();
183 114
                if ($labelCheck->isInvalid()) {
184 5
                    return $labelCheck;
185
                }
186
187 114
            $FwsResult = $this->parseFWS();
188 114
            if($FwsResult->isInvalid()) {
189 5
                return $FwsResult;
190
            }
191
192 114
            $domain .= $this->lexer->token['value'];
193
194 114
            if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::GENERIC)) {
0 ignored issues
show
Bug introduced by
Egulias\EmailValidator\EmailLexer::GENERIC of type integer is incompatible with the type Doctrine\Common\Lexer\T expected by parameter $type of Doctrine\Common\Lexer\AbstractLexer::isNextToken(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

194
            if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(/** @scrutinizer ignore-type */ EmailLexer::GENERIC)) {
Loading history...
195 56
                $tldMissing = false;
196
            }
197
198 114
            $exceptionsResult = $this->checkDomainPartExceptions($prev, $hasComments);
199 114
            if ($exceptionsResult->isInvalid()) {
200 41
                return $exceptionsResult;
201
            }
202 112
            $this->lexer->moveNext();
203
204 112
        } while (null !== $this->lexer->token['type']);
205
206 55
        $labelCheck = $this->checkLabelLength(true);
207 55
        if ($labelCheck->isInvalid()) {
208 2
            return $labelCheck;
209
        }
210 53
        $this->addTLDWarnings($tldMissing);
211
212 53
        $this->domainPart = $domain;
213 53
        return new ValidEmail();
214
    }
215
216
    /**
217
     * @psalm-param array|Token<int, string> $token
218
     */
219 135
    private function checkNotAllowedChars($token) : Result
220
    {
221 135
        $notAllowed = [EmailLexer::S_BACKSLASH => true, EmailLexer::S_SLASH=> true];
222 135
        if (isset($notAllowed[$token['type']])) {
223 4
            return new InvalidEmail(new CharNotAllowed(), $token['value']);
224
        }
225 135
        return new ValidEmail();
226
    }
227
228
    /**
229
     * @return Result
230
     */
231 18
    protected function parseDomainLiteral() : Result
232
    {
233
        try {
234 18
            $this->lexer->find(EmailLexer::S_CLOSEBRACKET);
235
        } catch (\RuntimeException $e) {
236
            return new InvalidEmail(new ExpectingDomainLiteralClose(), $this->lexer->token['value']);
237
        }
238
239 18
        $domainLiteralParser = new DomainLiteralParser($this->lexer);
240 18
        $result = $domainLiteralParser->parse();
241 18
        $this->warnings = array_merge($this->warnings, $domainLiteralParser->getWarnings());
242 18
        return $result;
243
    }
244
245 114
    protected function checkDomainPartExceptions(array $prev, bool $hasComments) : Result
246
    {
247 114
        if ($this->lexer->token['type'] === EmailLexer::S_OPENBRACKET && $prev['type'] !== EmailLexer::S_AT) {
248
            return new InvalidEmail(new ExpectingATEXT('OPENBRACKET not after AT'), $this->lexer->token['value']);
249
        }
250
251 114
        if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
0 ignored issues
show
Bug introduced by
Egulias\EmailValidator\EmailLexer::S_DOT of type integer is incompatible with the type Doctrine\Common\Lexer\T expected by parameter $type of Doctrine\Common\Lexer\AbstractLexer::isNextToken(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

251
        if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN && $this->lexer->isNextToken(/** @scrutinizer ignore-type */ EmailLexer::S_DOT)) {
Loading history...
252 1
            return new InvalidEmail(new DomainHyphened('Hypen found near DOT'), $this->lexer->token['value']);
253
        }
254
255 114
        if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH
256 114
            && $this->lexer->isNextToken(EmailLexer::GENERIC)) {
257
            return new InvalidEmail(new ExpectingATEXT('Escaping following "ATOM"'), $this->lexer->token['value']);
258
        }
259
260 114
        return $this->validateTokens($hasComments);
261
    }
262
263 108
    protected function validateTokens(bool $hasComments) : Result
264
    {
265 108
        $validDomainTokens = array(
266 108
            EmailLexer::GENERIC => true,
267 108
            EmailLexer::S_HYPHEN => true,
268 108
            EmailLexer::S_DOT => true,
269 108
        );
270
271 108
        if ($hasComments) {
272 2
            $validDomainTokens[EmailLexer::S_OPENPARENTHESIS] = true;
273 2
            $validDomainTokens[EmailLexer::S_CLOSEPARENTHESIS] = true;
274
        }
275
276 108
        if (!isset($validDomainTokens[$this->lexer->token['type']])) {
277 40
            return new InvalidEmail(new ExpectingATEXT('Invalid token in domain: ' . $this->lexer->token['value']), $this->lexer->token['value']);
278
        }
279
280 106
        return new ValidEmail();
281
    }
282
283 114
    private function checkLabelLength(bool $isEndOfDomain = false) : Result
284
    {
285 114
        if ($this->lexer->token['type'] === EmailLexer::S_DOT || $isEndOfDomain) {
286 72
            if ($this->isLabelTooLong($this->label)) {
287 7
                return new InvalidEmail(new LabelTooLong(), $this->lexer->token['value']);
288
            }
289 66
            $this->label = '';
290
        }
291 114
        $this->label .= $this->lexer->token['value'];
292 114
        return new ValidEmail();
293
    }
294
295
296 72
    private function isLabelTooLong(string $label) : bool
297
    {
298 72
        if (preg_match('/[^\x00-\x7F]/', $label)) {
299 10
            idn_to_ascii($label, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, $idnaInfo);
300 10
            return (bool) ($idnaInfo['errors'] & IDNA_ERROR_LABEL_TOO_LONG);
301
        }
302 66
        return strlen($label) > self::LABEL_MAX_LENGTH;
303
    }
304
305 71
    private function addTLDWarnings(bool $isTLDMissing) : void
306
    {
307 71
        if ($isTLDMissing) {
308 26
            $this->warnings[TLD::CODE] = new TLD();
309
        }
310
    }
311
312 148
    public function domainPart() : string
313
    {
314 148
        return $this->domainPart;
315
    }
316
}
317