User::hasRole()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 12
1
<?php
2
3
namespace App;
4
5
use Illuminate\Foundation\Auth\User as Authenticatable;
6
use Illuminate\Notifications\Notifiable;
7
8
class User extends Authenticatable
9
{
10
    use Notifiable;
11
12
    /**
13
     * The attributes that are mass assignable.
14
     *
15
     * @var array
16
     */
17
    protected $fillable = [
18
        'name',
19
        'email',
20
        'password',
21
        'email_token',
22
        'activated',
23
    ];
24
25
    /**
26
     * The attributes that should be hidden for arrays.
27
     *
28
     * @var array
29
     */
30
    protected $hidden = [
31
        'password',
32
        'remember_token',
33
    ];
34
35
    /**
36
     * Return the links for this user.
37
     *
38
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
39
     */
40
    public function links()
41
    {
42
        return $this->hasMany(Link::class);
43
    }
44
45
    /**
46
     * User is linked to a role.
47
     *
48
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
49
     */
50
    public function roles()
51
    {
52
        return $this->belongsToMany(\App\Role::class)->withTimestamps();
53
    }
54
55
    /**
56
     * Check User role.
57
     *
58
     * @param string $name
59
     *
60
     * @return bool
61
     */
62
    public function hasRole($name)
63
    {
64
        foreach ($this->roles as $role) {
0 ignored issues
show
Documentation introduced by
The property roles does not exist on object<App\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
65
            if ($role->name == $name) {
66
                return true;
67
            }
68
        }
69
70
        return false;
71
    }
72
73
    /**
74
     * Assign user to role.
75
     *
76
     * @param $role
77
     */
78
    public function assignRole(Role $role)
79
    {
80
        $this->roles()->attach($role);
81
    }
82
83
    /**
84
     * Remove user role.
85
     *
86
     * @param $role
87
     *
88
     * @return int
89
     */
90
    public function removeRole($role)
91
    {
92
        return $this->roles()->detach($role);
93
    }
94
95
    /**
96
     * Create a unique email token.
97
     *
98
     * @return int
99
     */
100
    public function createEmailToken() {
101
        $token = str_random(config('myio.general.mail_token_length'));
102
103
        if (self::whereEmailToken($token)->exists()) {
104
            return $this->generateUniqueHash();
0 ignored issues
show
Documentation Bug introduced by
The method generateUniqueHash does not exist on object<App\User>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
105
        }
106
107
        return $token;
108
    }
109
110
    /**
111
     * Verify a user.
112
     */
113
    public function verified()
114
    {
115
        $this->activated = 1;
0 ignored issues
show
Documentation introduced by
The property activated does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
116
        $this->email_token = null;
0 ignored issues
show
Documentation introduced by
The property email_token does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
117
        $this->save();
118
    }
119
}
120