Skip to content

Commit a8428f8

Browse files
authored
Merge pull request #59002 from nextcloud/fix/remove-static-vars
Remove usage of static vars or properties
2 parents 3bf88ae + 6a69c43 commit a8428f8

40 files changed

Lines changed: 411 additions & 265 deletions

apps/encryption/lib/Crypto/Encryption.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ class Encryption implements IEncryptionModule {
5656
/** @var int Current version of the file */
5757
private int $version = 0;
5858

59-
/** @var array remember encryption signature version */
59+
/**
60+
* @var array remember encryption signature version
61+
* @psalm-suppress ImpureStaticProperty
62+
*/
6063
private static $rememberVersion = [];
6164

6265
public function __construct(

apps/files_external/lib/Service/BackendService.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class BackendService {
6262

6363
/** @var IConfigHandler[] */
6464
private array $configHandlers = [];
65+
private bool $eventSent = false;
6566

6667
public function __construct(
6768
protected readonly IAppConfig $appConfig,
@@ -79,14 +80,14 @@ public function registerBackendProvider(IBackendProvider $provider) {
7980
$this->backendProviders[] = $provider;
8081
}
8182

82-
private function callForRegistrations() {
83-
static $eventSent = false;
84-
if (!$eventSent) {
83+
private function callForRegistrations(): void {
84+
$instance = Server::get(self::class);
85+
if (!$instance->eventSent) {
8586
Server::get(IEventDispatcher::class)->dispatch(
8687
'OCA\\Files_External::loadAdditionalBackends',
8788
new GenericEvent()
8889
);
89-
$eventSent = true;
90+
$instance->eventSent = true;
9091
}
9192
}
9293

apps/files_sharing/lib/SharedStorage.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class SharedStorage extends Jail implements LegacyISharedStorage, ISharedStorage
8989
private IAppConfig $appConfig;
9090
private IShareManager $shareManager;
9191

92+
/**
93+
* @psalm-suppress ImpureStaticProperty
94+
*/
9295
private static int $initDepth = 0;
9396

9497
public function __construct(array $parameters) {

apps/files_trashbin/lib/Command/RestoreAllFiles.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class RestoreAllFiles extends Base {
3030
private const SCOPE_USER = 1;
3131
private const SCOPE_GROUPFOLDERS = 2;
3232

33-
private static array $SCOPE_MAP = [
33+
private const SCOPE_MAP = [
3434
'user' => self::SCOPE_USER,
3535
'groupfolders' => self::SCOPE_GROUPFOLDERS,
3636
'all' => self::SCOPE_ALL
@@ -221,8 +221,8 @@ protected function parseArgs(InputInterface $input): array {
221221
}
222222

223223
protected function parseScope(string $scope): int {
224-
if (isset(self::$SCOPE_MAP[$scope])) {
225-
return self::$SCOPE_MAP[$scope];
224+
if (isset(self::SCOPE_MAP[$scope])) {
225+
return self::SCOPE_MAP[$scope];
226226
}
227227

228228
throw new InvalidOptionException("Invalid scope '$scope'");

apps/files_versions/lib/BlockVersioningOperation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
77
* SPDX-License-Identifier: AGPL-3.0-or-later
88
*/
9+
910
namespace OCA\Files_Versions;
1011

1112
use OCA\Files_Versions\Events\CreateVersionEvent;

apps/files_versions/lib/Listener/CreateVersionListenerForWorkflow.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
77
* SPDX-License-Identifier: AGPL-3.0-or-later
88
*/
9+
910
namespace OCA\Files_Versions\Listener;
1011

1112
use OCA\Files_Versions\BlockVersioningOperation;

apps/files_versions/lib/Listener/RegisterWorkflowIntegrationListener.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
77
* SPDX-License-Identifier: AGPL-3.0-or-later
88
*/
9+
910
namespace OCA\Files_Versions\Listener;
1011

1112
use OCA\Files_Versions\BlockVersioningOperation;

apps/files_versions/lib/Storage.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,17 @@ class Storage {
5656
public const DELETE_TRIGGER_QUOTA_EXCEEDED = 2;
5757

5858
// files for which we can remove the versions after the delete operation was successful
59-
private static $deletedFiles = [];
59+
/**
60+
* @psalm-suppress ImpureStaticProperty
61+
*/
62+
private static array $deletedFiles = [];
6063

61-
private static $sourcePathAndUser = [];
64+
/**
65+
* @psalm-suppress ImpureStaticProperty
66+
*/
67+
private static array $sourcePathAndUser = [];
6268

63-
private static $max_versions_per_interval = [
69+
private const MAX_VERSIONS_PER_INTERVAL = [
6470
//first 10sec, one version every 2sec
6571
1 => ['intervalEndsAfter' => 10, 'step' => 2],
6672
//next minute, one version every 10sec
@@ -763,12 +769,8 @@ protected static function getAutoExpireList($time, $versions) {
763769
$toDelete = []; // versions we want to delete
764770

765771
$interval = 1;
766-
$step = Storage::$max_versions_per_interval[$interval]['step'];
767-
if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) {
768-
$nextInterval = -1;
769-
} else {
770-
$nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'];
771-
}
772+
$step = Storage::MAX_VERSIONS_PER_INTERVAL[$interval]['step'];
773+
$nextInterval = $time - Storage::MAX_VERSIONS_PER_INTERVAL[$interval]['intervalEndsAfter'];
772774

773775
$firstVersion = reset($versions);
774776

@@ -797,12 +799,16 @@ protected static function getAutoExpireList($time, $versions) {
797799
$newInterval = false; // version checked so we can move to the next one
798800
} else { // time to move on to the next interval
799801
$interval++;
800-
$step = Storage::$max_versions_per_interval[$interval]['step'];
802+
if ($interval > count(Storage::MAX_VERSIONS_PER_INTERVAL)) {
803+
/* Should never happen, as last interval has -1 as nextInterval */
804+
throw new \Exception('MAX_VERSIONS_PER_INTERVAL is malformed or there is a logic issue');
805+
}
806+
$step = Storage::MAX_VERSIONS_PER_INTERVAL[$interval]['step'];
801807
$nextVersion = $prevTimestamp - $step;
802-
if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) {
808+
if (Storage::MAX_VERSIONS_PER_INTERVAL[$interval]['intervalEndsAfter'] === -1) {
803809
$nextInterval = -1;
804810
} else {
805-
$nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'];
811+
$nextInterval = $time - Storage::MAX_VERSIONS_PER_INTERVAL[$interval]['intervalEndsAfter'];
806812
}
807813
$newInterval = true; // we changed the interval -> check same version with new interval
808814
}

apps/user_ldap/lib/Access.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use OCA\User_LDAP\Mapping\AbstractMapping;
1717
use OCA\User_LDAP\User\Manager;
1818
use OCA\User_LDAP\User\OfflineUser;
19+
use OCP\Cache\CappedMemoryCache;
1920
use OCP\EventDispatcher\IEventDispatcher;
2021
use OCP\HintException;
2122
use OCP\IAppConfig;
@@ -49,6 +50,7 @@ class Access extends LDAPUtility {
4950
protected $groupMapper;
5051

5152
private string $lastCookie = '';
53+
private CappedMemoryCache $intermediates;
5254

5355
public function __construct(
5456
ILDAPWrapper $ldap,
@@ -62,6 +64,7 @@ public function __construct(
6264
) {
6365
parent::__construct($ldap);
6466
$this->userManager->setLdapAccess($this);
67+
$this->intermediates = new CappedMemoryCache();
6568
}
6669

6770
/**
@@ -484,8 +487,7 @@ public function dn2username($fdn) {
484487
* @throws \Exception
485488
*/
486489
public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped = null, ?array $record = null, bool $autoMapping = true) {
487-
static $intermediates = [];
488-
if (isset($intermediates[($isUser ? 'user-' : 'group-') . $fdn])) {
490+
if (isset($this->intermediates[($isUser ? 'user-' : 'group-') . $fdn])) {
489491
return false; // is a known intermediate
490492
}
491493

@@ -532,7 +534,7 @@ public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped
532534
$record = $this->readAttributes($fdn, $attributesToRead, $filter);
533535
if ($record === false) {
534536
$this->logger->debug('Cannot read attributes for ' . $fdn . '. Skipping.', ['filter' => $filter]);
535-
$intermediates[($isUser ? 'user-' : 'group-') . $fdn] = true;
537+
$this->intermediates[($isUser ? 'user-' : 'group-') . $fdn] = true;
536538
return false;
537539
}
538540
}
@@ -579,7 +581,7 @@ public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped
579581
$ldapName = $record[$nameAttribute];
580582
if (!isset($ldapName[0]) || empty($ldapName[0])) {
581583
$this->logger->debug('No or empty name for ' . $fdn . ' with filter ' . $filter . '.', ['app' => 'user_ldap']);
582-
$intermediates['group-' . $fdn] = true;
584+
$this->intermediates['group-' . $fdn] = true;
583585
return false;
584586
}
585587
$ldapName = $ldapName[0];

apps/user_ldap/lib/AccessFactory.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-License-Identifier: AGPL-3.0-or-later
68
*/
79

810
namespace OCA\User_LDAP;
911

12+
use OCA\User_LDAP\Mapping\GroupMapping;
13+
use OCA\User_LDAP\Mapping\UserMapping;
1014
use OCA\User_LDAP\User\Manager;
1115
use OCP\EventDispatcher\IEventDispatcher;
1216
use OCP\IAppConfig;
@@ -15,6 +19,8 @@
1519
use Psr\Log\LoggerInterface;
1620

1721
class AccessFactory {
22+
/** @var array<string,Access> */
23+
private array $accesses = [];
1824

1925
public function __construct(
2026
private ILDAPWrapper $ldap,
@@ -23,6 +29,8 @@ public function __construct(
2329
private IUserManager $ncUserManager,
2430
private LoggerInterface $logger,
2531
private IEventDispatcher $dispatcher,
32+
private UserMapping $userMapping,
33+
private GroupMapping $groupMapping,
2634
) {
2735
}
2836

@@ -39,4 +47,19 @@ public function get(Connection $connection): Access {
3947
$this->dispatcher,
4048
);
4149
}
50+
51+
public function getAccessForPrefix(string $configPrefix): Access {
52+
if (!isset(self::$accesses[$configPrefix])) {
53+
$this->addAccess($configPrefix);
54+
}
55+
return $this->accesses[$configPrefix];
56+
}
57+
58+
private function addAccess(string $configPrefix): void {
59+
$connector = new Connection($this->ldap, $configPrefix);
60+
$access = $this->get($connector);
61+
$access->setUserMapper($this->userMapping);
62+
$access->setGroupMapper($this->groupMapping);
63+
$this->accesses[$configPrefix] = $access;
64+
}
4265
}

0 commit comments

Comments
 (0)