Skip to content

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
alecpl committed Mar 23, 2024
1 parent 3a76c9b commit 818945a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 74 deletions.
60 changes: 0 additions & 60 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -600,36 +600,6 @@ parameters:
count: 1
path: program/lib/Roundcube/bootstrap.php

-
message: "#^Parameter \\#1 \\$key \\(string\\) of method rcube_cache_db\\:\\:store_record\\(\\) should be contravariant with parameter \\$key \\(mixed\\) of method rcube_cache\\:\\:store_record\\(\\)$#"
count: 1
path: program/lib/Roundcube/cache/db.php

-
message: "#^Parameter \\#3 \\$ts \\(DateTime\\) of method rcube_cache_db\\:\\:store_record\\(\\) should be contravariant with parameter \\$ts \\(mixed\\) of method rcube_cache\\:\\:store_record\\(\\)$#"
count: 1
path: program/lib/Roundcube/cache/db.php

-
message: "#^Offset non\\-falsy\\-string does not exist on array\\{\\}\\.$#"
count: 1
path: program/lib/Roundcube/cache/memcache.php

-
message: "#^Call to function method_exists\\(\\) with Redis and 'del' will always evaluate to true\\.$#"
count: 1
path: program/lib/Roundcube/cache/redis.php

-
message: "#^Negated boolean expression is always false\\.$#"
count: 3
path: program/lib/Roundcube/cache/redis.php

-
message: "#^Static property rcube_cache_redis\\:\\:\\$redis \\(Redis\\) does not accept false\\.$#"
count: 3
path: program/lib/Roundcube/cache/redis.php

-
message: "#^Call to function is_array\\(\\) with array will always evaluate to true\\.$#"
count: 2
Expand Down Expand Up @@ -1415,36 +1385,6 @@ parameters:
count: 2
path: program/lib/Roundcube/rcube_result_thread.php

-
message: "#^Call to function is_array\\(\\) with array will always evaluate to true\\.$#"
count: 1
path: program/lib/Roundcube/rcube_session.php

-
message: "#^Method rcube_session\\:\\:unserialize\\(\\) should return array but returns false\\.$#"
count: 1
path: program/lib/Roundcube/rcube_session.php

-
message: "#^Return type \\(bool\\) of method rcube_session\\:\\:gc\\(\\) should be covariant with return type \\(int\\|false\\) of method SessionHandlerInterface\\:\\:gc\\(\\)$#"
count: 1
path: program/lib/Roundcube/rcube_session.php

-
message: "#^Strict comparison using \\!\\=\\= between string and null will always evaluate to true\\.$#"
count: 1
path: program/lib/Roundcube/rcube_session.php

-
message: "#^Variable \\$id might not be defined\\.$#"
count: 1
path: program/lib/Roundcube/rcube_session.php

-
message: "#^Variable \\$length might not be defined\\.$#"
count: 1
path: program/lib/Roundcube/rcube_session.php

-
message: "#^Call to function is_array\\(\\) with array will always evaluate to true\\.$#"
count: 1
Expand Down
6 changes: 3 additions & 3 deletions program/lib/Roundcube/cache/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ protected function read_record($key)
/**
* Writes single cache record into DB.
*
* @param string $key Cache key name
* @param mixed $data Serialized cache data
* @param DateTime $ts Timestamp
* @param string $key Cache key name
* @param mixed $data Serialized cache data
* @param ?DateTime $ts Timestamp
*
* @return bool True on success, False on failure
*/
Expand Down
6 changes: 4 additions & 2 deletions program/lib/Roundcube/cache/memcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ public static function engine()
$available = 0;

// Callback for memcache failure
$error_callback = static function ($host, $port) use ($seen, $available) {
$error_callback = static function ($host, $port) use (&$seen, $available) {
// only report once
if (!$seen["{$host}:{$port}"]++) {
if (!array_key_exists("{$host}:{$port}", $seen)) {
$seen["{$host}:{$port}"] = true;
$available--;

rcube::raise_error([
'code' => 604, 'type' => 'memcache',
'line' => __LINE__, 'file' => __FILE__,
Expand Down
3 changes: 2 additions & 1 deletion program/lib/Roundcube/cache/redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class rcube_cache_redis extends rcube_cache
/**
* Instance of Redis object
*
* @var Redis
* @var Redis|false|null
*/
protected static $redis;

Expand Down Expand Up @@ -229,6 +229,7 @@ protected function delete_item($key)
}

try {
// @phpstan-ignore-next-line
$result = method_exists(self::$redis, 'del')
? self::$redis->del($key)
: self::$redis->delete($key);
Expand Down
6 changes: 6 additions & 0 deletions program/lib/Roundcube/rcube_cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,12 @@ protected function load_index()

/**
* Write data entry into cache
*
* @param string $key Cache key name
* @param mixed $data Serialized cache data
* @param ?DateTime $ts Timestamp
*
* @return bool True on success, False on failure
*/
protected function store_record($key, $data, $ts = null)
{
Expand Down
18 changes: 10 additions & 8 deletions program/lib/Roundcube/rcube_session.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function write($key, $vars)
*
* @param int $maxlifetime Maximum session lifetime
*
* @return bool True on success, False on failure
* @return int|false Number of deleted sessions on success, False on failure
*/
#[ReturnTypeWillChange]
public function gc($maxlifetime)
Expand All @@ -204,7 +204,7 @@ public function gc($maxlifetime)
// see rcube::shutdown() and rcube_session::write_close()
$this->gc_enabled = $maxlifetime;

return true;
return 0;
}

/**
Expand Down Expand Up @@ -356,9 +356,9 @@ protected function get_cache($key)
*
* Warning: Do not use if you already modified $_SESSION in the same request (#1490608)
*
* @param string $path Path denoting the session variable where to append the value
* @param string $key Key name under which to append the new value (use null for appending to an indexed list)
* @param mixed $value Value to append to the session data array
* @param string $path Path denoting the session variable where to append the value
* @param ?string $key Key name under which to append the new value (use null for appending to an indexed list)
* @param mixed $value Value to append to the session data array
*/
public function append($path, $key, $value)
{
Expand Down Expand Up @@ -510,7 +510,7 @@ protected function serialize($vars)
*
* @param string $str Serialized data string
*
* @return array Unserialized data
* @return array|false Unserialized data
*/
public static function unserialize($str)
{
Expand Down Expand Up @@ -562,7 +562,8 @@ public static function unserialize($str)
break;
case 'r': // reference
$q += 2;
for ($id = ''; ($q < $endptr) && ($str[$q] != ';'); $q++) {
$id = '';
for (; ($q < $endptr) && ($str[$q] != ';'); $q++) {
$id .= $str[$q];
}
$q++;
Expand All @@ -575,7 +576,8 @@ public static function unserialize($str)
break;
case 's': // string
$q += 2;
for ($length = ''; ($q < $endptr) && ($str[$q] != ':'); $q++) {
$length = '';
for (; ($q < $endptr) && ($str[$q] != ':'); $q++) {
$length .= $str[$q];
}
$q += 2;
Expand Down

0 comments on commit 818945a

Please sign in to comment.