TimedTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 86
ccs 0
cts 31
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A formatDateTime() 0 7 3
A getCreatedAt() 0 7 1
A setCreatedAt() 0 6 1
A getUpdatedAt() 0 7 1
A setUpdatedAt() 0 6 1
1
<?php
2
3
namespace Majora\Framework\Model;
4
5
/**
6
 * Implements many temporal functions and DateTime helpers
7
 */
8
trait TimedTrait
9
{
10
    /**
11
     * @var \DateTime
12
     */
13
    protected $createdAt;
14
15
    /**
16
     * @var \DateTime
17
     */
18
    protected $updatedAt;
19
20
    /**
21
     * Proxy to return given date if defined, formated under given format (if defined)
22
     *
23
     * @param \DateTime $date
24
     * @param string    $format  optional format
25
     *
26
     * @return \DateTime string
27
     */
28
    protected function formatDateTime(\DateTime $date = null, $format = null)
29
    {
30
        return $date && $format ?
0 ignored issues
show
Bug Best Practice introduced by
The expression $format of type string|null 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...
31
            $date->format($format) :
32
            $date
33
        ;
34
    }
35
36
    /**
37
     * Returns object created at.
38
     *
39
     * @param string $format optional date format
40
     *
41
     * @return \DateTime|string
42
     */
43
    public function getCreatedAt($format = null)
44
    {
45
        return $this->formatDateTime(
46
            $this->createdAt,
47
            $format
48
        );
49
    }
50
51
    /**
52
     * Define object created at.
53
     *
54
     * @param \DateTime $createdAt
55
     *
56
     * @return self
57
     */
58
    public function setCreatedAt(\DateTime $createdAt)
59
    {
60
        $this->createdAt = $createdAt;
61
62
        return $this;
63
    }
64
65
    /**
66
     * Returns object updated at.
67
     *
68
     * @param string $format optional date format
69
     *
70
     * @return \DateTime|string
71
     */
72
    public function getUpdatedAt($format = null)
73
    {
74
        return $this->formatDateTime(
75
            $this->updatedAt,
76
            $format
77
        );
78
    }
79
80
    /**
81
     * Define object updated at.
82
     *
83
     * @param \DateTime $updatedAt
84
     *
85
     * @return self
86
     */
87
    public function setUpdatedAt(\DateTime $updatedAt)
88
    {
89
        $this->updatedAt = $updatedAt;
90
91
        return $this;
92
    }
93
}
94