Skip to content

Commit

Permalink
bug #42114 [HttpFoundation] Fix return types of SessionHandler::gc() …
Browse files Browse the repository at this point in the history
…(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
-------

1261a41 [HttpFoundation] Fix return types of SessionHandler::gc()
  • Loading branch information
nicolas-grekas committed Jul 15, 2021
2 parents b1fbd1b + 1261a41 commit 6c8114a
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@ protected function doDestroy($sessionId)
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function destroy($sessionId)
}

/**
* @return bool
* @return int|false
*/
#[\ReturnTypeWillChange]
public function gc($maxlifetime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,14 @@ protected function doDestroy($sessionId)
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ protected function doDestroy($sessionId)
}

/**
* @return bool
* @return int|false
*/
#[\ReturnTypeWillChange]
public function gc($maxlifetime)
{
return true;
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public function read($sessionId)
}

/**
* @return bool
* @return int|false
*/
#[\ReturnTypeWillChange]
public function gc($maxlifetime)
Expand All @@ -299,7 +299,7 @@ public function gc($maxlifetime)
// 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,13 @@ public function close(): bool

/**
* {@inheritdoc}
*
* @return int|false
*/
public function gc($maxlifetime): bool
#[\ReturnTypeWillChange]
public function gc($maxlifetime)
{
return true;
return 0;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function testDestroySession()

public function testGcSession()
{
$this->assertTrue($this->storage->gc(123));
$this->assertIsInt($this->storage->gc(123));
}

public function testUpdateTimestamp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function testDestroySession()

public function testGcSession()
{
$this->assertTrue($this->storage->gc(123));
$this->assertIsInt($this->storage->gc(123));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,14 @@ public function testGc()
->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()
Expand Down

0 comments on commit 6c8114a

Please sign in to comment.