Completed
Push — develop ( 6d4660...72bd10 )
by
unknown
10s
created

NumberOfTokensPerIdentityOption::getDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * Copyright 2018 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\Stepup\Configuration\Value;
20
21
use JsonSerializable;
22
use Surfnet\Stepup\Exception\InvalidArgumentException;
23
24
class NumberOfTokensPerIdentityOption implements JsonSerializable
25
{
26
    const DISABLED = 0;
27
    
28
    /**
29
     * @var int
30
     */
31
    private $numberOfTokensPerIdentity;
32
33
    public static function getDefault()
34
    {
35
        return new self(self::DISABLED);
36
    }
37
38
    public function __construct($numberOfTokensPerIdentity)
39
    {
40
        if (!is_numeric($numberOfTokensPerIdentity)) {
41
            throw InvalidArgumentException::invalidType(
42
                'integer',
43
                'numberOfTokensPerIdentity',
44
                $numberOfTokensPerIdentity
45
            );
46
        }
47
48
        $this->numberOfTokensPerIdentity = $numberOfTokensPerIdentity;
0 ignored issues
show
Documentation Bug introduced by
It seems like $numberOfTokensPerIdentity can also be of type double or string. However, the property $numberOfTokensPerIdentity is declared as type integer. 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...
49
    }
50
51
    /**
52
     * @param NumberOfTokensPerIdentityOption $other
53
     * @return bool
54
     */
55
    public function equals(NumberOfTokensPerIdentityOption $other)
56
    {
57
        return $this->numberOfTokensPerIdentity === $other->numberOfTokensPerIdentity;
58
    }
59
60
    /**
61
     * @return bool
62
     */
63
    public function isEnabled()
64
    {
65
        return $this->numberOfTokensPerIdentity > self::DISABLED;
66
    }
67
68
    /**
69
     * @return int
70
     */
71
    public function getNumberOfTokensPerIdentity()
72
    {
73
        return $this->numberOfTokensPerIdentity;
74
    }
75
76
    public function jsonSerialize()
77
    {
78
        return $this->numberOfTokensPerIdentity;
79
    }
80
}
81