Completed
Push — master ( 0d5f24...6df70d )
by Luka
04:20
created

Authorization   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 47
ccs 13
cts 13
cp 1
rs 10
c 1
b 0
f 0
wmc 3
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A fromObject() 0 9 1
1
<?php
2
3
namespace Baguette\Mastodon\Service;
4
5
/**
6
 * Mastodon Anthorization object
7
 *
8
 * @author    USAMI Kenta <[email protected]>
9
 * @copyright 2017 Baguette HQ
10
 * @license   https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0
11
 * @property-read string $access_token
12
 * @property-read string $token_type
13
 * @property-read Scope  $scope
14
 * @property-read \DateTimeImmutable $created_at
15
 */
16
class Authorization
17
{
18
    use \Teto\Object\PrivateGetter;
19
20
    /** @var string */
21
    private $access_token;
22
    /** @var string */
23
    private $token_type;
24
    /** @var Scope */
25
    private $scope;
26
    /** @var \DateTimeImmutable|null */
27
    private $created_at;
28
29
    /**
30
     * @param string   $access_token
31
     * @param string   $token_type
32
     * @param string[] $scope
33
     * @param int      $created_at
34
     */
35 2
    public function __construct(
36
        $access_token,
37
        $token_type = 'bearer',
38
        Scope $scope = null,
39
        $created_at = null
40
    ) {
41 2
        $this->access_token = $access_token;
42 2
        $this->token_type = $token_type;
43 2
        $this->scope      = $scope;
44 2
        if ($created_at !== null) {
45 2
            $this->created_at = (new \DateTimeImmutable)->setTimeStamp($created_at);
0 ignored issues
show
Documentation Bug introduced by
It seems like (new \DateTimeImmutable(...tTimeStamp($created_at) can also be of type false. However, the property $created_at is declared as type object<DateTimeImmutable>|null. 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...
46
        }
47 2
    }
48
49
    /**
50
     * @param  object $obj
51
     * @return static
52
     */
53 1
    public static function fromObject($obj)
54
    {
55 1
        return new static(
56 1
            $obj->access_token,
57 1
            'bearer',
58 1
            \Baguette\Mastodon\scope($obj->scope),
59 1
            $obj->created_at
60
        );
61
    }
62
}
63