From 1261a4139d629f65846afb06291c478c17d222cd Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Thu, 15 Jul 2021 01:45:22 +0200 Subject: [PATCH] [HttpFoundation] Fix return types of SessionHandler::gc() Signed-off-by: Alexander M. Turek --- .../Session/Storage/Handler/MemcachedSessionHandler.php | 5 +++-- .../Session/Storage/Handler/MigratingSessionHandler.php | 2 +- .../Session/Storage/Handler/MongoDbSessionHandler.php | 9 ++++----- .../Session/Storage/Handler/NullSessionHandler.php | 4 ++-- .../Session/Storage/Handler/PdoSessionHandler.php | 4 ++-- .../Session/Storage/Handler/RedisSessionHandler.php | 7 +++++-- .../Handler/AbstractRedisSessionHandlerTestCase.php | 2 +- .../Tests/Session/Storage/Handler/Fixtures/common.inc | 5 +++-- .../Storage/Handler/MemcachedSessionHandlerTest.php | 2 +- .../Storage/Handler/MongoDbSessionHandlerTest.php | 7 ++++++- 10 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MemcachedSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MemcachedSessionHandler.php index c8f1189909..d8663a57b7 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MemcachedSessionHandler.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MemcachedSessionHandler.php @@ -99,12 +99,13 @@ class MemcachedSessionHandler extends AbstractSessionHandler } /** - * @return bool + * @return int|false */ + #[\ReturnTypeWillChange] public function gc($maxlifetime) { // not required here because memcached will auto expire the records anyhow. - return true; + return 0; } /** diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MigratingSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MigratingSessionHandler.php index c3e7ef6e60..a4f28ef21b 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MigratingSessionHandler.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MigratingSessionHandler.php @@ -63,7 +63,7 @@ class MigratingSessionHandler implements \SessionHandlerInterface, \SessionUpdat } /** - * @return bool + * @return int|false */ #[\ReturnTypeWillChange] public function gc($maxlifetime) diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MongoDbSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MongoDbSessionHandler.php index 6cb8847786..772a3e56bb 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MongoDbSessionHandler.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MongoDbSessionHandler.php @@ -100,15 +100,14 @@ class MongoDbSessionHandler extends AbstractSessionHandler } /** - * @return bool + * @return int|false */ + #[\ReturnTypeWillChange] public function gc($maxlifetime) { - $this->getCollection()->deleteMany([ + return $this->getCollection()->deleteMany([ $this->options['expiry_field'] => ['$lt' => new \MongoDB\BSON\UTCDateTime()], - ]); - - return true; + ])->getDeletedCount(); } /** diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NullSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NullSessionHandler.php index c34c25edbb..a85ab65869 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NullSessionHandler.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NullSessionHandler.php @@ -70,11 +70,11 @@ class NullSessionHandler extends AbstractSessionHandler } /** - * @return bool + * @return int|false */ #[\ReturnTypeWillChange] public function gc($maxlifetime) { - return true; + return 0; } } diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php index d3c5651d41..5fec9b17ec 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php @@ -290,7 +290,7 @@ class PdoSessionHandler extends AbstractSessionHandler } /** - * @return bool + * @return int|false */ #[\ReturnTypeWillChange] public function gc($maxlifetime) @@ -299,7 +299,7 @@ class PdoSessionHandler extends AbstractSessionHandler // This way, pruning expired sessions does not block them from being started while the current session is used. $this->gcCalled = true; - return true; + return 0; } /** diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler.php index 699d6da6f6..a9c4e392e9 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler.php @@ -104,10 +104,13 @@ class RedisSessionHandler extends AbstractSessionHandler /** * {@inheritdoc} + * + * @return int|false */ - public function gc($maxlifetime): bool + #[\ReturnTypeWillChange] + public function gc($maxlifetime) { - return true; + return 0; } /** diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/AbstractRedisSessionHandlerTestCase.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/AbstractRedisSessionHandlerTestCase.php index de5188d42c..3dcb3c51db 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/AbstractRedisSessionHandlerTestCase.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/AbstractRedisSessionHandlerTestCase.php @@ -114,7 +114,7 @@ abstract class AbstractRedisSessionHandlerTestCase extends TestCase public function testGcSession() { - $this->assertTrue($this->storage->gc(123)); + $this->assertIsInt($this->storage->gc(123)); } public function testUpdateTimestamp() diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/Fixtures/common.inc b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/Fixtures/common.inc index a887f607e8..fd662e3a16 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/Fixtures/common.inc +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/Fixtures/common.inc @@ -121,11 +121,12 @@ class TestSessionHandler extends AbstractSessionHandler return true; } - public function gc($maxLifetime): bool + #[\ReturnTypeWillChange] + public function gc($maxLifetime) { echo __FUNCTION__, "\n"; - return true; + return 1; } protected function doRead($sessionId): string diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MemcachedSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MemcachedSessionHandlerTest.php index 200567a29d..d404b74c6a 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MemcachedSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MemcachedSessionHandlerTest.php @@ -113,7 +113,7 @@ class MemcachedSessionHandlerTest extends TestCase public function testGcSession() { - $this->assertTrue($this->storage->gc(123)); + $this->assertIsInt($this->storage->gc(123)); } /** diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php index 01b92dfa22..bf8021c155 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php @@ -184,9 +184,14 @@ class MongoDbSessionHandlerTest extends TestCase ->willReturnCallback(function ($criteria) { $this->assertInstanceOf(\MongoDB\BSON\UTCDateTime::class, $criteria[$this->options['expiry_field']]['$lt']); $this->assertGreaterThanOrEqual(time() - 1, round((string) $criteria[$this->options['expiry_field']]['$lt'] / 1000)); + + $result = $this->createMock(\MongoDB\DeleteResult::class); + $result->method('getDeletedCount')->willReturn(42); + + return $result; }); - $this->assertTrue($this->storage->gc(1)); + $this->assertSame(42, $this->storage->gc(1)); } public function testGetConnection()