Completed
Pull Request — 1.x (#10)
by Dorian
01:37
created

DateTimeParser   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 63
rs 10
c 0
b 0
f 0

8 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 getImmutable() 0 1 1
A setImmutable() 0 1 1
A __construct() 0 7 1
C parse() 0 26 8
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
    /** @var bool */
22
    private $immutable;
23
    public function getImmutable() { return $this->immutable; }
24
    public function setImmutable(bool $immutable) { $this->immutable = $immutable; }
25
26
    /**
27
     * DateTimeParser constructor.
28
     * @param string|null $format
29
     * @param bool $ignoreWarnings
30
     * @param string $errorMessage
31
     */
32
    public function __construct(string $format = null, bool $ignoreWarnings = false, string $errorMessage = "", bool $immutable = false)
33
    {
34
        parent::__construct($errorMessage);
35
        $this->format = $format;
36
        $this->ignoreWarnings = $ignoreWarnings;
37
        $this->immutable = $immutable;
38
    }
39
40
    /**
41
     * @param mixed $rawValue
42
     * @return \DateTime
43
     * @throws ParsingException
44
     */
45
    public function parse($rawValue)
46
    {
47
        if ($rawValue === null || $rawValue === '') {
48
            return null;
49
        }
50
51
        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...
52
            $date = \DateTime::createFromFormat($this->format, $rawValue);
53
        } else {
54
            $date = new \DateTime($rawValue);
55
        }
56
57
        $errors = \DateTime::getLastErrors();
58
        if ($errors['error_count'] > 0) {
59
            $this->throw();
60
        }
61
        if (!$this->ignoreWarnings && $errors['warning_count'] > 0) {
62
            $this->throw();
63
        }
64
65
        if ($this->immutable) {
66
            $date = \DateTimeImmutable::createFromMutable($date);
67
        }
68
69
        return $date;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $date; (DateTime|false|DateTimeImmutable) is incompatible with the return type documented by MetaHydrator\Parser\DateTimeParser::parse of type DateTime|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
70
    }
71
}
72