These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace PragmaRX\Google2FA\Support; |
||
4 | |||
5 | use ParagonIE\ConstantTime\Base32 as ParagonieBase32; |
||
6 | |||
7 | trait Base32 |
||
8 | { |
||
9 | /** |
||
10 | * Generate a digit secret key in base32 format. |
||
11 | * |
||
12 | * @param int $length |
||
13 | * |
||
14 | * @return string |
||
15 | */ |
||
16 | public function generateBase32RandomKey($length = 16, $prefix = '') |
||
17 | { |
||
18 | $b32 = '234567QWERTYUIOPASDFGHJKLZXCVBNM'; |
||
19 | |||
20 | $secret = $prefix ? $this->toBase32($prefix) : ''; |
||
21 | |||
22 | for ($i = 0; $i < $length; $i++) { |
||
23 | $secret .= $b32[$this->getRandomNumber()]; |
||
0 ignored issues
–
show
|
|||
24 | } |
||
25 | |||
26 | $this->validateSecret($secret); |
||
0 ignored issues
–
show
It seems like
validateSecret() 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 Adding the
Loading history...
|
|||
27 | |||
28 | return $secret; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Decodes a base32 string into a binary string. |
||
33 | * |
||
34 | * @param string $b32 |
||
35 | * |
||
36 | * @throws InvalidCharactersException |
||
37 | * |
||
38 | * @return int |
||
39 | */ |
||
40 | public function base32Decode($b32) |
||
41 | { |
||
42 | $b32 = strtoupper($b32); |
||
43 | |||
44 | $this->validateSecret($b32); |
||
0 ignored issues
–
show
It seems like
validateSecret() 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 Adding the
Loading history...
|
|||
45 | |||
46 | return ParagonieBase32::decodeUpper($b32); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Encode a string to Base32. |
||
51 | * |
||
52 | * @param $string |
||
53 | * |
||
54 | * @return mixed |
||
55 | */ |
||
56 | public function toBase32($string) |
||
57 | { |
||
58 | $encoded = ParagonieBase32::encodeUpper($string); |
||
59 | |||
60 | return str_replace('=', '', $encoded); |
||
61 | } |
||
62 | } |
||
63 |
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.