[ticket/16955] Add stubs for cache classes and clean up

PHPBB3-16955
This commit is contained in:
Marc Alexander 2022-12-27 16:12:53 +01:00
parent b855b8a5a5
commit 83cb41e72a
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
15 changed files with 153 additions and 119 deletions

View file

@ -0,0 +1,24 @@
<?php
class APCUIterator implements Iterator
{
public function current()
{
}
public function next()
{
}
public function key()
{
}
public function valid()
{
}
public function rewind()
{
}
}

View file

@ -0,0 +1,20 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
/** @link https://www.php.net/manual/en/memcached.constants.php */
class Memcached
{
public const OPT_COMPRESSION = -1001;
public const OPT_BINARY_PROTOCOL = 18;
}

View file

@ -0,0 +1,21 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
/** @link https://phpredis.github.io/phpredis/Redis.html */
class Redis
{
public const OPT_PREFIX = 2;
public const SERIALIZER_PHP = 1;
}

View file

@ -10,6 +10,7 @@
* the docs/CREDITS.txt file. * the docs/CREDITS.txt file.
* *
*/ */
class SQLite3 class SQLite3
{ {
public function query(string $query) {} public function query(string $query) {}

View file

@ -38,39 +38,25 @@ class apcu extends \phpbb\cache\driver\memory
} }
/** /**
* Fetch an item from the cache * {@inheritDoc}
*
* @access protected
* @param string $var Cache key
* @return mixed Cached data
*/ */
function _read($var) protected function _read(string $var)
{ {
return apcu_fetch($this->key_prefix . $var); return apcu_fetch($this->key_prefix . $var);
} }
/** /**
* Store data in the cache * {@inheritDoc}
*
* @access protected
* @param string $var Cache key
* @param mixed $data Data to store
* @param int $ttl Time-to-live of cached data
* @return bool True if the operation succeeded
*/ */
function _write($var, $data, $ttl = 2592000) protected function _write(string $var, $data, int $ttl = 2592000): bool
{ {
return apcu_store($this->key_prefix . $var, $data, $ttl); return apcu_store($this->key_prefix . $var, $data, $ttl);
} }
/** /**
* Remove an item from the cache * {@inheritDoc}
* */
* @access protected protected function _delete(string $var): bool
* @param string $var Cache key
* @return bool True if the operation succeeded
*/
function _delete($var)
{ {
return apcu_delete($this->key_prefix . $var); return apcu_delete($this->key_prefix . $var);
} }

View file

@ -199,7 +199,7 @@ abstract class base implements \phpbb\cache\driver\driver_interface
* *
* @param string $dir Directory to remove * @param string $dir Directory to remove
* *
* @return null * @return void
*/ */
protected function remove_dir($dir) protected function remove_dir($dir)
{ {
@ -231,4 +231,13 @@ abstract class base implements \phpbb\cache\driver\driver_interface
@rmdir($dir); @rmdir($dir);
} }
/**
* Fetch an item from the cache
*
* @param string $var Cache key
*
* @return mixed Cached data
*/
abstract protected function _read(string $var);
} }

View file

@ -95,7 +95,7 @@ interface driver_interface
* *
* @param string $query SQL query * @param string $query SQL query
* *
* @return int|bool Query ID (integer) if cache contains a rowset * @return string|false Query ID (md5 of query) if cache contains a rowset
* for the specified query. * for the specified query.
* False otherwise. * False otherwise.
*/ */

View file

@ -95,6 +95,14 @@ class dummy extends \phpbb\cache\driver\base
return false; return false;
} }
/**
* {@inheritDoc}
*/
protected function _read(string $var)
{
return false;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View file

@ -331,17 +331,13 @@ class file extends \phpbb\cache\driver\base
} }
/** /**
* Read cached data from a specified file * {@inheritDoc}
* */
* @access private protected function _read(string $var)
* @param string $filename Filename to write
* @return mixed False if an error was encountered, otherwise the data type of the cached data
*/
function _read($filename)
{ {
global $phpEx; global $phpEx;
$filename = $this->clean_varname($filename); $filename = $this->clean_varname($var);
$file = "{$this->cache_dir}$filename.$phpEx"; $file = "{$this->cache_dir}$filename.$phpEx";
$type = substr($filename, 0, strpos($filename, '_')); $type = substr($filename, 0, strpos($filename, '_'));

View file

@ -37,7 +37,7 @@ if (!defined('PHPBB_ACM_MEMCACHED'))
/** /**
* ACM for Memcached * ACM for Memcached
*/ */
class memcached extends \phpbb\cache\driver\memory class memcached extends memory
{ {
/** @var string Extension to use */ /** @var string Extension to use */
protected $extension = 'memcached'; protected $extension = 'memcached';
@ -107,26 +107,17 @@ class memcached extends \phpbb\cache\driver\memory
} }
/** /**
* Fetch an item from the cache * {@inheritDoc}
* */
* @param string $var Cache key protected function _read(string $var)
*
* @return mixed Cached data
*/
protected function _read($var)
{ {
return $this->memcached->get($this->key_prefix . $var); return $this->memcached->get($this->key_prefix . $var);
} }
/** /**
* Store data in the cache * {@inheritDoc}
*
* @param string $var Cache key
* @param mixed $data Data to store
* @param int $ttl Time-to-live of cached data
* @return bool True if the operation succeeded
*/ */
protected function _write($var, $data, $ttl = 2592000) protected function _write(string $var, $data, int $ttl = 2592000): bool
{ {
if (!$this->memcached->replace($this->key_prefix . $var, $data, $ttl)) if (!$this->memcached->replace($this->key_prefix . $var, $data, $ttl))
{ {
@ -136,12 +127,9 @@ class memcached extends \phpbb\cache\driver\memory
} }
/** /**
* Remove an item from the cache * {@inheritDoc}
*
* @param string $var Cache key
* @return bool True if the operation succeeded
*/ */
protected function _delete($var) protected function _delete(string $var): bool
{ {
return $this->memcached->delete($this->key_prefix . $var); return $this->memcached->delete($this->key_prefix . $var);
} }

View file

@ -270,13 +270,33 @@ abstract class memory extends \phpbb\cache\driver\base
/** /**
* Check if a cache var exists * Check if a cache var exists
* *
* @access protected
* @param string $var Cache key * @param string $var Cache key
*
* @return bool True if it exists, otherwise false * @return bool True if it exists, otherwise false
*/ */
function _isset($var) protected function _isset(string $var): bool
{ {
// Most caches don't need to check // Most caches don't need to check
return true; return true;
} }
/**
* Remove an item from the cache
*
* @param string $var Cache key
*
* @return bool True if the operation succeeded
*/
abstract protected function _delete(string $var): bool;
/**
* Store data in the cache
*
* @param string $var Cache key
* @param mixed $data Data to store
* @param int $ttl Time-to-live of cached data
*
* @return bool True if the operation succeeded
*/
abstract protected function _write(string $var, $data, int $ttl = 2592000): bool;
} }

View file

@ -115,27 +115,17 @@ class redis extends \phpbb\cache\driver\memory
} }
/** /**
* Fetch an item from the cache * {@inheritDoc}
* */
* @access protected protected function _read(string $var)
* @param string $var Cache key
* @return mixed Cached data
*/
function _read($var)
{ {
return $this->redis->get($var); return $this->redis->get($var);
} }
/** /**
* Store data in the cache * {@inheritDoc}
*
* @access protected
* @param string $var Cache key
* @param mixed $data Data to store
* @param int $ttl Time-to-live of cached data
* @return bool True if the operation succeeded
*/ */
function _write($var, $data, $ttl = 2592000) protected function _write(string $var, $data, int $ttl = 2592000): bool
{ {
if ($ttl == 0) if ($ttl == 0)
{ {
@ -145,13 +135,9 @@ class redis extends \phpbb\cache\driver\memory
} }
/** /**
* Remove an item from the cache * {@inheritDoc}
* */
* @access protected protected function _delete(string $var): bool
* @param string $var Cache key
* @return bool True if the operation succeeded
*/
function _delete($var)
{ {
if ($this->redis->delete($var) > 0) if ($this->redis->delete($var) > 0)
{ {

View file

@ -31,13 +31,9 @@ class wincache extends \phpbb\cache\driver\memory
} }
/** /**
* Fetch an item from the cache * {@inheritDoc}
* */
* @access protected protected function _read(string $var)
* @param string $var Cache key
* @return mixed Cached data
*/
function _read($var)
{ {
$success = false; $success = false;
$result = wincache_ucache_get($this->key_prefix . $var, $success); $result = wincache_ucache_get($this->key_prefix . $var, $success);
@ -46,27 +42,17 @@ class wincache extends \phpbb\cache\driver\memory
} }
/** /**
* Store data in the cache * {@inheritDoc}
*
* @access protected
* @param string $var Cache key
* @param mixed $data Data to store
* @param int $ttl Time-to-live of cached data
* @return bool True if the operation succeeded
*/ */
function _write($var, $data, $ttl = 2592000) protected function _write(string $var, $data, int $ttl = 2592000): bool
{ {
return wincache_ucache_set($this->key_prefix . $var, $data, $ttl); return wincache_ucache_set($this->key_prefix . $var, $data, $ttl);
} }
/** /**
* Remove an item from the cache * {@inheritDoc}
* */
* @access protected protected function _delete(string $var): bool
* @param string $var Cache key
* @return bool True if the operation succeeded
*/
function _delete($var)
{ {
return wincache_ucache_delete($this->key_prefix . $var); return wincache_ucache_delete($this->key_prefix . $var);
} }

View file

@ -22,8 +22,11 @@
</issueHandlers> </issueHandlers>
<stubs> <stubs>
<file name="build/psalm/stubs/apcu/apcu.php"/>
<file name="build/psalm/stubs/memcached/memcached.php"/>
<file name="build/psalm/stubs/oci8/oci8.php"/> <file name="build/psalm/stubs/oci8/oci8.php"/>
<file name="build/psalm/stubs/pgsql/pgsql.php"/> <file name="build/psalm/stubs/pgsql/pgsql.php"/>
<file name="build/psalm/stubs/redis/redis.php"/>
<file name="build/psalm/stubs/sqlite3/sqlite3.php"/> <file name="build/psalm/stubs/sqlite3/sqlite3.php"/>
</stubs> </stubs>
</psalm> </psalm>

View file

@ -23,40 +23,26 @@ class phpbb_cache_memory extends \phpbb\cache\driver\memory
} }
/** /**
* Fetch an item from the cache * {@inheritDoc}
* */
* @access protected protected function _read(string $var)
* @param string $var Cache key
* @return mixed Cached data
*/
function _read($var)
{ {
return $this->data[$var] ?? false; return $this->data[$var] ?? false;
} }
/** /**
* Store data in the cache * {@inheritDoc}
*
* @access protected
* @param string $var Cache key
* @param mixed $data Data to store
* @param int $ttl Time-to-live of cached data
* @return bool True if the operation succeeded
*/ */
function _write($var, $data, $ttl = 2592000) protected function _write(string $var, $data, int $ttl = 2592000): bool
{ {
$this->data[$var] = $data; $this->data[$var] = $data;
return true; return true;
} }
/** /**
* Remove an item from the cache * {@inheritDoc}
* */
* @access protected protected function _delete(string $var): bool
* @param string $var Cache key
* @return bool True if the operation succeeded
*/
function _delete($var)
{ {
unset($this->data[$var]); unset($this->data[$var]);
return true; return true;