Passed
Pull Request — master (#129)
by
unknown
05:20 queued 01:55
created

DatePart::__construct()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 10
c 0
b 0
f 0
nc 8
nop 2
dl 0
loc 21
ccs 10
cts 10
cp 1
crap 6
rs 9.2222
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\Part;
8
9
use ZBateson\MbWrapper\MbWrapper;
10
use DateTime;
11
use Exception;
12
13
/**
14
 * Parses a header into a DateTime object.
15
 *
16
 * @author Zaahid Bateson
17
 */
18
class DatePart extends LiteralPart
19
{
20
    /**
21
     * @var DateTime the parsed date, or null if the date could not be parsed
22
     */
23
    protected $date;
24
25
    /**
26
     * Tries parsing the passed token as an RFC 2822 date, and failing that into
27
     * an RFC 822 date, and failing that, tries to parse it by calling
28
     * ``` new DateTime($value) ```.
29
     *
30
     * @param MbWrapper $charsetConverter
31
     * @param string $token
32
     */
33 2
    public function __construct(MbWrapper $charsetConverter, $token) {
34
35
        // parent::__construct converts character encoding -- may cause problems
36
        // sometimes.
37 2
        $dateToken = trim($token);
38 2
        parent::__construct($charsetConverter, $dateToken);
39
40 2
        $date = $this->parseDateToken($dateToken);
41
42
        // @see https://bugs.php.net/bug.php?id=42486
43 2
        if ($date === false && preg_match('#UT$#', $dateToken)) {
44 1
            $date = $this->parseDateToken($dateToken . 'C');
45
        }
46
        // Missing "+" in timezone definition. eg: Thu, 13 Mar 2014 15:02:47 0000 (not RFC compliant)
47 2
        if (preg_match('# [0-9]{4}$#', $dateToken)) {
48 1
            $date = $this->parseDateToken(preg_replace('# ([0-9]{4})$#', ' +$1', $dateToken));
49
        }
50
51
        try {
52 2
            $this->date = ($date) ?: new DateTime($dateToken);
53 1
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
54
        }
55 2
    }
56
57
    /**
58
     * Parse date string token
59
     * @param string $dateToken Date token as string
60
     *
61
     * @return \DateTime|false Returns \DateTime or false on failure.
62
     */
63 2
    private function parseDateToken($dateToken)
64
    {
65
        try {
66 2
            $date = new DateTime($dateToken);
67 2
        } catch (Exception $e) {
68 2
            return false;
69
        }
70 1
        return $date;
71
    
72
        // First check as RFC822 which allows only 2-digit years
73
        $date = DateTime::createFromFormat(DateTime::RFC822, $dateToken);
0 ignored issues
show
Unused Code introduced by
$date = DateTime::create...me::RFC822, $dateToken) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
74
        if ($date === false) {
75
            $date = DateTime::createFromFormat(DateTime::RFC2822, $dateToken);
76
        }
77
78
        return $date;
79
    }
80
81
    /**
82
     * Returns a DateTime object or false if it can't be parsed.
83
     *
84
     * @return DateTime
85
     */
86 2
    public function getDateTime()
87
    {
88 2
        return $this->date;
89
    }
90
}
91