DateTime   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 46
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A now() 0 7 2
A _setNow() 0 3 1
A format() 0 9 3
A __construct() 0 1 1
1
<?php
2
3
namespace BWC\Share\Sys;
4
5
6
final class DateTime
7
{
8
    const FORMAT_YMDHIS = 'Y-m-d H:i:s';
9
10
11
    /** @var int */
12
    private static $_now = null;
13
14
    /**
15
     * @return int
16
     */
17
    static function now() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
18
        if (self::$_now) {
19
            return self::$_now;
20
        } else {
21
            return time();
22
        }
23
    }
24
25
    /**
26
     * @param int|null $value
27
     */
28
    static function _setNow($value) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
29
        self::$_now = $value;
30
    }
31
32
33
    /**
34
     * @param string $format
35
     * @param int|null $ts
36
     * @return string
37
     */
38
    static function format($format = self::FORMAT_YMDHIS, $ts = null) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
39
        if (!$ts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ts of type integer|null is loosely compared to false; 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...
40
            $ts = self::now();
41
        }
42
        if (!$format) {
43
            $format = 'Y-m-d H:i:s';
44
        }
45
        return date($format, $ts);
46
    }
47
48
49
50
    private function __construct() { }
51
}