12
12
13
13
use chillerlan \Authenticator \Authenticators \AuthenticatorInterface ;
14
14
use InvalidArgumentException ;
15
- use TypeError ;
16
15
use function array_keys ;
17
16
use function array_replace ;
18
17
use function http_build_query ;
19
- use function is_string ;
20
18
use function rawurlencode ;
21
19
use function sprintf ;
22
20
use function strtolower ;
@@ -108,17 +106,10 @@ class Authenticator{
108
106
109
107
/**
110
108
* Authenticator constructor
111
- *
112
- * @param array|null $options
113
- * @param string|null $secret
114
109
*/
115
- public function __construct (array $ options = null , $ secret = null ){
116
-
117
- if ($ options === null ){
118
- $ options = [];
119
- }
120
-
121
- $ this ->setOptions ($ options );
110
+ public function __construct (array $ options = null , string $ secret = null ){
111
+ // phpcs:ignore
112
+ $ this ->setOptions ($ options ?? []);
122
113
123
114
if ($ secret !== null ){
124
115
$ this ->setSecret ($ secret );
@@ -132,33 +123,28 @@ public function __construct(array $options = null, $secret = null){
132
123
* Please note that this will reset the secret phrase stored with the authenticator instance
133
124
* if a different mode than the current is given.
134
125
*
135
- * @param array $options
136
- *
137
- * @return \chillerlan\Authenticator\Authenticator
138
126
* @throws \InvalidArgumentException
139
127
*/
140
- public function setOptions (array $ options ){
141
- $ defaults = self ::DEFAULTS ;
128
+ public function setOptions (array $ options ):self {
142
129
// replace settings with the current and given ones
143
- $ this ->options = array_replace ($ defaults , $ this ->options , $ options );
130
+ $ this ->options = array_replace (self :: DEFAULTS , $ this ->options , $ options );
144
131
145
132
// remove unwanted keys
146
133
foreach (array_keys ($ this ->options ) as $ key ){
147
- if (!isset ($ defaults [$ key ])){
134
+ if (!isset (self :: DEFAULTS [$ key ])){
148
135
unset($ this ->options [$ key ]);
149
136
}
150
137
}
151
138
152
139
// invoke a new authenticator interface if necessary
153
140
if (!isset ($ this ->authenticator ) || $ this ->options ['mode ' ] !== $ this ->mode ){
154
- $ mode = strtolower ($ this ->options ['mode ' ]);
155
- $ modes = AuthenticatorInterface::MODES ;
141
+ $ mode = strtolower ($ this ->options ['mode ' ]);
156
142
157
- if (!isset ($ modes [$ mode ])){
143
+ if (!isset (AuthenticatorInterface:: MODES [$ mode ])){
158
144
throw new InvalidArgumentException ('Invalid mode: ' .$ mode );
159
145
}
160
146
161
- $ class = $ modes [$ mode ];
147
+ $ class = AuthenticatorInterface:: MODES [$ mode ];
162
148
$ this ->mode = $ mode ;
163
149
$ this ->authenticator = new $ class ;
164
150
}
@@ -171,12 +157,9 @@ public function setOptions(array $options){
171
157
/**
172
158
* Sets a secret phrase from a Base32 representation
173
159
*
174
- * @param string $encodedSecret
175
- *
176
- * @return \chillerlan\Authenticator\Authenticator
177
160
* @codeCoverageIgnore
178
161
*/
179
- public function setSecret ($ encodedSecret ){
162
+ public function setSecret (string $ encodedSecret ): self {
180
163
$ this ->authenticator ->setSecret ($ encodedSecret );
181
164
182
165
return $ this ;
@@ -185,22 +168,18 @@ public function setSecret($encodedSecret){
185
168
/**
186
169
* Returns a Base32 representation of the current secret phrase
187
170
*
188
- * @return string
189
171
* @codeCoverageIgnore
190
172
*/
191
- public function getSecret (){
173
+ public function getSecret (): string {
192
174
return $ this ->authenticator ->getSecret ();
193
175
}
194
176
195
177
/**
196
178
* Generates a new (secure random) secret phrase
197
179
*
198
- * @param int|null $length
199
- *
200
- * @return string
201
180
* @codeCoverageIgnore
202
181
*/
203
- public function createSecret ($ length = null ){
182
+ public function createSecret (int $ length = null ): string {
204
183
return $ this ->authenticator ->createSecret ($ length );
205
184
}
206
185
@@ -211,12 +190,9 @@ public function createSecret($length = null){
211
190
* - a UNIX timestamp (TOTP)
212
191
* - a counter value (HOTP)
213
192
*
214
- * @param int|null $data
215
- *
216
- * @return string
217
193
* @codeCoverageIgnore
218
194
*/
219
- public function code ($ data = null ){
195
+ public function code (int $ data = null ): string {
220
196
return $ this ->authenticator ->code ($ data );
221
197
}
222
198
@@ -227,13 +203,9 @@ public function code($data = null){
227
203
* - a UNIX timestamp (TOTP)
228
204
* - a counter value (HOTP)
229
205
*
230
- * @param string $otp
231
- * @param int|null $data
232
- *
233
- * @return bool
234
206
* @codeCoverageIgnore
235
207
*/
236
- public function verify ($ otp , $ data = null ){
208
+ public function verify (string $ otp , int $ data = null ): bool {
237
209
return $ this ->authenticator ->verify ($ otp , $ data );
238
210
}
239
211
@@ -242,24 +214,9 @@ public function verify($otp, $data = null){
242
214
*
243
215
* @link https://github.com/google/google-authenticator/wiki/Key-Uri-Format#parameters
244
216
*
245
- * @param string $label
246
- * @param string $issuer
247
- * @param int|null $hotpCounter
248
- * @param bool|null $omitSettings
249
- *
250
- * @return string
251
217
* @throws \InvalidArgumentException
252
218
*/
253
- public function getUri ($ label , $ issuer , $ hotpCounter = null , $ omitSettings = null ){
254
-
255
- if (!is_string ($ label )){
256
- throw new TypeError ('$label is expected to be string ' ); // @codeCoverageIgnore
257
- }
258
-
259
- if (!is_string ($ issuer )){
260
- throw new TypeError ('$issuer is expected to be string ' ); // @codeCoverageIgnore
261
- }
262
-
219
+ public function getUri (string $ label , string $ issuer , int $ hotpCounter = null , bool $ omitSettings = null ):string {
263
220
$ label = trim ($ label );
264
221
$ issuer = trim ($ issuer );
265
222
0 commit comments