Passed
Pull Request — master (#129)
by
unknown
03:02
created

DatePart::__construct()   B

Complexity

Conditions 8
Paths 27

Size

Total Lines 31
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8.2964

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 14
c 1
b 0
f 0
nc 27
nop 2
dl 0
loc 31
ccs 10
cts 12
cp 0.8333
crap 8.2964
rs 8.4444
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 2
        $dateToken = trim($token);
36
        // parent::__construct converts character encoding -- may cause problems sometimes.
37 2
        parent::__construct($charsetConverter, $dateToken);
38
39
        // Missing "+" in timezone definition. eg: Thu, 13 Mar 2014 15:02:47 0000 (not RFC compliant)
40
        // Won't result in an Exception, but in a valid DateTime in year `0000` - therefore we need to check this first:
41 2
        if (preg_match('# [0-9]{4}$#', $dateToken)) {
42
            try {
43 1
                $this->date = new DateTime(preg_replace('# ([0-9]{4})$#', ' +$1', $dateToken));
44
                // $this->addParsingError('Invalid Date: "+/-" missing before timezone definition');
45
            } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
46
            }
47
        }
48
49 2
        if (!isset($this->date))
50
        {
51
            try {
52 2
                $this->date = new DateTime($dateToken);
53 2
            } 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
        }
56
57
        // @see https://bugs.php.net/bug.php?id=42486
58
        // TODO: Test is missing:
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
59 2
        if (!isset($this->date) && preg_match('#UT$#', $dateToken)) {
60
            try {
61 1
                $this->date = new DateTime($dateToken . 'C');
62
                // $this->addParsingError('Invalid Date: "C" missing from timezone "UT"'); // https://github.com/zbateson/mail-mime-parser/issues/124
63
            } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
64
            }
65
        }
66 2
    }
67
68
    /**
69
     * Parse date string token
70
     * @param string $dateToken Date token as string
71
     *
72
     * @return \DateTime|false Returns \DateTime or false on failure.
73
     */
74
    private function parseDateToken($dateToken)
0 ignored issues
show
Unused Code introduced by
The method parseDateToken() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
75
    {
76
77
        // First check as RFC822 which allows only 2-digit years
78
        $date = DateTime::createFromFormat(DateTime::RFC822, $dateToken);
79
        if ($date === false) {
80
            $date = DateTime::createFromFormat(DateTime::RFC2822, $dateToken);
81
        }
82
83
        return $date;
84
    }
85
86
    /**
87
     * Returns a DateTime object or false if it can't be parsed.
88
     *
89
     * @return DateTime
90
     */
91 2
    public function getDateTime()
92
    {
93 2
        return $this->date;
94
    }
95
}
96