AbstractDateHeader   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 101
rs 10
c 1
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A create() 0 4 2
A getFieldName() 0 4 1
A setDate() 0 8 1
A getDate() 0 4 1
A getFieldValue() 0 4 1
A __toString() 0 4 1
1
<?php
2
/*
3
 * This file is part of the Borobudur-Http package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Http\Header;
12
13
use DateTime;
14
use DateTimeZone;
15
16
/**
17
 * @author      Iqbal Maulana <[email protected]>
18
 * @created     7/27/15
19
 */
20
abstract class AbstractDateHeader implements HeaderInterface
21
{
22
    use HeaderFactoryTrait {
23
        HeaderFactoryTrait::create as protected createHeader;
24
    }
25
26
    /**
27
     * @var string
28
     */
29
    protected static $headerName;
30
31
    /**
32
     * @var string
33
     */
34
    protected static $format = 'D, d M Y H:i:s \G\M\T';
35
36
    /**
37
     * @var DateTime
38
     */
39
    private $date;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param DateTime|null $date
45
     */
46
    public function __construct(DateTime $date = null)
47
    {
48
        GenericHeader::assertEmptyHeaderFieldName(static::$headerName);
49
        $this->setDate(null !== $date ? $date : new DateTime());
50
    }
51
52
    /**
53
     * Factory create header.
54
     *
55
     * @param string $fieldName
56
     * @param string $fieldValue
57
     *
58
     * @return $this
59
     */
60
    public static function create($fieldName, $fieldValue)
61
    {
62
        return self::createHeader($fieldName, new DateTime($fieldValue ?: 'now'));
0 ignored issues
show
Documentation introduced by
new \DateTime($fieldValue ?: 'now') is of type object<DateTime>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
63
    }
64
65
    /**
66
     * Get header name.
67
     *
68
     * @return string
69
     */
70
    public function getFieldName()
71
    {
72
        return static::$headerName;
73
    }
74
75
    /**
76
     * Set date.
77
     *
78
     * @param DateTime $date
79
     *
80
     * @return $this
81
     */
82
    public function setDate(DateTime $date)
83
    {
84
        $date->setTimezone(new DateTimeZone('GMT'));
85
86
        $this->date = $date;
87
88
        return $this;
89
    }
90
91
    /**
92
     * Get date.
93
     *
94
     * @return DateTime
95
     */
96
    public function getDate()
97
    {
98
        return $this->date;
99
    }
100
101
    /**
102
     * Get header field value.
103
     *
104
     * @return mixed
105
     */
106
    public function getFieldValue()
107
    {
108
        return $this->date->format(static::$format);
109
    }
110
111
    /**
112
     * Cast header to string.
113
     *
114
     * @return string
115
     */
116
    public function __toString()
117
    {
118
        return sprintf('%s: %s', $this->getFieldName(), $this->getFieldValue());
119
    }
120
}
121