Completed
Push — master ( e1cce4...d2c19b )
by David
09:34 queued 11s
created

User::randomNickname()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Torralbodavid\DuckFunkCore\Models\Arcturus;
4
5
use Carbon\Carbon;
6
use Illuminate\Contracts\Auth\Authenticatable;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Notifications\Notifiable;
9
use Torralbodavid\DuckFunkCore\Events\UserEvent;
10
11
class User extends Model implements Authenticatable
12
{
13
    use Notifiable;
14
15
    protected $table = 'users';
16
    protected $guarded = [];
17
18
    /**
19
     * The event map for the model.
20
     *
21
     * @var array
22
     */
23
    protected $dispatchesEvents = [
24
        'saved' => UserEvent::class,
25
    ];
26
27
    /**
28
     * The attributes that are mass assignable.
29
     *
30
     * @var array
31
     */
32
    protected $fillable = [
33
        'username', 'mail', 'password', 'account_created', 'ip_register', 'ip_current', 'last_login',
34
    ];
35
36
    /**
37
     * The attributes that should be hidden for arrays.
38
     *
39
     * @var array
40
     */
41
    protected $hidden = [
42
        'password', 'remember_token',
43
    ];
44
45
    /**
46
     * The attributes that should be cast to native types.
47
     *
48
     * @var array
49
     */
50
    protected $casts = [
51
        'email_verified_at' => 'datetime',
52
    ];
53
54
    public function getEmailAttribute()
55
    {
56
        return $this->mail;
57
    }
58
59
    public function getEmailForPasswordReset()
60
    {
61
        return $this->mail;
62
    }
63
64
    public function getEmailForVerification()
65
    {
66
        return $this->mail;
67
    }
68
69
    /**
70
     * Get the permissions for an avatar.
71
     */
72
    public function permissions()
73
    {
74
        return $this->hasOne(Permissions::class, 'id', 'rank');
75
    }
76
77
    /*
78
     * Get avatar settings
79
     */
80
    public function settings()
81
    {
82
        return $this->hasOne(UserSettings::class, 'user_id', 'id');
83
    }
84
85
    /*
86
     * Get the bans for an avatar
87
     */
88
    public function bans()
89
    {
90
        return $this->hasMany(Bans::class, 'id', 'user_id');
91
    }
92
93
    /**
94
     * Get friend requests.
95
     */
96
    public function friend_requests()
97
    {
98
        return $this->belongsToMany($this, 'messenger_friendrequests', 'user_to_id', 'user_from_id');
99
    }
100
101
    /**
102
     * Get friends.
103
     */
104
    public function friends()
105
    {
106
        return $this->belongsToMany($this, 'messenger_friendships', 'user_one_id', 'user_two_id');
107
    }
108
109
    public function getLastLoginAttribute()
110
    {
111
        return Carbon::createFromTimestamp($this->attributes['last_login'])->diffForHumans();
112
    }
113
114
    /**
115
     * Get the name of the unique identifier for the user.
116
     *
117
     * @return string
118
     */
119
    public function getAuthIdentifierName()
120
    {
121
        return 'mail';
122
    }
123
124
    /**
125
     * Get the unique identifier for the user.
126
     *
127
     * @return mixed
128
     */
129
    public function getAuthIdentifier()
130
    {
131
        return $this->{$this->getAuthIdentifierName()};
132
    }
133
134
    /**
135
     * Get the password for the user.
136
     *
137
     * @return string
138
     */
139
    public function getAuthPassword()
140
    {
141
        return $this->password;
142
    }
143
144
    /**
145
     * Get the token value for the "remember me" session.
146
     *
147
     * @return string
148
     */
149
    public function getRememberToken()
150
    {
151
        return auth()->user()->getRememberToken();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
152
    }
153
154
    /**
155
     * Set the token value for the "remember me" session.
156
     *
157
     * @param string $value
158
     * @return void
159
     */
160
    public function setRememberToken($value)
161
    {
162
        return auth()->user()->setRememberToken();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
163
    }
164
165
    /**
166
     * Get the column name for the "remember me" token.
167
     *
168
     * @return string
169
     */
170
    public function getRememberTokenName()
171
    {
172
        return auth()->user()->getRememberTokenName();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
173
    }
174
175
    /*
176
     * Custom methods
177
     */
178
    public static function randomNickname()
179
    {
180
        $username = preg_replace('/[^A-Za-z0-9 ]/', '',
181
            faker('firstName').faker('lastName').faker()->numberBetween(1, 99)
182
        );
183
184
        return (self::where('username', $username)->exists()) ? self::randomNickname() : $username;
185
    }
186
}
187