UserIdentity   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 12 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 9
loc 75
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 9 9 1
A findIdentity() 0 4 1
A findIdentityByAccessToken() 0 4 1
A fromArray() 0 15 2
A getId() 0 4 1
A getAuthKey() 0 4 1
A validateAuthKey() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace hiapi\Core\Auth;
4
5
use yii\base\Model;
6
use yii\web\IdentityInterface;
7
8
/**
9
 * Basic user identity.
10
 * We'll see if it is enough.
11
 */
12
class UserIdentity extends Model implements IdentityInterface
13
{
14
    public $id;
15
    public $username;
16
    public $state;
17
    public $roles = [];
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 View Code Duplication
    public function rules()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
    {
24
        return [
25
            ['id',          'integer'],
26
            ['username',    'string'],
27
            ['state',       'string'],
28
            ['roles',       'trim'],
29
        ];
30
    }
31
32
    /** {@inheritdoc} */
33
    public static function findIdentity($id)
34
    {
35
        throw new \yii\base\InvalidCallException('no `findIdentity` and not expected');
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public static function findIdentityByAccessToken($token, $type = null)
42
    {
43
        throw new \yii\base\InvalidCallException('no `findIdentityByAccessToken` and not expected');
44
    }
45
46
    public static function fromArray(array $info): self
47
    {
48
        $id = $info['id'] ?? $info['sub'] ?? null;
49
50
        if (empty($id)) {
51
            return null;
52
        }
53
54
        return new static([
55
            'id'        => $id,
56
            'username'  => $info['username'] ?? $info['email'] ?? null,
57
            'state'     => $info['state'] ?? null,
58
            'roles'     => $info['roles'] ?? null,
59
        ]);
60
    }
61
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getId()
67
    {
68
        return $this->id;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getAuthKey()
75
    {
76
        return null;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function validateAuthKey($authKey)
83
    {
84
        return false;
85
    }
86
}
87