Browse Source

Merge pull request #16605 from cakephp/security-readibility

Improve code readability.
othercorey 3 years ago
parent
commit
1dc5f2cdfb
2 changed files with 18 additions and 20 deletions
  1. 4 6
      src/Utility/Security.php
  2. 14 14
      tests/TestCase/Utility/SecurityTest.php

+ 4 - 6
src/Utility/Security.php

@@ -163,18 +163,16 @@ class Security
      */
     public static function engine($instance = null)
     {
-        if ($instance === null && static::$_instance === null) {
-            if (extension_loaded('openssl')) {
-                $instance = new OpenSsl();
-            }
-        }
         if ($instance) {
-            static::$_instance = $instance;
+            return static::$_instance = $instance;
         }
         if (isset(static::$_instance)) {
             /** @psalm-suppress LessSpecificReturnStatement */
             return static::$_instance;
         }
+        if (extension_loaded('openssl')) {
+            return static::$_instance = new OpenSsl();
+        }
         throw new InvalidArgumentException(
             'No compatible crypto engine available. ' .
             'Load the openssl extension.'

+ 14 - 14
tests/TestCase/Utility/SecurityTest.php

@@ -28,6 +28,20 @@ use RuntimeException;
 class SecurityTest extends TestCase
 {
     /**
+     * Test engine
+     */
+    public function testEngineEquivalence(): void
+    {
+        $restore = Security::engine();
+        $newEngine = new OpenSsl();
+
+        Security::engine($newEngine);
+
+        $this->assertSame($newEngine, Security::engine());
+        $this->assertNotSame($restore, Security::engine());
+    }
+
+    /**
      * testHash method
      */
     public function testHash(): void
@@ -186,20 +200,6 @@ class SecurityTest extends TestCase
     }
 
     /**
-     * Test engine
-     */
-    public function testEngineEquivalence(): void
-    {
-        $restore = Security::engine();
-        $newEngine = new OpenSsl();
-
-        Security::engine($newEngine);
-
-        $this->assertSame($newEngine, Security::engine());
-        $this->assertNotSame($restore, Security::engine());
-    }
-
-    /**
      * Tests that the salt can be set and retrieved
      */
     public function testSalt(): void