Add stricter psalm type for CappedMemoryCache

Signed-off-by: Carl Schwan <carl@carlschwan.eu>
pull/32250/head
Carl Schwan 2 years ago
parent 5a0b28d603
commit ec6b83cc18

@ -54,6 +54,7 @@ use OCP\Files\IMimeTypeDetector;
use OCP\ICacheFactory;
use OCP\IMemcache;
use OCP\Server;
use OCP\ICache;
class AmazonS3 extends \OC\Files\Storage\Common {
use S3ConnectionTrait;
@ -63,23 +64,18 @@ class AmazonS3 extends \OC\Files\Storage\Common {
return false;
}
/** @var CappedMemoryCache|Result[] */
private $objectCache;
/** @var CappedMemoryCache<array|false> */
private CappedMemoryCache $objectCache;
/** @var CappedMemoryCache|bool[] */
private $directoryCache;
/** @var CappedMemoryCache<bool> */
private CappedMemoryCache $directoryCache;
/** @var CappedMemoryCache|array */
private $filesCache;
/** @var CappedMemoryCache<array> */
private CappedMemoryCache $filesCache;
/** @var IMimeTypeDetector */
private $mimeDetector;
/** @var bool|null */
private $versioningEnabled = null;
/** @var IMemcache */
private $memCache;
private IMimeTypeDetector $mimeDetector;
private ?bool $versioningEnabled = null;
private ICache $memCache;
public function __construct($parameters) {
parent::__construct($parameters);
@ -146,10 +142,9 @@ class AmazonS3 extends \OC\Files\Storage\Common {
}
/**
* @param $key
* @return array|false
*/
private function headObject($key) {
private function headObject(string $key) {
if (!isset($this->objectCache[$key])) {
try {
$this->objectCache[$key] = $this->getConnection()->headObject([
@ -165,6 +160,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
}
if (is_array($this->objectCache[$key]) && !isset($this->objectCache[$key]["Key"])) {
/** @psalm-suppress InvalidArgument Psalm doesn't understand nested arrays well */
$this->objectCache[$key]["Key"] = $key;
}
return $this->objectCache[$key];

@ -84,10 +84,8 @@ class SMB extends Common implements INotifyStorage {
*/
protected $root;
/**
* @var IFileInfo[]
*/
protected $statCache;
/** @var CappedMemoryCache<IFileInfo> */
protected CappedMemoryCache $statCache;
/** @var ILogger */
protected $logger;
@ -527,7 +525,7 @@ class SMB extends Common implements INotifyStorage {
}
try {
$this->statCache = [];
$this->statCache = new CappedMemoryCache();
$content = $this->share->dir($this->buildPath($path));
foreach ($content as $file) {
if ($file->isDirectory()) {

@ -109,6 +109,7 @@ class MountProvider implements IMountProvider {
$view = new View('/' . $user->getUID() . '/files');
$ownerViews = [];
$sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($user->getUID());
/** @var CappedMemoryCache<bool> $folderExistCache */
$foldersExistCache = new CappedMemoryCache();
foreach ($superShares as $share) {
try {

@ -96,6 +96,7 @@ class SharedMount extends MountPoint implements MoveableMount {
*
* @param \OCP\Share\IShare $share
* @param SharedMount[] $mountpoints
* @param CappedMemoryCache<bool> $folderExistCache
* @return string
*/
private function verifyMountPoint(

@ -55,12 +55,12 @@ use Psr\Log\LoggerInterface;
class Group_LDAP extends BackendUtility implements GroupInterface, IGroupLDAP, IGetDisplayNameBackend, IDeleteGroupBackend {
protected $enabled = false;
/** @var string[][] $cachedGroupMembers array of users with gid as key */
protected $cachedGroupMembers;
/** @var string[] $cachedGroupsByMember array of groups with uid as key */
protected $cachedGroupsByMember;
/** @var string[] $cachedNestedGroups array of groups with gid (DN) as key */
protected $cachedNestedGroups;
/** @var CappedMemoryCache<string[]> $cachedGroupMembers array of users with gid as key */
protected CappedMemoryCache $cachedGroupMembers;
/** @var CappedMemoryCache<string[]> $cachedGroupsByMember array of groups with uid as key */
protected CappedMemoryCache $cachedGroupsByMember;
/** @var CappedMemoryCache<string[]> $cachedNestedGroups array of groups with gid (DN) as key */
protected CappedMemoryCache $cachedNestedGroups;
/** @var GroupPluginManager */
protected $groupPluginManager;
/** @var LoggerInterface */

@ -35,15 +35,10 @@ use OCP\IConfig;
use OCP\IDBConnection;
class Helper {
/** @var IConfig */
private $config;
/** @var IDBConnection */
private $connection;
/** @var CappedMemoryCache */
protected $sanitizeDnCache;
private IConfig $config;
private IDBConnection $connection;
/** @var CappedMemoryCache<string> */
protected CappedMemoryCache $sanitizeDnCache;
public function __construct(IConfig $config,
IDBConnection $connection) {

@ -47,43 +47,20 @@ use Psr\Log\LoggerInterface;
* cache
*/
class Manager {
/** @var Access */
protected $access;
/** @var IConfig */
protected $ocConfig;
/** @var IDBConnection */
protected $db;
/** @var IUserManager */
protected $userManager;
/** @var INotificationManager */
protected $notificationManager;
/** @var FilesystemHelper */
protected $ocFilesystem;
/** @var LoggerInterface */
protected $logger;
/** @var Image */
protected $image;
/** @param \OCP\IAvatarManager */
protected $avatarManager;
/**
* @var CappedMemoryCache $usersByDN
*/
protected $usersByDN;
/**
* @var CappedMemoryCache $usersByUid
*/
protected $usersByUid;
/** @var IManager */
private $shareManager;
protected ?Access $access = null;
protected IConfig $ocConfig;
protected IDBConnection $db;
protected IUserManager $userManager;
protected INotificationManager $notificationManager;
protected FilesystemHelper $ocFilesystem;
protected LoggerInterface $logger;
protected Image $image;
protected IAvatarManager $avatarManager;
/** @var CappedMemoryCache<User> $usersByDN */
protected CappedMemoryCache $usersByDN;
/** @var CappedMemoryCache<User> $usersByUid */
protected CappedMemoryCache $usersByUid;
private IManager $shareManager;
public function __construct(
IConfig $ocConfig,

@ -109,8 +109,8 @@ class Manager implements IManager {
/** @var ILogger */
protected $logger;
/** @var CappedMemoryCache */
protected $operationsByScope = [];
/** @var CappedMemoryCache<int[]> */
protected CappedMemoryCache $operationsByScope;
/** @var IUserSession */
protected $session;
@ -350,10 +350,11 @@ class Manager implements IManager {
$qb->setParameters(['scope' => $scopeContext->getScope(), 'scopeId' => $scopeContext->getScopeId()]);
$result = $qb->execute();
$this->operationsByScope[$scopeContext->getHash()] = [];
$operations = [];
while (($opId = $result->fetchOne()) !== false) {
$this->operationsByScope[$scopeContext->getHash()][] = (int)$opId;
$operations[] = (int)$opId;
}
$this->operationsByScope[$scopeContext->getHash()] = $operations;
$result->closeCursor();
return in_array($id, $this->operationsByScope[$scopeContext->getHash()], true);

@ -1371,9 +1371,7 @@
</InvalidReturnType>
</file>
<file src="apps/files_external/lib/Lib/Storage/SMB.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>new CappedMemoryCache()</code>
</InvalidPropertyAssignmentValue>
<InvalidPropertyAssignmentValue occurrences="1"/>
<InvalidScalarArgument occurrences="7">
<code>$e-&gt;getCode()</code>
<code>$e-&gt;getCode()</code>
@ -1911,16 +1909,6 @@
<ParadoxicalCondition occurrences="1"/>
</file>
<file src="apps/user_ldap/lib/Group_LDAP.php">
<InvalidArgument occurrences="1">
<code>$this-&gt;cachedGroupsByMember[$uid]</code>
</InvalidArgument>
<InvalidPropertyAssignmentValue occurrences="5">
<code>$this-&gt;cachedGroupsByMember</code>
<code>$this-&gt;cachedNestedGroups</code>
<code>new CappedMemoryCache()</code>
<code>new CappedMemoryCache()</code>
<code>new CappedMemoryCache()</code>
</InvalidPropertyAssignmentValue>
<InvalidReturnStatement occurrences="1">
<code>$groupName</code>
</InvalidReturnStatement>
@ -2116,9 +2104,6 @@
<InvalidOperand occurrences="1">
<code>$result</code>
</InvalidOperand>
<InvalidPropertyAssignmentValue occurrences="1">
<code>[]</code>
</InvalidPropertyAssignmentValue>
<InvalidReturnStatement occurrences="1">
<code>$result</code>
</InvalidReturnStatement>
@ -2989,11 +2974,6 @@
<code>getShareForToken</code>
</UndefinedMethod>
</file>
<file src="lib/private/Encryption/File.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>new CappedMemoryCache()</code>
</InvalidPropertyAssignmentValue>
</file>
<file src="lib/private/Encryption/Keys/Storage.php">
<InvalidNullableReturnType occurrences="1">
<code>deleteUserKey</code>
@ -3123,17 +3103,6 @@
<LessSpecificImplementedReturnType occurrences="1">
<code>array</code>
</LessSpecificImplementedReturnType>
<UndefinedInterfaceMethod occurrences="9">
<code>$this-&gt;cacheInfoCache</code>
<code>$this-&gt;cacheInfoCache</code>
<code>$this-&gt;cacheInfoCache</code>
<code>$this-&gt;mountsForUsers</code>
<code>$this-&gt;mountsForUsers</code>
<code>$this-&gt;mountsForUsers</code>
<code>$this-&gt;mountsForUsers</code>
<code>$this-&gt;mountsForUsers</code>
<code>$this-&gt;mountsForUsers</code>
</UndefinedInterfaceMethod>
</file>
<file src="lib/private/Files/Filesystem.php">
<InvalidNullableReturnType occurrences="1">

@ -88,7 +88,7 @@ class CappedMemoryCache implements ICache, \ArrayAccess {
}
/**
* @param string $key
* @param string $offset
* @param T $value
* @return void
*/

@ -28,15 +28,10 @@ use OC\Cache\CappedMemoryCache;
use OCP\Diagnostics\IQueryLogger;
class QueryLogger implements IQueryLogger {
/**
* @var \OC\Diagnostics\Query
*/
protected $activeQuery;
/**
* @var CappedMemoryCache
*/
protected $queries;
protected int $index = 0;
protected ?Query $activeQuery = null;
/** @var CappedMemoryCache<Query> */
protected CappedMemoryCache $queries;
/**
* QueryLogger constructor.
@ -74,7 +69,8 @@ class QueryLogger implements IQueryLogger {
public function stopQuery() {
if ($this->activated && $this->activeQuery) {
$this->activeQuery->end(microtime(true));
$this->queries[] = $this->activeQuery;
$this->queries[(string)$this->index] = $this->activeQuery;
$this->index++;
$this->activeQuery = null;
}
}

@ -47,9 +47,9 @@ class File implements \OCP\Encryption\IFile {
/**
* cache results of already checked folders
*
* @var array
* @var CappedMemoryCache<array>
*/
protected $cache;
protected CappedMemoryCache $cache;
public function __construct(Util $util,
IRootFolder $rootFolder,
@ -62,10 +62,10 @@ class File implements \OCP\Encryption\IFile {
/**
* get list of users with access to the file
* Get list of users with access to the file
*
* @param string $path to the file
* @return array ['users' => $uniqueUserIds, 'public' => $public]
* @return array{users: string[], public: bool}
*/
public function getAccessList($path) {

@ -220,7 +220,7 @@ class Util {
* get the owner and the path for the file relative to the owners files folder
*
* @param string $path
* @return array
* @return array{0: string, 1: string}
* @throws \BadMethodCallException
*/
public function getUidAndFilename($path) {

@ -38,21 +38,12 @@ use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFolder;
class AppData implements IAppData {
/** @var IRootFolder */
private $rootFolder;
/** @var SystemConfig */
private $config;
/** @var string */
private $appId;
/** @var Folder */
private $folder;
/** @var (ISimpleFolder|NotFoundException)[]|CappedMemoryCache */
private $folders;
private IRootFolder $rootFolder;
private SystemConfig $config;
private string $appId;
private ?Folder $folder = null;
/** @var CappedMemoryCache<ISimpleFolder|NotFoundException> */
private CappedMemoryCache $folders;
/**
* AppData constructor.

@ -36,7 +36,6 @@ use OCP\Files\Config\ICachedMountInfo;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;
use OCP\ICache;
use OCP\IDBConnection;
use OCP\IUser;
use OCP\IUserManager;
@ -46,30 +45,17 @@ use Psr\Log\LoggerInterface;
* Cache mounts points per user in the cache so we can easilly look them up
*/
class UserMountCache implements IUserMountCache {
/**
* @var IDBConnection
*/
private $connection;
/**
* @var IUserManager
*/
private $userManager;
private IDBConnection $connection;
private IUserManager $userManager;
/**
* Cached mount info.
* Map of $userId to ICachedMountInfo.
*
* @var ICache
* @var CappedMemoryCache<ICachedMountInfo[]>
**/
private $mountsForUsers;
private CappedMemoryCache $mountsForUsers;
private LoggerInterface $logger;
/**
* @var ICache
*/
private $cacheInfoCache;
/** @var CappedMemoryCache<array> */
private CappedMemoryCache $cacheInfoCache;
/**
* UserMountCache constructor.
@ -132,6 +118,7 @@ class UserMountCache implements IUserMountCache {
foreach ($addedMounts as $mount) {
$this->addToCache($mount);
/** @psalm-suppress InvalidArgument */
$this->mountsForUsers[$user->getUID()][] = $mount;
}
foreach ($removedMounts as $mount) {

@ -11,6 +11,7 @@ namespace Test\Files\Config;
use OC\DB\QueryBuilder\Literal;
use OC\Files\Mount\MountPoint;
use OC\Files\Storage\Storage;
use OC\Cache\CappedMemoryCache;
use OC\User\Manager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Config\ICachedMountInfo;
@ -114,7 +115,7 @@ class UserMountCacheTest extends TestCase {
}
private function clearCache() {
$this->invokePrivate($this->cache, 'mountsForUsers', [[]]);
$this->invokePrivate($this->cache, 'mountsForUsers', [new CappedMemoryCache()]);
}
public function testNewMounts() {

Loading…
Cancel
Save