Test Failed
Push — master ( a6b51e...5fffdb )
by Gabriel
08:05
created

HasApiTokensTrait::createToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 1
1
<?php
2
3
namespace ByTIC\Hello\Models\Traits;
4
5
use ByTIC\Hello\Models\AccessTokens\Token;
6
use ByTIC\Hello\Models\Clients\PersonalAccess\TokenFactory;
7
use ByTIC\Hello\Utility\ModelsHelper;
8
use Nip\Container\Container;
9
10
/**
11
 * Trait HasApiTokensTrait
12
 * @package ByTIC\Hello\Models\Traits
13
 */
14
trait HasApiTokensTrait
15
{
16
    /**
17
     * The current access token for the authentication user.
18
     *
19
     * @var Token
20
     */
21
    protected $accessToken = null;
22
23
    /**
24
     * Set the current access token being used by the user.
25
     *
26
     * @param Token|boolean $token
27
     * @return Token|null
28
     */
29
    public function setToken($token)
30
    {
31
        return $this->accessToken = $token;
0 ignored issues
show
Documentation Bug introduced by
It seems like $token can also be of type boolean. However, the property $accessToken is declared as type object<ByTIC\Hello\Models\AccessTokens\Token>. 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...
32
    }
33
34
    /**
35
     * Get the current access token being used by the user.
36
     *
37
     * @return Token|null
38
     */
39
    public function token()
40
    {
41
        if ($this->accessToken === null) {
42
            $this->initToken();
43
        }
44
        return $this->accessToken;
45
    }
46
47
    /**
48
     * @param $name
49
     * @param array $scopes
50
     */
51
    public function initTokenInModel($name, array $scopes = [])
52
    {
53
        $token = $this->createToken($name, $scopes);
54
        $this->setToken($token);
0 ignored issues
show
Bug introduced by
It seems like $token defined by $this->createToken($name, $scopes) on line 53 can be null; however, ByTIC\Hello\Models\Trait...TokensTrait::setToken() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
55
    }
56
57
    /**
58
     * Create a new personal access token for the user.
59
     *
60
     * @param string $name
61
     * @param array $scopes
62
     * @return Token|null
63
     */
64 1
    public function createToken($name, array $scopes = [])
65
    {
66 1
        $tokenResponse = Container::getInstance()
67 1
            ->get(TokenFactory::class)
68 1
            ->make($this->getIdentifier(), $name, $scopes);
0 ignored issues
show
Bug introduced by
It seems like getIdentifier() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
69
70 1
        return $tokenResponse;
71
    }
72
73
    protected function initToken()
74
    {
75
        $this->setToken($this->generateToken());
76
    }
77
78
    /**
79
     * @return bool
80
     */
81
    protected function generateToken()
82
    {
83
        return false;
84
    }
85
86
    /**
87
     * @param null $name
88
     * @return string|null
89
     */
90
    protected function generateTokenName($name = null)
91
    {
92
        return !empty($name) ? $name : 'default';
93
    }
94
}
95