Completed
Pull Request — 1.x (#5)
by Dorian
01:32
created

DateTimeParser::parse()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.9811
c 0
b 0
f 0
cc 7
eloc 13
nc 9
nop 1
1
<?php
2
namespace MetaHydrator\Parser;
3
4
use MetaHydrator\Exception\ParsingException;
5
6
/**
7
 * An implementation of ParserInterface used to reconstruct DateTime objects from a string
8
 */
9
class DateTimeParser extends AbstractParser
10
{
11
    /** @var null|string */
12
    private $format;
13
    public function getFormat() { return $this->format; }
14
    public function setFormat(string $format) { $this->format = $format; }
15
16
    /** @var bool */
17
    private $ignoreWarnings;
18
    public function getIgnoreWarnings() { return $this->ignoreWarnings; }
19
    public function setIgnoreWarnings(bool $ignoreWarnings) { $this->ignoreWarnings = $ignoreWarnings; }
20
21
    /**
22
     * DateTimeParser constructor.
23
     * @param string|null $format
24
     * @param bool $ignoreWarnings
25
     * @param string $errorMessage
26
     */
27
    public function __construct(string $format = null, bool $ignoreWarnings = false, string $errorMessage = "")
28
    {
29
        parent::__construct($errorMessage);
30
        $this->format = $format;
31
        $this->ignoreWarnings = $ignoreWarnings;
32
    }
33
34
    /**
35
     * @param mixed $rawValue
36
     * @return \DateTime
37
     * @throws ParsingException
38
     */
39
    public function parse($rawValue)
40
    {
41
        if ($rawValue === null || $rawValue === '') {
42
            return null;
43
        }
44
45
        if ($this->format) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->format of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
46
            $date = \DateTime::createFromFormat($this->format, $rawValue);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression \DateTime::createFromFor...is->format, $rawValue); of type DateTime|false adds false to the return on line 59 which is incompatible with the return type documented by MetaHydrator\Parser\DateTimeParser::parse of type DateTime|null. It seems like you forgot to handle an error condition.
Loading history...
47
        } else {
48
            $date = new \DateTime($rawValue);
49
        }
50
51
        $errors = \DateTime::getLastErrors();
52
        if ($errors['error_count'] > 0) {
53
            $this->throw();
54
        }
55
        if (!$this->ignoreWarnings && $errors['warning_count'] > 0) {
56
            $this->throw();
57
        }
58
59
        return $date;
60
    }
61
}
62