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

User::getPasswordResetToken()   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
    /** @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 */
22
    private $email;
23
24
    /**
25
     * @param string $username
26
     * @param string $password
27
     * @param string $displayName
28
     * @param $email
29
     * @param PasswordResetToken $token
30
     */
31
    public function __construct($username, $password, $displayName, $email, PasswordResetToken $token = null)
32
    {
33
        $this->username = $username;
34
        $this->password = $password;
35
        $this->displayName = $displayName;
36
        $this->email = $email;
37
        
38
        if ($token) {
39
            $this->passwordResetToken = $token;
40
        }
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function getRoles()
47
    {
48
        return [ 'ROLE_USER' ];
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function getPassword()
55
    {
56
        return $this->password;
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public function getSalt()
63
    {
64
        return;
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70
    public function getUsername()
71
    {
72
        return $this->username;
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function eraseCredentials()
79
    {
80
        return;
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    public function getDisplayName()
87
    {
88
        return $this->displayName;
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94
    public function __toString()
95
    {
96
        return $this->getDisplayName();
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function clearPasswordResetToken()
103
    {
104
        $this->passwordResetToken = null;
105
106
        return $this;
107
    }
108
109
    /**
110
     * @return self
111
     */
112
    public function generatePasswordResetToken()
113
    {
114
        $this->passwordResetToken = PasswordResetToken::generate();
115
116
        return $this;
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    public function getPasswordResetToken()
123
    {
124
        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...
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getEmail()
131
    {
132
        return $this->email;
133
    }
134
135
    /**
136
     * @param $password
137
     *
138
     * @return self
139
     */
140
    public function setPassword($password)
141
    {
142
        $this->password = $password;
143
144
        return $this;
145
    }
146
}
147