DateJoinedMutatorTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 53
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setDateJoined() 0 11 3
A getDateJoined() 0 11 2
A getDateJoinedIso8601() 0 12 3
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\objects;
10
11
use craft\helpers\DateTimeHelper;
12
use DateTime;
13
14
/**
15
 * @property DateTime|null $dateJoined
16
 *
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
trait DateJoinedMutatorTrait
21
{
22
    /**
23
     * @noinspection PhpDocMissingThrowsInspection
24
     *
25
     * @param $value
26
     * @return $this
27
     */
28
    public function setDateJoined($value)
29
    {
30
        if ($value) {
31
            /** @noinspection PhpUnhandledExceptionInspection */
32
            $value = DateTimehelper::toDateTime($value);
33
        }
34
35
        $this->dateJoined = $value ?: null;
36
37
        return $this;
38
    }
39
40
    /**
41
     * @noinspection PhpDocMissingThrowsInspection
42
     *
43
     * @return DateTime
44
     */
45
    public function getDateJoined()
46
    {
47
        if (empty($this->dateJoined)) {
48
            /** @noinspection PhpUnhandledExceptionInspection */
49
            return DateTimeHelper::toDateTime(
50
                new DateTime()
0 ignored issues
show
Documentation introduced by
new \DateTime() 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...
51
            );
52
        }
53
54
        return $this->dateJoined;
55
    }
56
57
    /**
58
     * @return string|null
59
     */
60
    public function getDateJoinedIso8601()
61
    {
62
        if (!$dateJoined = $this->getDateJoined()) {
63
            return null;
64
        }
65
66
        if (!$iso = DateTimeHelper::toIso8601($dateJoined)) {
67
            return null;
68
        }
69
70
        return $iso;
71
    }
72
}
73