11<?php
22
3+ declare (strict_types=1 );
4+
35namespace Codeception \Module ;
46
57use Codeception \Lib \Interfaces \RequiresPackage ;
6- use Codeception \Module as CodeceptionModule ;
8+ use Codeception \Module ;
79use Codeception \Exception \ModuleException ;
810use Codeception \TestInterface ;
11+ use Exception ;
912use Predis \Client as RedisDriver ;
1013
1114/**
4750 *
4851 * @author Marc Verney <[email protected] > 4952 */
50- class Redis extends CodeceptionModule implements RequiresPackage
53+ class Redis extends Module implements RequiresPackage
5154{
5255 /**
5356 * {@inheritdoc}
5457 *
5558 * No default value is set for the database, using this parameter.
59+ *
60+ * @var array
5661 */
5762 protected $ config = [
5863 'host ' => '127.0.0.1 ' ,
@@ -62,6 +67,8 @@ class Redis extends CodeceptionModule implements RequiresPackage
6267
6368 /**
6469 * {@inheritdoc}
70+ *
71+ * @var string[]
6572 */
6673 protected $ requiredFields = [
6774 'database '
@@ -76,7 +83,7 @@ class Redis extends CodeceptionModule implements RequiresPackage
7683
7784 public function _requires ()
7885 {
79- return [' Predis\Client ' => '"predis/predis": "^1.0" ' ];
86+ return [\ Predis \Client::class => '"predis/predis": "^1.0" ' ];
8087 }
8188
8289 /**
@@ -88,7 +95,7 @@ public function _initialize()
8895 {
8996 try {
9097 $ this ->driver = new RedisDriver ($ this ->config );
91- } catch (\ Exception $ e ) {
98+ } catch (Exception $ e ) {
9299 throw new ModuleException (
93100 __CLASS__ ,
94101 $ e ->getMessage ()
@@ -110,8 +117,6 @@ public function _beforeSuite($settings = [])
110117
111118 /**
112119 * Code to run before each test
113- *
114- * @param TestInterface $test
115120 */
116121 public function _before (TestInterface $ test )
117122 {
@@ -125,11 +130,11 @@ public function _before(TestInterface $test)
125130 *
126131 * @throws ModuleException
127132 */
128- public function cleanup ()
133+ public function cleanup (): void
129134 {
130135 try {
131136 $ this ->driver ->flushdb ();
132- } catch (\ Exception $ e ) {
137+ } catch (Exception $ e ) {
133138 throw new ModuleException (
134139 __CLASS__ ,
135140 $ e ->getMessage ()
@@ -174,21 +179,20 @@ public function cleanup()
174179 *
175180 * @param string $key The key name
176181 *
177- * @return mixed
182+ * @return array|string|null
178183 *
179184 * @throws ModuleException if the key does not exist
180185 */
181- public function grabFromRedis ($ key )
186+ public function grabFromRedis (string $ key )
182187 {
183188 $ args = func_get_args ();
184189
185190 switch ($ this ->driver ->type ($ key )) {
186191 case 'none ' :
187192 throw new ModuleException (
188193 $ this ,
189- " Cannot grab key \" $ key \ " as it does not exist "
194+ sprintf ( ' Cannot grab key "%s " as it does not exist ' , $ key )
190195 );
191- break ;
192196
193197 case 'string ' :
194198 $ reply = $ this ->driver ->get ($ key );
@@ -214,8 +218,7 @@ public function grabFromRedis($key)
214218 if (count ($ args ) === 2 ) {
215219 throw new ModuleException (
216220 $ this ,
217- "The method grabFromRedis(), when used with sorted "
218- . "sets, expects either one argument or three "
221+ 'The method grabFromRedis(), when used with sorted sets, expects either one argument or three '
219222 );
220223 }
221224 $ reply = $ this ->driver ->zrange (
@@ -273,15 +276,14 @@ public function grabFromRedis($key)
273276 *
274277 * @throws ModuleException
275278 */
276- public function haveInRedis ($ type , $ key , $ value )
279+ public function haveInRedis (string $ type , string $ key , $ value ): void
277280 {
278281 switch (strtolower ($ type )) {
279282 case 'string ' :
280283 if (!is_scalar ($ value )) {
281284 throw new ModuleException (
282285 $ this ,
283- 'If second argument of haveInRedis() method is "string", '
284- . 'third argument must be a scalar '
286+ 'If second argument of haveInRedis() method is "string", third argument must be a scalar '
285287 );
286288 }
287289 $ this ->driver ->set ($ key , $ value );
@@ -299,8 +301,7 @@ public function haveInRedis($type, $key, $value)
299301 if (!is_array ($ value )) {
300302 throw new ModuleException (
301303 $ this ,
302- 'If second argument of haveInRedis() method is "zset", '
303- . 'third argument must be an (associative) array '
304+ 'If second argument of haveInRedis() method is "zset", third argument must be an (associative) array '
304305 );
305306 }
306307 $ this ->driver ->zadd ($ key , $ value );
@@ -310,8 +311,7 @@ public function haveInRedis($type, $key, $value)
310311 if (!is_array ($ value )) {
311312 throw new ModuleException (
312313 $ this ,
313- 'If second argument of haveInRedis() method is "hash", '
314- . 'third argument must be an array '
314+ 'If second argument of haveInRedis() method is "hash", third argument must be an array '
315315 );
316316 }
317317 $ this ->driver ->hmset ($ key , $ value );
@@ -320,7 +320,7 @@ public function haveInRedis($type, $key, $value)
320320 default :
321321 throw new ModuleException (
322322 $ this ,
323- " Unknown type \" $ type \ " for key \" $ key \ ". Allowed types are "
323+ sprintf ( ' Unknown type "%s " for key "%s ". Allowed types are ' , $ type , $ key )
324324 . '"string", "list", "set", "zset", "hash" '
325325 );
326326 }
@@ -357,11 +357,11 @@ public function haveInRedis($type, $key, $value)
357357 * @param mixed $value Optional. If specified, also checks the key has this
358358 * value. Booleans will be converted to 1 and 0 (even inside arrays)
359359 */
360- public function dontSeeInRedis ($ key , $ value = null )
360+ public function dontSeeInRedis (string $ key , $ value = null ): void
361361 {
362362 $ this ->assertFalse (
363- ( bool ) $ this ->checkKeyExists ($ key , $ value ),
364- " The key \" $ key \ " exists " . ($ value ? ' and its value matches the one provided ' : '' )
363+ $ this ->checkKeyExists ($ key , $ value ),
364+ sprintf ( ' The key "%s " exists ' , $ key ) . ($ value ? ' and its value matches the one provided ' : '' )
365365 );
366366 }
367367
@@ -398,17 +398,15 @@ public function dontSeeInRedis($key, $value = null)
398398 * @param mixed $item The item
399399 * @param null $itemValue Optional and only used for zsets and hashes. If
400400 * specified, the method will also check that the $item has this value/score
401- *
402- * @return bool
403401 */
404- public function dontSeeRedisKeyContains ($ key , $ item , $ itemValue = null )
402+ public function dontSeeRedisKeyContains (string $ key , $ item , $ itemValue = null ): void
405403 {
406404 $ this ->assertFalse (
407- ( bool ) $ this ->checkKeyContains ($ key , $ item , $ itemValue ),
408- " The key \" $ key \ " contains " . (
405+ $ this ->checkKeyContains ($ key , $ item , $ itemValue ),
406+ sprintf ( ' The key "%s " contains ' , $ key ) . (
409407 is_null ($ itemValue )
410- ? "\" $ item\""
411- : " [ \" $ item \ " => \" $ itemValue \" ] "
408+ ? sprintf ( ' "%s" ' , $ item)
409+ : sprintf ( ' ["%s " => "%s"] ' , $ item , $ itemValue )
412410 )
413411 );
414412 }
@@ -443,11 +441,11 @@ public function dontSeeRedisKeyContains($key, $item, $itemValue = null)
443441 * @param mixed $value Optional. If specified, also checks the key has this
444442 * value. Booleans will be converted to 1 and 0 (even inside arrays)
445443 */
446- public function seeInRedis ($ key , $ value = null )
444+ public function seeInRedis (string $ key , $ value = null ): void
447445 {
448446 $ this ->assertTrue (
449- ( bool ) $ this ->checkKeyExists ($ key , $ value ),
450- " Cannot find key \" $ key\"" . ($ value ? ' with the provided value ' : '' )
447+ $ this ->checkKeyExists ($ key , $ value ),
448+ sprintf ( ' Cannot find key "%s" ' , $ key) . ($ value ? ' with the provided value ' : '' )
451449 );
452450 }
453451
@@ -471,7 +469,7 @@ public function seeInRedis($key, $value = null)
471469 *
472470 * @return mixed
473471 */
474- public function sendCommandToRedis ($ command )
472+ public function sendCommandToRedis (string $ command )
475473 {
476474 return call_user_func_array (
477475 [$ this ->driver , $ command ],
@@ -512,17 +510,15 @@ public function sendCommandToRedis($command)
512510 * @param mixed $item The item
513511 * @param null $itemValue Optional and only used for zsets and hashes. If
514512 * specified, the method will also check that the $item has this value/score
515- *
516- * @return bool
517513 */
518- public function seeRedisKeyContains ($ key , $ item , $ itemValue = null )
514+ public function seeRedisKeyContains (string $ key , $ item , $ itemValue = null ): void
519515 {
520516 $ this ->assertTrue (
521- ( bool ) $ this ->checkKeyContains ($ key , $ item , $ itemValue ),
522- " The key \" $ key \ " does not contain " . (
517+ $ this ->checkKeyContains ($ key , $ item , $ itemValue ),
518+ sprintf ( ' The key "%s " does not contain ' , $ key ) . (
523519 is_null ($ itemValue )
524- ? "\" $ item\""
525- : " [ \" $ item \ " => \" $ itemValue \" ] "
520+ ? sprintf ( ' "%s" ' , $ item)
521+ : sprintf ( ' ["%s " => "%s"] ' , $ item , $ itemValue )
526522 )
527523 );
528524 }
@@ -556,10 +552,9 @@ private function boolToString($var)
556552 * specified, the method will also check that the $item has this value/score
557553 *
558554 * @return bool
559- *
560555 * @throws ModuleException
561556 */
562- private function checkKeyContains ($ key , $ item , $ itemValue = null )
557+ private function checkKeyContains (string $ key , $ item , $ itemValue = null ): bool
563558 {
564559 $ result = null ;
565560
@@ -608,12 +603,11 @@ private function checkKeyContains($key, $item, $itemValue = null)
608603 case 'none ' :
609604 throw new ModuleException (
610605 $ this ,
611- " Key \" $ key \ " does not exist "
606+ sprintf ( ' Key "%s " does not exist ' , $ key )
612607 );
613- break ;
614608 }
615609
616- return $ result ;
610+ return ( bool ) $ result ;
617611 }
618612
619613 /**
@@ -622,10 +616,8 @@ private function checkKeyContains($key, $item, $itemValue = null)
622616 * @param string $key The key name
623617 * @param mixed $value Optional. If specified, also checks the key has this
624618 * value. Booleans will be converted to 1 and 0 (even inside arrays)
625- *
626- * @return bool
627619 */
628- private function checkKeyExists ($ key , $ value = null )
620+ private function checkKeyExists (string $ key , $ value = null ): bool
629621 {
630622 $ type = $ this ->driver ->type ($ key );
631623
@@ -684,7 +676,7 @@ private function checkKeyExists($key, $value = null)
684676 *
685677 * @return array
686678 */
687- private function scoresToFloat (array $ arr )
679+ private function scoresToFloat (array $ arr ): array
688680 {
689681 foreach ($ arr as $ member => $ score ) {
690682 $ arr [$ member ] = (float ) $ score ;
0 commit comments