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 Nip\Container\Container; |
||||
8 | |||||
9 | /** |
||||
10 | * Trait HasApiTokensTrait |
||||
11 | * @package ByTIC\Hello\Models\Traits |
||||
12 | */ |
||||
13 | trait HasApiTokensTrait |
||||
14 | { |
||||
15 | /** |
||||
16 | * The current access token for the authentication user. |
||||
17 | * |
||||
18 | * @var Token |
||||
19 | */ |
||||
20 | protected $accessToken = null; |
||||
21 | |||||
22 | /** |
||||
23 | * Set the current access token being used by the user. |
||||
24 | * |
||||
25 | * @param Token|boolean $token |
||||
26 | * @return Token|null |
||||
27 | */ |
||||
28 | public function setToken($token) |
||||
29 | { |
||||
30 | return $this->accessToken = $token; |
||||
0 ignored issues
–
show
|
|||||
31 | } |
||||
32 | |||||
33 | /** |
||||
34 | * Get the current access token being used by the user. |
||||
35 | * |
||||
36 | * @return Token|null |
||||
37 | */ |
||||
38 | public function token() |
||||
39 | { |
||||
40 | if ($this->accessToken === null) { |
||||
41 | $this->initToken(); |
||||
42 | } |
||||
43 | return $this->accessToken; |
||||
0 ignored issues
–
show
The expression
return $this->accessToken could also return false which is incompatible with the documented return type ByTIC\Hello\Models\AccessTokens\Token|null . Did you maybe forget to handle an error condition?
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled. ![]() |
|||||
44 | } |
||||
45 | |||||
46 | /** |
||||
47 | * @param $name |
||||
48 | * @param array $scopes |
||||
49 | */ |
||||
50 | public function initTokenInModel($name, array $scopes = []) |
||||
51 | { |
||||
52 | $token = $this->createToken($name, $scopes); |
||||
53 | $this->setToken($token); |
||||
54 | } |
||||
55 | |||||
56 | /** |
||||
57 | * Create a new personal access token for the user. |
||||
58 | * |
||||
59 | * @param string $name |
||||
60 | * @param array $scopes |
||||
61 | * @return Token|null |
||||
62 | */ |
||||
63 | 1 | public function createToken($name, array $scopes = []) |
|||
64 | { |
||||
65 | 1 | $tokenResponse = Container::getInstance() |
|||
66 | 1 | ->get(TokenFactory::class) |
|||
67 | 1 | ->make($this->getIdentifier(), $name, $scopes); |
|||
0 ignored issues
–
show
It seems like
getIdentifier() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
68 | |||||
69 | 1 | return $tokenResponse; |
|||
70 | } |
||||
71 | |||||
72 | protected function initToken() |
||||
73 | { |
||||
74 | $this->setToken($this->generateToken()); |
||||
75 | } |
||||
76 | |||||
77 | /** |
||||
78 | * @return bool |
||||
79 | */ |
||||
80 | protected function generateToken() |
||||
81 | { |
||||
82 | return false; |
||||
83 | } |
||||
84 | |||||
85 | /** |
||||
86 | * @param null $name |
||||
0 ignored issues
–
show
|
|||||
87 | * @return string|null |
||||
88 | */ |
||||
89 | protected function generateTokenName($name = null) |
||||
90 | { |
||||
91 | return !empty($name) ? $name : 'default'; |
||||
92 | } |
||||
93 | } |
||||
94 |
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 theid
property of an instance of theAccount
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.