Completed
Push — master ( ba6b04...ce04f3 )
by Vitaliy
05:28 queued 01:29
created

Symbols::getRandomSymbol()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace iiifx\PasswordGenerator;
4
5
use InvalidArgumentException;
6
7
class Symbols
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $symbols;
13
14
    /**
15
     * @var int
16
     */
17
    protected $priority;
18
19
    /**
20
     * @param string $symbols
21
     * @param int    $priority
22
     */
23 8
    public function __construct ( $symbols, $priority = 100 )
24
    {
25 8
        $this->setSymbols( $symbols );
26 7
        $this->setPriority( $priority );
27 6
    }
28
29
    /**
30
     * @param $symbols
31
     *
32
     * @return bool
33
     *
34
     * @throws InvalidArgumentException
35
     */
36 8
    protected function setSymbols ( $symbols )
37
    {
38 8
        if ( is_string( $symbols ) && strlen( $symbols ) > 0 ) {
39 7
            $this->symbols = $symbols;
40 7
            return true;
41
        }
42 1
        throw new InvalidArgumentException( 'Symbols must be a string, and contain at least one character' );
43
    }
44
45
    /**
46
     * @param int $priority
47
     *
48
     * @return bool
49
     *
50
     * @throws InvalidArgumentException
51
     */
52 7
    protected function setPriority ( $priority )
53
    {
54 7
        if ( is_numeric( $priority ) && $priority > 0 && $priority <= 100 ) {
55 6
            $this->priority = $priority;
0 ignored issues
show
Documentation Bug introduced by
It seems like $priority can also be of type double or string. However, the property $priority 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...
56 6
            return true;
57
        }
58 1
        throw new InvalidArgumentException( 'Priority must be interger and in the range from 1 to 100' );
59
    }
60
61
    /**
62
     * @return int
63
     */
64 3
    public function getPriority ()
65
    {
66 3
        return $this->priority;
67
    }
68
69
    /**
70
     * @return string
71
     */
72 2
    public function getRandomSymbol ()
73
    {
74 2
        $length = strlen( $this->symbols );
75 2
        $position = mt_rand( 0, $length - 1 );
76 2
        return $this->symbols[ $position ];
77
    }
78
}
79