Completed
Pull Request — master (#12)
by
unknown
11:38
created

User::getRoles()   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 SumoCoders\FrameworkMultiUserBundle\User;
4
5
use SumoCoders\FrameworkMultiUserBundle\Security\PasswordResetToken;
6
7
class User implements UserInterface, PasswordReset
8
{
9
    /**
10
     * @var string
11
     */
12
    private $username;
13
14
    /**
15
     * @var string
16
     */
17
    private $password;
18
19
    /**
20
     * @var string
21
     */
22
    private $displayName;
23
24
    /**
25
     * @var PasswordResetToken
26
     */
27
    private $passwordResetToken;
28
29
    /**
30
     * @var string
31
     */
32
    private $email;
33
34
    /**
35
     * @var int
36
     */
37
    private $id;
38
39
    /**
40
     * @param string $username
41
     * @param string $password
42
     * @param string $displayName
43
     * @param string $email
44
     * @param int $id
45
     * @param PasswordResetToken $token
46
     */
47
    public function __construct($username, $password, $displayName, $email, $id = null, PasswordResetToken $token = null)
48
    {
49
        $this->username = $username;
50
        $this->password = $password;
51
        $this->displayName = $displayName;
52
        $this->email = $email;
53
        $this->id = $id;
54
        
55
        if ($token) {
56
            $this->passwordResetToken = $token;
57
        }
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function getRoles()
64
    {
65
        return [ 'ROLE_USER' ];
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function getPassword()
72
    {
73
        return $this->password;
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79
    public function getSalt()
80
    {
81
        return;
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87
    public function getUsername()
88
    {
89
        return $this->username;
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    public function eraseCredentials()
96
    {
97
        return;
98
    }
99
100
    /**
101
     * {@inheritDoc}
102
     */
103
    public function getDisplayName()
104
    {
105
        return $this->displayName;
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     */
111
    public function __toString()
112
    {
113
        return $this->getDisplayName();
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function clearPasswordResetToken()
120
    {
121
        $this->passwordResetToken = null;
122
123
        return $this;
124
    }
125
126
    /**
127
     * @return self
128
     */
129
    public function generatePasswordResetToken()
130
    {
131
        $this->passwordResetToken = PasswordResetToken::generate();
132
133
        return $this;
134
    }
135
136
    /**
137
     * @return string
138
     */
139
    public function getPasswordResetToken()
140
    {
141
        return $this->passwordResetToken;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->passwordResetToken; (SumoCoders\FrameworkMult...rity\PasswordResetToken) is incompatible with the return type declared by the interface SumoCoders\FrameworkMult...::getPasswordResetToken of type string.

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...
142
    }
143
144
    /**
145
     * @return string
146
     */
147
    public function getEmail()
148
    {
149
        return $this->email;
150
    }
151
152
    /**
153
     * @param $password
154
     *
155
     * @return self
156
     */
157
    public function setPassword($password)
158
    {
159
        $this->password = $password;
160
161
        return $this;
162
    }
163
164
    /**
165
     * @return int
166
     */
167
    public function getId()
168
    {
169
        return $this->id;
170
    }
171
}
172