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

User::generatePasswordResetToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
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
    /** @var string */
10
    private $username;
11
12
    /** @var string */
13
    private $password;
14
15
    /** @var string */
16
    private $displayName;
17
18
    /** @var PasswordResetToken */
19
    private $passwordResetToken;
20
21
    /** @var string */
22
    private $email;
23
24
    /**
25
     * @var int
26
     */
27
    private $id;
28
29
    /**
30
     * @param string $username
31
     * @param string $password
32
     * @param string $displayName
33
     * @param $email
34
     * @param PasswordResetToken $token
35
     */
36
    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...
37
    {
38
        $this->username = $username;
39
        $this->password = $password;
40
        $this->displayName = $displayName;
41
        $this->email = $email;
42
        
43
        if ($token) {
44
            $this->passwordResetToken = $token;
45
        }
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function getRoles()
52
    {
53
        return [ 'ROLE_USER' ];
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    public function getPassword()
60
    {
61
        return $this->password;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    public function getSalt()
68
    {
69
        return;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function getUsername()
76
    {
77
        return $this->username;
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function eraseCredentials()
84
    {
85
        return;
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91
    public function getDisplayName()
92
    {
93
        return $this->displayName;
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    public function __toString()
100
    {
101
        return $this->getDisplayName();
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function clearPasswordResetToken()
108
    {
109
        $this->passwordResetToken = null;
110
111
        return $this;
112
    }
113
114
    /**
115
     * @return self
116
     */
117
    public function generatePasswordResetToken()
118
    {
119
        $this->passwordResetToken = PasswordResetToken::generate();
120
121
        return $this;
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function getPasswordResetToken()
128
    {
129
        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...
130
    }
131
132
    /**
133
     * @return string
134
     */
135
    public function getEmail()
136
    {
137
        return $this->email;
138
    }
139
140
    /**
141
     * @param $password
142
     *
143
     * @return self
144
     */
145
    public function setPassword($password)
146
    {
147
        $this->password = $password;
148
149
        return $this;
150
    }
151
152
    /**
153
     * @return int
154
     */
155
    public function getId()
156
    {
157
        return $this->id;
158
    }
159
}
160