Completed
Push — 1.x ( 2692eb...201d44 )
by Dorian
12s
created

DateTimeParser   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 53
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormat() 0 1 1
A setFormat() 0 1 1
A getIgnoreWarnings() 0 1 1
A setIgnoreWarnings() 0 1 1
A __construct() 0 6 1
C parse() 0 22 7
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