Completed
Pull Request — master (#16)
by
unknown
03:03
created

UserWithPassword   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 171
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 2
A getRoles() 0 4 1
A getPassword() 0 4 1
A getSalt() 0 4 1
A getUsername() 0 4 1
A eraseCredentials() 0 4 1
A getDisplayName() 0 4 1
A __toString() 0 4 1
A clearPasswordResetToken() 0 6 1
A generatePasswordResetToken() 0 6 1
A getPasswordResetToken() 0 4 1
A getEmail() 0 4 1
A setPassword() 0 6 1
A getId() 0 4 1
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\User;
4
5
use SumoCoders\FrameworkMultiUserBundle\Security\PasswordResetToken;
6
7
class UserWithPassword implements User, PasswordReset
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $username;
13
14
    /**
15
     * @var string
16
     */
17
    protected $password;
18
19
    /**
20
     * @var string
21
     */
22
    protected $displayName;
23
24
    /**
25
     * @var PasswordResetToken
26
     */
27
    protected $passwordResetToken;
28
29
    /**
30
     * @var string
31
     */
32
    protected $email;
33
34
    /**
35
     * @var int
36
     */
37
    protected $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(
48
        $username,
49
        $password,
50
        $displayName,
51
        $email,
52
        $id = null,
53
        PasswordResetToken $token = null
54
    ) {
55
        $this->username = $username;
56
        $this->password = $password;
57
        $this->displayName = $displayName;
58
        $this->email = $email;
59
        $this->id = $id;
60
61
        if ($token) {
62
            $this->passwordResetToken = $token;
63
        }
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69
    public function getRoles()
70
    {
71
        return [ 'ROLE_USER' ];
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77
    public function getPassword()
78
    {
79
        return $this->password;
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85
    public function getSalt()
86
    {
87
        return;
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93
    public function getUsername()
94
    {
95
        return $this->username;
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101
    public function eraseCredentials()
102
    {
103
        return;
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     */
109
    public function getDisplayName()
110
    {
111
        return $this->displayName;
112
    }
113
114
    /**
115
     * {@inheritDoc}
116
     */
117
    public function __toString()
118
    {
119
        return $this->getDisplayName();
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function clearPasswordResetToken()
126
    {
127
        $this->passwordResetToken = null;
128
129
        return $this;
130
    }
131
132
    /**
133
     * @return self
134
     */
135
    public function generatePasswordResetToken()
136
    {
137
        $this->passwordResetToken = PasswordResetToken::generate();
138
139
        return $this;
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    public function getPasswordResetToken()
146
    {
147
        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...
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getEmail()
154
    {
155
        return $this->email;
156
    }
157
158
    /**
159
     * @param string $password
160
     *
161
     * @return self
162
     */
163
    public function setPassword($password)
164
    {
165
        $this->password = $password;
166
167
        return $this;
168
    }
169
170
    /**
171
     * @return int
172
     */
173
    public function getId()
174
    {
175
        return $this->id;
176
    }
177
}
178