]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add stricter psalm type for CappedMemoryCache 32250/head
authorCarl Schwan <carl@carlschwan.eu>
Tue, 3 May 2022 09:15:24 +0000 (11:15 +0200)
committerCarl Schwan <carl@carlschwan.eu>
Thu, 12 May 2022 13:06:18 +0000 (15:06 +0200)
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
16 files changed:
apps/files_external/lib/Lib/Storage/AmazonS3.php
apps/files_external/lib/Lib/Storage/SMB.php
apps/files_sharing/lib/MountProvider.php
apps/files_sharing/lib/SharedMount.php
apps/user_ldap/lib/Group_LDAP.php
apps/user_ldap/lib/Helper.php
apps/user_ldap/lib/User/Manager.php
apps/workflowengine/lib/Manager.php
build/psalm-baseline.xml
lib/private/Cache/CappedMemoryCache.php
lib/private/Diagnostics/QueryLogger.php
lib/private/Encryption/File.php
lib/private/Encryption/Util.php
lib/private/Files/AppData/AppData.php
lib/private/Files/Config/UserMountCache.php
tests/lib/Files/Config/UserMountCacheTest.php

index d3e9605e5a1fb77189372fe6a2bd3d6e9a4b6f0d..e7a8c5f9329504c7bf85c399be1a103b19f46eb5 100644 (file)
@@ -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];
index c2555f87b9359b474fc908d6f3af27c995997a63..bdb9b4f9c8f818e893d33e6d37dc73059778a9de 100644 (file)
@@ -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()) {
index bfb403876220361f680a417dadaf9907b9c132e9..d27f9e5e0da3493894cf416c7b34fc3ede755670 100644 (file)
@@ -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 {
index 398da5eaf23906a74d7864b92f8b8e4436730b8b..95ff66c4b711297b3f55c29279f921ed3f7a58cf 100644 (file)
@@ -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(
index f9d9b06174303c5a1cc7e865c0a4c53121f251da..8fcb10cb850d076860bd75acad281ec7821e5c50 100644 (file)
@@ -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 */
index 437fab6b6a8331bac8cc8966b73947dcb99b2909..3ca5de67874853589e6100652cc96cb29a6982fe 100644 (file)
@@ -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) {
index e752b113e3f12acdc0deff6419d65dab38fdb646..e52b093f5af8edafe8904a50b7cd186f38156fae 100644 (file)
@@ -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,
index 34dbf507b9191e034c89fc3a7a610fd1107600a9..f6c3e3086c2918087bfa23b9b2bf4a05cb8ee247 100644 (file)
@@ -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);
index 7152a3fbffef14c0879c140353a8d84a0de335b4..60b1a435339cb37e4c5a38bd8c5402be7f249004 100644 (file)
     </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>
     <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>
     <InvalidOperand occurrences="1">
       <code>$result</code>
     </InvalidOperand>
-    <InvalidPropertyAssignmentValue occurrences="1">
-      <code>[]</code>
-    </InvalidPropertyAssignmentValue>
     <InvalidReturnStatement occurrences="1">
       <code>$result</code>
     </InvalidReturnStatement>
       <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>
     <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">
index 0a3300435ebe38928fef9ba68767ef5ed3e679fc..6063b5e7110f2086fc65c6033feeb20d35aa6cc6 100644 (file)
@@ -88,7 +88,7 @@ class CappedMemoryCache implements ICache, \ArrayAccess {
        }
 
        /**
-        * @param string $key
+        * @param string $offset
         * @param T $value
         * @return void
         */
index 499947178a31f77fd7f3ff73ae221aacff05ba70..40d68d94ae339868ca8686eb8184812b8b8fed66 100644 (file)
@@ -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;
                }
        }
index 2c486dfade675863194fb5dea86885aa27095942..2d7e23a888359255da8906a530a3e56a8ca82e1e 100644 (file)
@@ -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) {
 
index dc878ba8fc133c31085adc8cd2f8043cf36c94d4..693e24c472138ea76a017299d40186d229150917 100644 (file)
@@ -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) {
index 53f69be71274a984b611bb60dd41f7b9dc9afeeb..471de799c2f47acaae96009f0b2b5dd128159f86 100644 (file)
@@ -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.
index a5fe04c2cac9df08a5a9f087632fead6c5aef79d..c326eeb0b6c82a55d8209e56a45b85247329f858 100644 (file)
@@ -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) {
index f4c6a427abd4980551b2b906bbf07aae0f78f004..221159bc983beb57426787491a1c73aaf1053b3d 100644 (file)
@@ -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() {