bug #42114 [HttpFoundation] Fix return types of SessionHandler::gc() (derrabus)

This PR was merged into the 4.4 branch.

Discussion
----------

[HttpFoundation] Fix return types of SessionHandler::gc()

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | #41552
| License       | MIT
| Doc PR        | N/A

`SessionHandler::gc()` should return the number of garbage-collected sessions. We often return `true` which violates that contract.

Commits
-------

1261a4139d [HttpFoundation] Fix return types of SessionHandler::gc()
This commit is contained in:
Nicolas Grekas 2021-07-15 10:23:19 +02:00
commit 6c8114ad2a
10 changed files with 28 additions and 19 deletions

View File

@ -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;
}
/**

View File

@ -63,7 +63,7 @@ class MigratingSessionHandler implements \SessionHandlerInterface, \SessionUpdat
}
/**
* @return bool
* @return int|false
*/
#[\ReturnTypeWillChange]
public function gc($maxlifetime)

View File

@ -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();
}
/**

View File

@ -70,11 +70,11 @@ class NullSessionHandler extends AbstractSessionHandler
}
/**
* @return bool
* @return int|false
*/
#[\ReturnTypeWillChange]
public function gc($maxlifetime)
{
return true;
return 0;
}
}

View File

@ -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;
}
/**

View File

@ -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;
}
/**

View File

@ -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()

View File

@ -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

View File

@ -113,7 +113,7 @@ class MemcachedSessionHandlerTest extends TestCase
public function testGcSession()
{
$this->assertTrue($this->storage->gc(123));
$this->assertIsInt($this->storage->gc(123));
}
/**

View File

@ -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()