1212
1313use chillerlan \Authenticator \Authenticators \AuthenticatorInterface ;
1414use InvalidArgumentException ;
15- use TypeError ;
1615use function array_keys ;
1716use function array_replace ;
1817use function http_build_query ;
19- use function is_string ;
2018use function rawurlencode ;
2119use function sprintf ;
2220use function strtolower ;
@@ -108,17 +106,10 @@ class Authenticator{
108106
109107 /**
110108 * Authenticator constructor
111- *
112- * @param array|null $options
113- * @param string|null $secret
114109 */
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 ?? []);
122113
123114 if ($ secret !== null ){
124115 $ this ->setSecret ($ secret );
@@ -132,33 +123,28 @@ public function __construct(array $options = null, $secret = null){
132123 * Please note that this will reset the secret phrase stored with the authenticator instance
133124 * if a different mode than the current is given.
134125 *
135- * @param array $options
136- *
137- * @return \chillerlan\Authenticator\Authenticator
138126 * @throws \InvalidArgumentException
139127 */
140- public function setOptions (array $ options ){
141- $ defaults = self ::DEFAULTS ;
128+ public function setOptions (array $ options ):self {
142129 // 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 );
144131
145132 // remove unwanted keys
146133 foreach (array_keys ($ this ->options ) as $ key ){
147- if (!isset ($ defaults [$ key ])){
134+ if (!isset (self :: DEFAULTS [$ key ])){
148135 unset($ this ->options [$ key ]);
149136 }
150137 }
151138
152139 // invoke a new authenticator interface if necessary
153140 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 ' ]);
156142
157- if (!isset ($ modes [$ mode ])){
143+ if (!isset (AuthenticatorInterface:: MODES [$ mode ])){
158144 throw new InvalidArgumentException ('Invalid mode: ' .$ mode );
159145 }
160146
161- $ class = $ modes [$ mode ];
147+ $ class = AuthenticatorInterface:: MODES [$ mode ];
162148 $ this ->mode = $ mode ;
163149 $ this ->authenticator = new $ class ;
164150 }
@@ -171,12 +157,9 @@ public function setOptions(array $options){
171157 /**
172158 * Sets a secret phrase from a Base32 representation
173159 *
174- * @param string $encodedSecret
175- *
176- * @return \chillerlan\Authenticator\Authenticator
177160 * @codeCoverageIgnore
178161 */
179- public function setSecret ($ encodedSecret ){
162+ public function setSecret (string $ encodedSecret ): self {
180163 $ this ->authenticator ->setSecret ($ encodedSecret );
181164
182165 return $ this ;
@@ -185,22 +168,18 @@ public function setSecret($encodedSecret){
185168 /**
186169 * Returns a Base32 representation of the current secret phrase
187170 *
188- * @return string
189171 * @codeCoverageIgnore
190172 */
191- public function getSecret (){
173+ public function getSecret (): string {
192174 return $ this ->authenticator ->getSecret ();
193175 }
194176
195177 /**
196178 * Generates a new (secure random) secret phrase
197179 *
198- * @param int|null $length
199- *
200- * @return string
201180 * @codeCoverageIgnore
202181 */
203- public function createSecret ($ length = null ){
182+ public function createSecret (int $ length = null ): string {
204183 return $ this ->authenticator ->createSecret ($ length );
205184 }
206185
@@ -211,12 +190,9 @@ public function createSecret($length = null){
211190 * - a UNIX timestamp (TOTP)
212191 * - a counter value (HOTP)
213192 *
214- * @param int|null $data
215- *
216- * @return string
217193 * @codeCoverageIgnore
218194 */
219- public function code ($ data = null ){
195+ public function code (int $ data = null ): string {
220196 return $ this ->authenticator ->code ($ data );
221197 }
222198
@@ -227,13 +203,9 @@ public function code($data = null){
227203 * - a UNIX timestamp (TOTP)
228204 * - a counter value (HOTP)
229205 *
230- * @param string $otp
231- * @param int|null $data
232- *
233- * @return bool
234206 * @codeCoverageIgnore
235207 */
236- public function verify ($ otp , $ data = null ){
208+ public function verify (string $ otp , int $ data = null ): bool {
237209 return $ this ->authenticator ->verify ($ otp , $ data );
238210 }
239211
@@ -242,24 +214,9 @@ public function verify($otp, $data = null){
242214 *
243215 * @link https://github.com/google/google-authenticator/wiki/Key-Uri-Format#parameters
244216 *
245- * @param string $label
246- * @param string $issuer
247- * @param int|null $hotpCounter
248- * @param bool|null $omitSettings
249- *
250- * @return string
251217 * @throws \InvalidArgumentException
252218 */
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 {
263220 $ label = trim ($ label );
264221 $ issuer = trim ($ issuer );
265222
0 commit comments