CreatedAt::getCreatedAt()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.2222
c 0
b 0
f 0
cc 6
nc 5
nop 1
1
<?php
2
3
namespace ETNA\Doctrine\Extensions;
4
5
use Doctrine\ORM\Mapping\Column;
6
use Doctrine\ORM\Mapping\PrePersist;
7
8 View Code Duplication
trait CreatedAt
0 ignored issues
show
Duplication introduced by
This class 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...
9
{
10
    /**
11
     * @Column(type="datetime", name="created_at")
12
     */
13
    private $created_at;
14
15
    /**
16
     * Getter
17
     */
18
    public function getCreatedAt($format = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
19
    {
20
        switch (true) {
21
            case is_string($this->created_at):
22
            case is_object($this->created_at) && get_class($this->created_at) !== 'DateTime':
23
                throw new \Exception("created_at is not a datetime", 400);
24
            case $this->created_at === null:
25
            case $format === null:
26
                return $this->created_at;
27
            default:
28
                return $this->created_at->format($format);
29
        }
30
    }
31
32
    /**
33
     * @PrePersist
34
     */
35
    public function setCreatedAtBeforePersist()
36
    {
37
        $this->created_at = new \DateTime(date('Y-m-d H:i:s'));
38
    }
39
}
40