Add stricter psalm type for CappedMemoryCache

Signed-off-by: Carl Schwan <carl@carlschwan.eu>
pull/32250/head
Carl Schwan 2022-05-03 11:15:24 +07:00
parent 5a0b28d603
commit ec6b83cc18
16 changed files with 74 additions and 161 deletions

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

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

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

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

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

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

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

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

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

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

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

@ -47,9 +47,9 @@ class File implements \OCP\Encryption\IFile {
/** /**
* cache results of already checked folders * cache results of already checked folders
* *
* @var array * @var CappedMemoryCache<array>
*/ */
protected $cache; protected CappedMemoryCache $cache;
public function __construct(Util $util, public function __construct(Util $util,
IRootFolder $rootFolder, 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 * @param string $path to the file
* @return array ['users' => $uniqueUserIds, 'public' => $public] * @return array{users: string[], public: bool}
*/ */
public function getAccessList($path) { 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 * get the owner and the path for the file relative to the owners files folder
* *
* @param string $path * @param string $path
* @return array * @return array{0: string, 1: string}
* @throws \BadMethodCallException * @throws \BadMethodCallException
*/ */
public function getUidAndFilename($path) { public function getUidAndFilename($path) {

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

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

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