Browse Source

Update return types for session handler methods.

The return types now match those of `SessionHandlerInterface` methods.
ADmad 4 years ago
parent
commit
0189f1461d

+ 2 - 2
composer.json

@@ -26,7 +26,7 @@
         "ext-intl": "*",
         "ext-json": "*",
         "ext-mbstring": "*",
-        "cakephp/chronos": "^2.0",
+        "cakephp/chronos": "dev-2.next as 2.3",
         "composer/ca-bundle": "^1.2",
         "laminas/laminas-diactoros": "^2.2.2",
         "laminas/laminas-httphandlerrunner": "^1.1",
@@ -55,7 +55,7 @@
         "cakephp/validation": "self.version"
     },
     "require-dev": {
-        "cakephp/cakephp-codesniffer": "^4.0",
+        "cakephp/cakephp-codesniffer": "^4.5",
         "mikey179/vfsstream": "^1.6",
         "paragonie/csp-builder": "^2.3",
         "phpunit/phpunit": "^8.5 || ^9.3"

+ 13 - 10
src/Http/Session/CacheSession.php

@@ -20,6 +20,7 @@ namespace Cake\Http\Session;
 
 use Cake\Cache\Cache;
 use InvalidArgumentException;
+use ReturnTypeWillChange;
 use SessionHandlerInterface;
 
 /**
@@ -55,11 +56,11 @@ class CacheSession implements SessionHandlerInterface
     /**
      * Method called on open of a database session.
      *
-     * @param string $savePath The path where to store/retrieve the session.
+     * @param string $path The path where to store/retrieve the session.
      * @param string $name The session name.
      * @return bool Success
      */
-    public function open($savePath, $name): bool
+    public function open($path, $name): bool
     {
         return true;
     }
@@ -78,14 +79,15 @@ class CacheSession implements SessionHandlerInterface
      * Method used to read from a cache session.
      *
      * @param string $id ID that uniquely identifies session in cache.
-     * @return string Session data or empty string if it does not exist.
+     * @return string|false Session data or false if it does not exist.
      */
-    public function read($id): string
+    #[ReturnTypeWillChange]
+    public function read($id)
     {
         $value = Cache::read($id, $this->_options['config']);
 
-        if (empty($value)) {
-            return '';
+        if ($value === null) {
+            return false;
         }
 
         return $value;
@@ -121,13 +123,14 @@ class CacheSession implements SessionHandlerInterface
     }
 
     /**
-     * No-op method. Always returns true since cache engine don't have garbage collection.
+     * No-op method. Always returns 0 since cache engine don't have garbage collection.
      *
      * @param int $maxlifetime Sessions that have not updated for the last maxlifetime seconds will be removed.
-     * @return bool Always true.
+     * @return int|false
      */
-    public function gc($maxlifetime): bool
+    #[ReturnTypeWillChange]
+    public function gc($maxlifetime)
     {
-        return true;
+        return 0;
     }
 }

+ 12 - 17
src/Http/Session/DatabaseSession.php

@@ -19,6 +19,7 @@ declare(strict_types=1);
 namespace Cake\Http\Session;
 
 use Cake\ORM\Locator\LocatorAwareTrait;
+use ReturnTypeWillChange;
 use SessionHandlerInterface;
 
 /**
@@ -84,11 +85,11 @@ class DatabaseSession implements SessionHandlerInterface
     /**
      * Method called on open of a database session.
      *
-     * @param string $savePath The path where to store/retrieve the session.
+     * @param string $path The path where to store/retrieve the session.
      * @param string $name The session name.
      * @return bool Success
      */
-    public function open($savePath, $name): bool
+    public function open($path, $name): bool
     {
         return true;
     }
@@ -107,9 +108,10 @@ class DatabaseSession implements SessionHandlerInterface
      * Method used to read from a database session.
      *
      * @param string $id ID that uniquely identifies session in database.
-     * @return string Session data or empty string if it does not exist.
+     * @return string|false Session data or false if it does not exist.
      */
-    public function read($id): string
+    #[ReturnTypeWillChange]
+    public function read($id)
     {
         /** @var string $pkField */
         $pkField = $this->_table->getPrimaryKey();
@@ -121,20 +123,14 @@ class DatabaseSession implements SessionHandlerInterface
             ->first();
 
         if (empty($result)) {
-            return '';
+            return false;
         }
 
         if (is_string($result['data'])) {
             return $result['data'];
         }
 
-        $session = stream_get_contents($result['data']);
-
-        if ($session === false) {
-            return '';
-        }
-
-        return $session;
+        return stream_get_contents($result['data']);
     }
 
     /**
@@ -180,12 +176,11 @@ class DatabaseSession implements SessionHandlerInterface
      * Helper function called on gc for database sessions.
      *
      * @param int $maxlifetime Sessions that have not updated for the last maxlifetime seconds will be removed.
-     * @return bool True on success, false on failure.
+     * @return int|false The number of deleted sessions on success, or false on failure.
      */
-    public function gc($maxlifetime): bool
+    #[ReturnTypeWillChange]
+    public function gc($maxlifetime)
     {
-        $this->_table->deleteAll(['expires <' => time()]);
-
-        return true;
+        return $this->_table->deleteAll(['expires <' => time()]);
     }
 }

+ 3 - 3
tests/TestCase/Http/Session/DatabaseSessionTest.php

@@ -133,7 +133,7 @@ class DatabaseSessionTest extends TestCase
         $this->assertSame($expected, $result);
 
         $result = $this->storage->read('made up value');
-        $this->assertSame('', $result);
+        $this->assertFalse($result);
     }
 
     /**
@@ -146,7 +146,7 @@ class DatabaseSessionTest extends TestCase
         $this->assertTrue($this->storage->write('foo', 'Some value'));
 
         $this->assertTrue($this->storage->destroy('foo'), 'Destroy failed');
-        $this->assertSame('', $this->storage->read('foo'), 'Value still present.');
+        $this->assertFalse($this->storage->read('foo'), 'Value still present.');
         $this->assertTrue($this->storage->destroy('foo'), 'Destroy should always return true');
     }
 
@@ -165,7 +165,7 @@ class DatabaseSessionTest extends TestCase
 
         sleep(1);
         $storage->gc(0);
-        $this->assertSame('', $storage->read('foo'));
+        $this->assertFalse($storage->read('foo'));
     }
 
     /**