Completed
Pull Request — master (#358)
by Leny
24:06
created

User::getHeartbeat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Victoire\Bundle\UserBundle\Model;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use FOS\UserBundle\Model\User as BaseUser;
7
8
/**
9
 * Victoire User model.
10
 */
11
class User extends BaseUser implements VictoireUserInterface
12
{
13
    use \Gedmo\Timestampable\Traits\TimestampableEntity;
14
15
    /**
16
     * @ORM\Id
17
     * @ORM\Column(type="integer")
18
     * @ORM\GeneratedValue(strategy="AUTO")
19
     */
20
    protected $id;
21
22
    /**
23
     * @var string
24
     *
25
     * @ORM\Column(name="firstname", type="string", length=255, nullable=true)
26
     */
27
    protected $firstname;
28
29
    /**
30
     * @var string
31
     *
32
     * @ORM\Column(name="lastname", type="string", length=255, nullable=true)
33
     */
34
    protected $lastname;
35
36
    /**
37
     * @var string
38
     *
39
     * @ORM\Column(name="locale", type="string", length=255, nullable=true)
40
     */
41
    protected $locale;
42
43
    /**
44
     * @var \DateTime
45
     *
46
     * @ORM\Column(name="heartbeat", type="datetime", nullable=true)
47
     */
48
    protected $heartbeat;
49
50
    protected $pages;
51
52
    /**
53
     * Get firstname.
54
     *
55
     * @return string
56
     */
57
    public function getFirstname()
58
    {
59
        return $this->firstname;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->firstname; (string) is incompatible with the return type declared by the interface Victoire\Bundle\UserBund...Interface::getFirstname of type Victoire\Bundle\UserBund...l\VictoireUserInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
60
    }
61
62
    /**
63
     * Set firstname.
64
     *
65
     * @param string $firstname
66
     *
67
     * @return $this
68
     */
69
    public function setFirstname($firstname)
70
    {
71
        $this->firstname = $firstname;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Get lastname.
78
     *
79
     * @return string
80
     */
81
    public function getLastname()
82
    {
83
        return $this->lastname;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->lastname; (string) is incompatible with the return type declared by the interface Victoire\Bundle\UserBund...rInterface::getLastname of type Victoire\Bundle\UserBund...l\VictoireUserInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
84
    }
85
86
    /**
87
     * Set lastname.
88
     *
89
     * @param string $lastname
90
     *
91
     * @return $this
92
     */
93
    public function setLastname($lastname)
94
    {
95
        $this->lastname = $lastname;
96
97
        return $this;
98
    }
99
100
    /**
101
     * Get fullName.
102
     *
103
     * @return string
104
     */
105
    public function getFullName()
106
    {
107
        return $this->firstname.' '.$this->lastname;
108
    }
109
110
    /**
111
     * Get locale.
112
     *
113
     * @return string
114
     */
115
    public function getLocale()
116
    {
117
        return $this->locale;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->locale; (string) is incompatible with the return type declared by the interface Victoire\Bundle\UserBund...serInterface::getLocale of type Victoire\Bundle\UserBund...l\VictoireUserInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
118
    }
119
120
    /**
121
     * Set locale.
122
     *
123
     * @param string $locale
124
     *
125
     * @return $this
126
     */
127
    public function setLocale($locale)
128
    {
129
        $this->locale = $locale;
130
131
        return $this;
132
    }
133
134
    /**
135
     * @return \DateTime
136
     */
137
    public function getHeartbeat()
138
    {
139
        return $this->heartbeat;
140
    }
141
142
    /**
143
     * @param \DateTime $heartbeat
144
     */
145
    public function setHeartbeat($heartbeat)
146
    {
147
        $this->heartbeat = $heartbeat;
148
    }
149
}
150