User::findByEmail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Services\Auth;
4
5
use Carbon\Carbon;
6
use Illuminate\Auth\Authenticatable;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Notifications\Notifiable;
9
use Illuminate\Auth\Passwords\CanResetPassword;
10
use Illuminate\Foundation\Auth\Access\Authorizable;
11
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
12
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
13
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
14
15
/**
16
 * @property int $id
17
 * @property string $email
18
 * @property string $password
19
 * @property string $first_name
20
 * @property string $last_name
21
 * @property string $name
22
 * @property string $remember_token
23
 * @property string $locale
24
 * @property \Carbon\Carbon $last_activity
25
 * @property \Carbon\Carbon $created_at
26
 * @property \Carbon\Carbon $updated_at
27
 */
28
abstract class User extends Model implements AuthenticatableContract, CanResetPasswordContract, AuthorizableContract
29
{
30
    use Authenticatable, CanResetPassword, Authorizable, Notifiable, UserPresenter;
31
32
    protected $guarded = ['id'];
33
    protected $hidden = ['password', 'remember_token'];
34
    protected $dates = ['last_activity'];
35
36
    abstract public function guardDriver(): string;
37
38
    abstract public function getHomeUrl(): string;
39
40
    abstract public function getProfileUrl(): string;
41
42
    public function getNameAttribute(): string
43
    {
44
        return $this->first_name.' '.$this->last_name;
45
    }
46
47
    public function hasNeverLoggedIn(): bool
48
    {
49
        return empty($this->password);
50
    }
51
52
    public function registerLastActivity(): User
53
    {
54
        $this->last_activity = Carbon::now();
55
56
        return $this;
57
    }
58
59
    public function isCurrentUser(): bool
60
    {
61
        if (! $this->id) {
62
            return false;
63
        }
64
65
        if ($this->guardDriver() !== config('auth.defaults.guard')) {
66
            return false;
67
        }
68
69
        return $this->id === auth()->id();
0 ignored issues
show
Bug introduced by
The method id 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...
70
    }
71
72
    /**
73
     * @param string $token
74
     *
75
     * @return \App\Services\Auth\User|null
76
     */
77
    public static function findByToken(string $token)
78
    {
79
        $resetRecord = app('db')->table('password_resets')->where('token', $token)->first();
80
81
        if (empty($resetRecord)) {
82
            return;
83
        }
84
85
        return static::where('email', $resetRecord->email)->first();
86
    }
87
88
    /**
89
     * @param string $email
90
     *
91
     * @return \App\Services\Auth\User|null
92
     */
93
    public static function findByEmail(string $email)
94
    {
95
        return static::where('email', $email)->first();
96
    }
97
}
98