Test Failed
Push — master ( c4aa6a...a5e42e )
by Mathieu
03:19
created

TimestampableTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 44.74 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 0
dl 34
loc 76
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setCreated() 17 17 4
A setLastModified() 17 17 4
A getCreated() 0 4 1
A getLastModified() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Charcoal\Object;
4
5
use InvalidArgumentException;
6
7
use DateTime;
8
use DateTimeInterface;
9
10
/**
11
 *
12
 */
13
trait TimestampableTrait
14
{
15
    /**
16
     * Object creation date (set automatically on save)
17
     * @var DateTimeInterface
18
     */
19
    private $created;
20
21
    /**
22
     * Object last modified date (set automatically on save and update)
23
     * @var DateTimeInterface
24
     */
25
    private $lastModified;
26
27
    /**
28
     * @param \DateTimeInterface|string|null $created The object's creation timestamp.
29
     * @throws InvalidArgumentException If the provided date/time is invalid.
30
     * @return self
31
     */
32 View Code Duplication
    public function setCreated($created)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34
        if ($created === null) {
35
            $this->created = null;
36
            return $this;
37
        }
38
        if (is_string($created)) {
39
            $created = new DateTime($created);
40
        }
41
        if (!($created instanceof DateTimeInterface)) {
42
            throw new InvalidArgumentException(
43
                'Invalid "Created" value. Must be a date/time string or a DateTime object.'
44
            );
45
        }
46
        $this->created = $created;
47
        return $this;
48
    }
49
50
    /**
51
     * @return DateTimeInterface|null
52
     */
53
    public function getCreated()
54
    {
55
        return $this->created;
56
    }
57
58
    /**
59
     * @param \DateTimeInterface|string|null $lastModified The object's last modified timestamp.
60
     * @throws InvalidArgumentException If the provided date/time is invalid.
61
     * @return self
62
     */
63 View Code Duplication
    public function setLastModified($lastModified)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        if ($lastModified === null) {
66
            $this->lastModified = null;
67
            return $this;
68
        }
69
        if (is_string($lastModified)) {
70
            $lastModified = new DateTime($lastModified);
71
        }
72
        if (!($lastModified instanceof DateTimeInterface)) {
73
            throw new InvalidArgumentException(
74
                'Invalid "Last Modified" value. Must be a date/time string or a DateTime object.'
75
            );
76
        }
77
        $this->lastModified = $lastModified;
78
        return $this;
79
    }
80
81
    /**
82
     * @return DateTimeInterface|null
83
     */
84
    public function getLastModified()
85
    {
86
        return $this->lastModified;
87
    }
88
}
89