Test Failed
Push — master ( e258e4...a626ba )
by Zaahid
15:25
created

AddressPart::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 1
cts 1
cp 1
cc 2
nc 2
nop 0
crap 2
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
8
namespace ZBateson\MailMimeParser\Header\Part;
9
10
use Psr\Log\LogLevel;
11
12
/**
13
 * Holds a single address or name/address pair.
14
 *
15
 * The name part of the address may be mime-encoded, but the email address part
16
 * can't be mime-encoded.  Any whitespace in the email address part is stripped
17
 * out.
18
 *
19
 * A convenience method, getEmail, is provided for clarity -- but getValue
20
 * returns the email address as well.
21
 *
22
 * @author Zaahid Bateson
23
 */
24
class AddressPart extends NameValuePart
25
{
26
    protected function getValueFromParts(array $parts) : string
27
    {
28
        return \implode(\array_map(
29
            function ($p) {
30
                if ($p instanceof AddressPart) {
31
                    return $p->getValue();
32
                } elseif ($p instanceof QuotedLiteralPart && $p->getValue() !== '') {
33 97
                    return '"' . \preg_replace('/(["\\\])/', '\\\$1', $p->getValue()) . '"';
34
                } else {
35 97
                    return \preg_replace('/\s+/', '', $p->getValue());
36 97
                }
37 97
            },
38 97
            $parts
39 97
        ));
40
    }
41 97
42
    /**
43
     * Returns the email address.
44
     *
45
     * @return string The email address.
46
     */
47
    public function getEmail() : string
48
    {
49 95
        return $this->value;
50
    }
51 95
52
    protected function validate() : void
53
    {
54 1
        if (empty($this->value)) {
55
            $this->addError('Address doesn\'t contain an email address', LogLevel::ERROR);
56 1
        }
57 1
    }
58
}
59