Test Failed
Push — master ( a8b57d...533f3a )
by Dan Michael O.
03:50
created

UserIdentifiers::all()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma\Users;
4
5
class UserIdentifiers
6
{
7
    /* @var string */
8
    protected $id;
9
10
    /* @var \stdClass */
11
    protected $data;
12
13
    /**
14
     * UserIdentifiers constructor.
15
     *
16
     * @param \stdClass   $data
17
     */
18
    public function __construct($id, $data = null)
19
    {
20
        $this->id = $id;
21
        $this->data = $data ?? [];
0 ignored issues
show
Documentation Bug introduced by
It seems like $data ?? array() can also be of type array. However, the property $data is declared as type object<stdClass>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
22
    }
23
24
    /**
25
     * Get a flat array of all the user IDs.
26
     *
27
     * @param string $status (Default: 'ACTIVE').
28
     *
29
     * @return string[]
30
     */
31 View Code Duplication
    public function all($status='ACTIVE')
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...
32
    {
33
        $ids = [$this->id];
34
        foreach ($this->data as $identifier) {
0 ignored issues
show
Bug introduced by
The expression $this->data of type object<stdClass> is not traversable.
Loading history...
35
            if (is_null($status) || $identifier->status == $status) {
36
                $ids[] = $identifier->value;
37
            }
38
        }
39
40
        return $ids;
41
    }
42
43
    /**
44
     * Get all active user identifiers of a given type, like 'BARCODE' or 'UNIV_ID'.
45
     *
46
     * @param string $value
47
     *
48
     * @return null|string
49
     */
50 View Code Duplication
    public function allOfType($value, $status = 'ACTIVE')
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...
51
    {
52
        $ids = [];
53
        foreach ($this->data as $identifier) {
0 ignored issues
show
Bug introduced by
The expression $this->data of type object<stdClass> is not traversable.
Loading history...
54
            if ($identifier->id_type->value == $value && (is_null($status) || $identifier->status == $status)) {
55
                $ids[] = $identifier->value;
56
            }
57
        }
58
        return $ids;
59
    }
60
61
    /**
62
     * Get the first active user identifier of a given type, like 'BARCODE' or 'UNIV_ID'.
63
     *
64
     * @param string $value
65
     *
66
     * @return null|string
67
     */
68
    public function firstOfType($value, $status = 'ACTIVE')
69
    {
70
        foreach ($this->data as $identifier) {
0 ignored issues
show
Bug introduced by
The expression $this->data of type object<stdClass> is not traversable.
Loading history...
71
            if ($identifier->id_type->value == $value && (is_null($status) || $identifier->status == $status)) {
72
                return $identifier->value;
73
            }
74
        }
75
    }
76
77
    /**
78
     * Get the first active barcode.
79
     *
80
     * @return null|string
81
     */
82
    public function getBarcode()
83
    {
84
        return $this->firstOfType('BARCODE');
85
    }
86
87
    /**
88
     * Get all active barcodes.
89
     *
90
     * @return string[]
91
     */
92
    public function getBarcodes()
93
    {
94
        return $this->allOfType('BARCODE');
95
    }
96
97
    /**
98
     * Get the first active university id.
99
     *
100
     * @return null|string
101
     */
102
    public function getUniversityId()
103
    {
104
        return $this->firstOfType('UNIV_ID');
105
    }
106
107
    /**
108
     * Get all active university ids.
109
     *
110
     * @return string[]
111
     */
112
    public function getUniversityIds()
113
    {
114
        return $this->allOfType('UNIV_ID');
115
    }
116
117
    public function __get($key)
118
    {
119
        $method = 'get' . ucfirst($key);
120
        if (method_exists($this, $method)) {
121
            return $this->$method();
122
        }
123
    }
124
}
125