DateCreatedMutatorTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 33
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setDateCreated() 0 10 3
A getDateCreated() 0 10 2
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 $dateCreated
16
 *
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 2.0.0
19
 */
20
trait DateCreatedMutatorTrait
21
{
22
    /**
23
     * @param $value
24
     * @return $this
25
     * @throws \Exception
26
     */
27
    public function setDateCreated($value)
28
    {
29
        if ($value) {
30
            $value = DateTimehelper::toDateTime($value);
31
        }
32
33
        $this->dateCreated = $value ?: null;
34
35
        return $this;
36
    }
37
38
    /**
39
     * @return DateTime|false|null
40
     * @throws \Exception
41
     */
42
    public function getDateCreated()
43
    {
44
        if (empty($this->dateCreated)) {
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->dateCreated;
51
    }
52
}
53