Completed
Pull Request — master (#12)
by
unknown
04:36 queued 01:22
created

User   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 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 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 $email
44
     * @param PasswordResetToken $token
45
     */
46
    public function __construct($username, $password, $displayName, $email, $id = null, PasswordResetToken $token = null)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
    {
48
        $this->username = $username;
49
        $this->password = $password;
50
        $this->displayName = $displayName;
51
        $this->email = $email;
52
        
53
        if ($token) {
54
            $this->passwordResetToken = $token;
55
        }
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61
    public function getRoles()
62
    {
63
        return [ 'ROLE_USER' ];
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69
    public function getPassword()
70
    {
71
        return $this->password;
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77
    public function getSalt()
78
    {
79
        return;
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85
    public function getUsername()
86
    {
87
        return $this->username;
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93
    public function eraseCredentials()
94
    {
95
        return;
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101
    public function getDisplayName()
102
    {
103
        return $this->displayName;
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     */
109
    public function __toString()
110
    {
111
        return $this->getDisplayName();
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function clearPasswordResetToken()
118
    {
119
        $this->passwordResetToken = null;
120
121
        return $this;
122
    }
123
124
    /**
125
     * @return self
126
     */
127
    public function generatePasswordResetToken()
128
    {
129
        $this->passwordResetToken = PasswordResetToken::generate();
130
131
        return $this;
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function getPasswordResetToken()
138
    {
139
        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...
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    public function getEmail()
146
    {
147
        return $this->email;
148
    }
149
150
    /**
151
     * @param $password
152
     *
153
     * @return self
154
     */
155
    public function setPassword($password)
156
    {
157
        $this->password = $password;
158
159
        return $this;
160
    }
161
162
    /**
163
     * @return int
164
     */
165
    public function getId()
166
    {
167
        return $this->id;
168
    }
169
}
170