Passed
Push — master ( 295c7f...61bbf4 )
by Zaahid
09:01
created

IdBaseConsumer::getPartForToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 6
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace ZBateson\MailMimeParser\Header\Consumer;
8
9
use ZBateson\MailMimeParser\Header\Part\CommentPart;
10
11
/**
12
 * Serves as a base-consumer for ID headers (like Message-ID and Content-ID).
13
 * 
14
 * IdBaseConsumer handles invalidly-formatted IDs not within '<' and '>'
15
 * characters.  Processing for validly-formatted IDs are passed on to its
16
 * sub-consumer, IdConsumer.
17
 *
18
 * @author Zaahid Bateson
19
 */
20
class IdBaseConsumer extends AbstractConsumer
21
{
22
    /**
23
     * Returns the following as sub-consumers:
24
     *  - {@see CommentConsumer}
25
     *  - {@see QuotedStringConsumer}
26
     *  - {@see IdConsumer}
27
     *
28
     * @return AbstractConsumer[] the sub-consumers
29
     */
30 29
    protected function getSubConsumers()
31
    {
32 29
        return [
33 29
            $this->consumerService->getCommentConsumer(),
34 29
            $this->consumerService->getQuotedStringConsumer(),
35 29
            $this->consumerService->getIdConsumer()
36 29
        ];
37
    }
38
    
39
    /**
40
     * Returns '\s+' as a whitespace separator.
41
     * 
42
     * @return string[] an array of regex pattern matchers.
43
     */
44 29
    protected function getTokenSeparators()
45
    {
46 29
        return ['\s+'];
47
    }
48
49
    /**
50
     * IdBaseConsumer doesn't have start/end tokens, and so always returns
51
     * false.
52
     * 
53
     * @param string $token
54
     * @return boolean false
55
     */
56 29
    protected function isEndToken($token)
57
    {
58 29
        return false;
59
    }
60
    
61
    /**
62
     * IdBaseConsumer doesn't have start/end tokens, and so always returns
63
     * false.
64
     * 
65
     * @codeCoverageIgnore
66
     * @param string $token
67
     * @return boolean false
68
     */
69
    protected function isStartToken($token)
70
    {
71
        return false;
72
    }
73
    
74
    /**
75
     * Returns null for whitespace, and LiteralPart for anything else.
76
     * 
77
     * @param string $token the token
78
     * @param bool $isLiteral set to true if the token represents a literal -
79
     *        e.g. an escaped token
80
     * @return \ZBateson\MailMimeParser\Header\IHeaderPart|null the constructed
81
     *         header part or null if the token should be ignored
82
     */
83 2
    protected function getPartForToken($token, $isLiteral)
84
    {
85 2
        if (preg_match('/^\s+$/', $token)) {
86 2
            return null;
87
        }
88
        return $this->partFactory->newLiteralPart($token);
89
    }
90
91
    /**
92
     * Overridden to filter out any found CommentPart objects.
93
     *
94
     * @param \ZBateson\MailMimeParser\Header\IHeaderPart[] $parts
95
     * @return \ZBateson\MailMimeParser\Header\IHeaderPart[]
96
     */
97 29
    protected function processParts(array $parts)
98
    {
99 29
        return array_values(array_filter($parts, function ($part) {
100 29
            if (empty($part) || $part instanceof CommentPart) {
101 2
                return false;
102
            }
103 29
            return true;
104 29
        }));
105
    }
106
}
107