DateUpdatedMutatorTrait::getDateUpdatedIso8601()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 10
cp 0
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\objects;
10
11
use craft\helpers\DateTimeHelper;
12
use DateTime;
13
14
/**
15
 * @property DateTime|null $dateUpdated
16
 *
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 2.0.0
19
 */
20
trait DateUpdatedMutatorTrait
21
{
22
    /**
23
     * @param $value
24
     * @return $this
25
     * @throws \Exception
26
     */
27
    public function setDateUpdated($value)
28
    {
29
        if ($value) {
30
            $value = DateTimeHelper::toDateTime($value);
31
        }
32
33
        $this->dateUpdated = $value ?: null;
34
35
        return $this;
36
    }
37
38
    /**
39
     * @return DateTime|false|null
40
     * @throws \Exception
41
     */
42
    public function getDateUpdated()
43
    {
44
        if (empty($this->dateUpdated)) {
45
            return DateTimeHelper::toDateTime(
46
                new DateTime('now')
0 ignored issues
show
Documentation introduced by
new \DateTime('now') is of type object<DateTime>, but the function expects a string|integer|array|null.

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...
47
            );
48
        }
49
50
        return $this->dateUpdated;
51
    }
52
53
    /**
54
     * @return false|string|null
55
     * @throws \Exception
56
     */
57
    public function getDateUpdatedIso8601()
58
    {
59
60
        // Get the datetime
61
        if (!$dateCreated = $this->getDateUpdated()) {
62
            return null;
63
        }
64
65
        // Convert it to iso
66
        if (!$iso = DateTimeHelper::toIso8601($dateCreated)) {
67
            return null;
68
        }
69
70
        return $iso;
71
    }
72
}
73