Skip to content

Commit

Permalink
Fix tedious#416 Session class incompatibility with PHP 7.x/8+
Browse files Browse the repository at this point in the history
Due to difference in SessionInterfaceHandler interface in PHP 7.x and PHP 8+
gc method return type was ommited and suppressed warning as PHP 8+
It's better for future to make different version of Stash for PHP 7.x and PHP 8+

Minor change was made also to remove cmpatibility with PHP version before 5.4.0
while Stash package is compatible with 7+.
  • Loading branch information
mitkola committed Nov 19, 2022
1 parent a2c9df1 commit f1925f5
Showing 1 changed file with 21 additions and 32 deletions.
53 changes: 21 additions & 32 deletions src/Stash/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
namespace Stash;

use Stash\Interfaces\PoolInterface;
use Stash\Session\SessionHandlerInterface as SessionHandlerInterface;
/*
* Rough fix for SessionHandlerInterface difference in gc
* return type after version PHP 8.0.0.
* To be compatible with PHP 7.x versions we fixed return type
* and suppress depreciation warning
*/
if (version_compare(PHP_VERSION,"8.0.0",">=")) {
$__ERROR_REPORTING_FIXED = error_reporting(E_ALL ^ E_DEPRECATED);
}

/**
* Stash\Session lets developers use Stash's Pool class to back session storage.
Expand Down Expand Up @@ -55,41 +63,14 @@ class Session implements \SessionHandlerInterface
protected $options = array();

/**
* Registers a Session object with PHP as the session handler. This
* eliminates some boilerplate code from projects while also helping with
* the differences in php versions.
* Registers a Session object with PHP as the session handler.
*
* @param \Stash\Session $handler
* @return bool
*/
public static function registerHandler(Session $handler): bool
{
// this isn't possible to test with the CLI phpunit test
// @codeCoverageIgnoreStart

if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
return session_set_save_handler($handler, true);
} else {
$results = session_set_save_handler(
array($handler, 'open'),
array($handler, 'close'),
array($handler, 'read'),
array($handler, 'write'),
array($handler, 'destroy'),
array($handler, 'gc')
);

if (!$results) {
return false;
}

// the following prevents unexpected effects when using objects as save handlers
register_shutdown_function('session_write_close');

return true;
}

// @codeCoverageIgnoreEnd
return session_set_save_handler($handler, true);
}

/**
Expand Down Expand Up @@ -223,11 +204,19 @@ public function destroy($session_id) : bool
* gc_probability as zero) and call the "purge" function on the Stash\Pool
* class directly.
*
* @param int $maxlifetime
* @param int|false $maxlifetime
* @return bool
*/
public function gc($maxlifetime) : int|false
//#[ReturnTypeWillChange]
public function gc($maxlifetime)
{
return $this->pool->purge();
}
}

/*
* Restore error reporting to previous state
*/
if (isset($__ERROR_REPORTING_FIXED)) {
error_reporting($__ERROR_REPORTING_FIXED);
}

0 comments on commit f1925f5

Please sign in to comment.