Passed
Push — master ( edb799...f409da )
by Petr
07:56
created

Sequence::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace kalanis\RemoteRequest\Protocols\Fsp\Session;
4
5
6
use kalanis\RemoteRequest\Interfaces\IRRTranslations;
7
use kalanis\RemoteRequest\RequestException;
8
9
10
/**
11
 * Class Sequence
12
 * @package kalanis\RemoteRequest\Protocols\Fsp\Session
13
 * Session sequences in FSP
14
 */
15
class Sequence
16
{
17
    /** @var IRRTranslations */
18
    protected $lang = null;
19
    /** @var int */
20
    protected $key = 0;
21
    /** @var float */
22
    protected $created = 0.0;
23
    /** @var float */
24
    protected $done = 0.0;
25
    /** @var float */
26
    protected $length = 0.0;
27
28 1
    public static function newSequence(IRRTranslations $lang): self
29
    {
30 1
        $lib = new static($lang);
31 1
        return $lib->generateSequence();
32
    }
33
34 4
    final public function __construct(IRRTranslations $lang)
35
    {
36 4
        $this->lang = $lang;
37 4
    }
38
39 3
    public function generateSequence(): self
40
    {
41 3
        $this->key = $this->getRandInitial();
42 3
        $this->created = microtime(true);
0 ignored issues
show
Documentation Bug introduced by
It seems like microtime(true) can also be of type string. However, the property $created is declared as type double. 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...
43 3
        return $this;
44
    }
45
46
    /**
47
     * @return int
48
     * @codeCoverageIgnore because how to process random number?
49
     */
50
    protected function getRandInitial(): int
51
    {
52
        return rand(0, 65535);
53
    }
54
55 3
    public function getKey(): int
56
    {
57 3
        return $this->key;
58
    }
59
60
    /**
61
     * @param int $sequence
62
     * @throws RequestException
63
     * @return $this
64
     */
65 3
    public function checkSequence(int $sequence): self
66
    {
67 3
        if ($this->key != $sequence) {
68 1
            throw new RequestException($this->lang->rrFspWrongSequence($sequence, $this->key));
69
        }
70 2
        return $this;
71
    }
72
73 2
    public function updateSequence(): self
74
    {
75 2
        $this->done = microtime(true);
0 ignored issues
show
Documentation Bug introduced by
It seems like microtime(true) can also be of type string. However, the property $done is declared as type double. 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...
76 2
        $this->length = $this->done - $this->created;
77 2
        return $this;
78
    }
79
}
80