Failed Conditions
Push — ng ( 06d981...bf1375 )
by Florent
11:23
created

User::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Bundle\Tests\TestBundle\Entity;
15
16
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId;
17
use OAuth2Framework\Component\Core\UserAccount\UserAccount;
18
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
19
use Symfony\Component\Security\Core\User\EquatableInterface;
20
use Symfony\Component\Security\Core\User\UserInterface;
21
22
final class User implements UserInterface, UserAccount, EquatableInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    private $username;
28
29
    /**
30
     * @var string
31
     */
32
    private $password;
33
34
    /**
35
     * @var null|string
36
     */
37
    private $salt;
38
39
    /**
40
     * @var string[]
41
     */
42
    private $roles;
43
44
    /**
45
     * @var string[]
46
     */
47
    private $oauth2Passwords = [];
48
49
    /**
50
     * @var UserAccountId
51
     */
52
    private $publicId;
53
54
    /**
55
     * @var \DateTimeImmutable|null
56
     */
57
    private $lastLoginAt;
58
59
    /**
60
     * @var array
61
     */
62
    private $parameters = [];
63
64
    /**
65
     * @param string                  $username
66
     * @param string                  $password
67
     * @param string|null             $salt
68
     * @param string[]                $roles
69
     * @param string[]                $oauth2Passwords
70
     * @param UserAccountId           $publicId
71
     * @param \DateTimeImmutable|null $lastLoginAt
72
     * @param array                   $parameters
73
     */
74
    public function __construct(string $username, string $password, string $salt = null, array $roles, array $oauth2Passwords, UserAccountId $publicId, \DateTimeImmutable $lastLoginAt = null, array $parameters = [])
75
    {
76
        $this->username = $username;
77
        $this->password = $password;
78
        $this->salt = $salt;
79
        $this->roles = $roles;
80
        $this->oauth2Passwords = $oauth2Passwords;
81
        $this->publicId = $publicId;
82
        $this->lastLoginAt = $lastLoginAt;
83
        $this->parameters = $parameters;
84
    }
85
86
    /**
87
     * @param string                  $username
88
     * @param string                  $password
89
     * @param string|null             $salt
90
     * @param string[]                $roles
91
     * @param string[]                $oauth2Passwords
92
     * @param UserAccountId           $publicId
93
     * @param \DateTimeImmutable|null $lastLoginAt
94
     * @param array                   $parameters
95
     *
96
     * @return User
97
     */
98
    public static function create(string $username, string $password, string $salt = null, array $roles, array $oauth2Passwords, UserAccountId $publicId, \DateTimeImmutable $lastLoginAt = null, array $parameters = [])
99
    {
100
        return new self($username, $password, $salt, $roles, $oauth2Passwords, $publicId, $lastLoginAt, $parameters);
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function getOAuth2Passwords(): array
107
    {
108
        return $this->oauth2Passwords;
109
    }
110
111
    /**
112
     * @return ResourceOwnerId
113
     */
114
    public function getPublicId(): ResourceOwnerId
115
    {
116
        return $this->publicId;
117
    }
118
119
    /**
120
     * @return \DateTimeImmutable|null
121
     */
122
    public function getLastLoginAt()
123
    {
124
        return $this->lastLoginAt;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function has(string $key): bool
131
    {
132
        return array_key_exists($key, $this->parameters);
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function get(string $key)
139
    {
140
        if (!$this->has($key)) {
141
            throw new \InvalidArgumentException(sprintf('Configuration value with key "%s" does not exist.', $key));
142
        };
143
144
        return $this->parameters[$key];
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function getRoles(): array
151
    {
152
        return $this->roles;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function getSalt()
159
    {
160
        return $this->salt;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function getUsername(): string
167
    {
168
        return $this->username;
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174
    public function getPassword(): string
175
    {
176
        return $this->password;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function eraseCredentials()
183
    {
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function isEqualTo(UserInterface $user)
190
    {
191
        if (!$user instanceof self) {
192
            return false;
193
        }
194
195
        if ($this->password !== $user->getPassword()) {
196
            return false;
197
        }
198
199
        if ($this->getSalt() !== $user->getSalt()) {
200
            return false;
201
        }
202
203
        if ($this->username !== $user->getUsername()) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !($this->username... $user->getUsername());.
Loading history...
204
            return false;
205
        }
206
207
        return true;
208
    }
209
}
210