Completed
Push — master ( 8e4d04...2544e2 )
by Mehmet
04:32
created

Date::checkMin()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Selami\Entity\DataType;
5
6
use Selami\Entity\Interfaces\DataTypeInterface;
7
use DateTime;
8
use Assert\Assertion;
9
use BadMethodCallException;
10
use InvalidArgumentException;
11
12
class Date extends DataTypeAbstract implements DataTypeInterface
13
{
14
    const DATA_TYPE_ERROR   = 'Error for value "%s" for "%s" : INVALID_TYPE. Must be a date string.';
15
    const DATA_FORMAT_ERROR = 'Error for value "%s" for "%s": INVALID_DATE_FORMAT';
16
    const DATA_MIN_ERROR    = 'Error for value "%s" for "%s": MIN_VALUE=';
17
    const DATA_MAX_ERROR    = 'Error for value "%s" for "%s": MAX_VALUE=';
18
19
    protected static $defaults = [
20
        'format'    => 'Y-m-d H:i:s',
21
        'default'   => 'now',
22
        'min'       => null,
23
        'max'       => null
24
    ];
25
26
    protected static $validDateOptions = [
27
        'now',
28
        'tomorrow',
29
        'next week',
30
        'next month',
31
        'next year',
32
        'yesterday',
33
        'previous week',
34
        'previous month',
35
        'previous year',
36
    ];
37
38
    /**
39
     * Date constructor.
40
     * @param string $key
41
     * @param string $datum
42
     * @param array $options
43
     */
44
    public function __construct(string $key, string $datum, array $options = [])
45
    {
46
        $this->key = $key;
47
        $this->datum = $datum;
48
        $this->options = array_merge(self::$defaults, $options);
49
        $this->errorMessageTemplate = self::DATA_TYPE_ERROR;
50
    }
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function assert()
55
    {
56
        $this->isString();
57
        $this->checkFormat();
58
        $this->checkMin();
59
        $this->checkMax();
60
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type declared by the interface Selami\Entity\Interfaces\DataTypeInterface::assert of type Selami\Entity\Interfaces\true.

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...
61
    }
62
    private function isString()
63
    {
64
        if (!is_string($this->datum)) {
65
            $this->throwException();
66
        }
67
    }
68
69
    private function checkFormat()
70
    {
71
        $this->errorMessageTemplate = self::DATA_FORMAT_ERROR;
72
73
        if (in_array($this->datum, self::$validDateOptions, true)) {
74
            return true;
75
        }
76
        try {
77
            Assertion::date($this->datum, $this->options['format']);
78
        } catch (BadMethodCallException $e) {
79
            $this->throwException();
80
        }
81
    }
82
83 View Code Duplication
    private function checkMin()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        $this->errorMessageTemplate = self::DATA_MIN_ERROR . $this->options['min'];
86
        if ($this->options['min'] === null) {
87
            return true;
88
        }
89
        if ($this->datum < $this->options['min']) {
90
            $this->throwException();
91
        }
92
        return true;
93
    }
94
95 View Code Duplication
    private function checkMax()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97
        $this->errorMessageTemplate = self::DATA_MIN_ERROR . $this->options['max'];
98
        if ($this->options['max'] === null) {
99
            return true;
100
        }
101
        if ($this->datum > $this->options['min']) {
102
            $this->throwException();
103
        }
104
        return true;
105
    }
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function normalize()
110
    {
111
        try {
112
            $this->assert();
113
            $date = new DateTime($this->datum);
114
        } catch (InvalidArgumentException $e) {
115
            $date = new DateTime($this->options['default']);
116
        }
117
118
        return $date->format($this->options['format']);
119
    }
120
}
121