Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

includes/Dater.php (1 issue)

Check for loose comparison of integers.

Best Practice Bug Major

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript;
3
4
use DateTime;
5
use DateTimeZone;
6
use function date_default_timezone_get;
7
8
/**
9
 * parent class to handle the date time
10
 *
11
 * @since 4.0.0
12
 *
13
 * @package Redaxscript
14
 * @category Date
15
 * @author Henry Ruhs
16
 */
17
18
class Dater
19
{
20
	/**
21
	 * time zone
22
	 *
23
	 * @var DateTimeZone
24
	 */
25
26
	protected $_timeZone;
27
28
	/**
29
	 * date time
30
	 *
31
	 * @var DateTime
32
	 */
33
34
	protected $_dateTime;
35
36
	/**
37
	 * init the class
38
	 *
39
	 * @since 4.0.0
40
	 *
41
	 * @param int $date timestamp of the date
42
	 */
43
44
	public function init(int $date = null) : void
45 2
	{
46
		$zone = null;
47 2
		if (Db::getStatus() === 2)
48 2
		{
49
			$settingModel = new Model\Setting();
50 2
			$zone = $settingModel->get('zone');
51 2
		}
52
		$this->_timeZone = new DateTimeZone($zone ? : date_default_timezone_get());
53 2
		$this->_dateTime = new DateTime();
54 2
		$this->_dateTime->setTimezone($this->_timeZone);
55 2
		if ($date)
0 ignored issues
show
Bug Best Practice introduced by
The expression $date of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
56 2
		{
57
			$this->_dateTime->setTimestamp($date);
58 1
		}
59
	}
60 2
61
	/**
62
	 * get the time zone
63
	 *
64
	 * @since 4.0.0
65
	 *
66
	 * @return DateTimeZone
67
	 */
68
69
	public function getTimeZone() : DateTimeZone
70 1
	{
71
		return $this->_timeZone;
72 1
	}
73
74
	/**
75
	 * get the date time
76
	 *
77
	 * @since 4.0.0
78
	 *
79
	 * @return DateTime
80
	 */
81
82
	public function getDateTime() : DateTime
83 2
	{
84
		return $this->_dateTime;
85 2
	}
86
87
	/**
88
	 * format to time
89
	 *
90
	 * @since 4.0.0
91
	 *
92
	 * @return string
93
	 */
94
95
	public function formatTime() : string
96 1
	{
97
		$settingModel = new Model\Setting();
98 1
		return $this->getDateTime()->format($settingModel->get('time'));
99 1
	}
100
101
	/**
102
	 * format to date
103
	 *
104
	 * @since 4.0.0
105
	 *
106
	 * @return string
107
	 */
108
109
	public function formatDate() : string
110 1
	{
111
		$settingModel = new Model\Setting();
112 1
		return $this->getDateTime()->format($settingModel->get('date'));
113 1
	}
114
115
	/**
116
	 * format to field
117
	 *
118
	 * @since 4.0.0
119
	 *
120
	 * @return string
121
	 */
122
123
	public function formatField() : string
124 1
	{
125
		return $this->getDateTime()->format('Y-m-d\TH:i');
126 1
	}
127
}
128