Skip to content

Commit 9f580e4

Browse files
committed
chore: apply strict rector rules on appstore
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 3b69f9f commit 9f580e4

17 files changed

Lines changed: 257 additions & 129 deletions

File tree

apps/appstore/lib/AppInfo/Application.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,19 @@
1313
use OCP\AppFramework\Bootstrap\IBootstrap;
1414
use OCP\AppFramework\Bootstrap\IRegistrationContext;
1515

16-
class Application extends App implements IBootstrap {
16+
final class Application extends App implements IBootstrap {
1717
public const APP_ID = 'appstore';
1818

19-
/**
20-
* @param array $urlParams
21-
*/
2219
public function __construct(array $urlParams = []) {
2320
parent::__construct(self::APP_ID, $urlParams);
2421
}
2522

23+
#[\Override()]
2624
public function register(IRegistrationContext $context): void {
2725
$context->registerSearchProvider(AppSearch::class);
2826
}
2927

28+
#[\Override()]
3029
public function boot(IBootContext $context): void {
3130
}
3231
}

apps/appstore/lib/Controller/ApiController.php

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ class ApiController extends OCSController {
4343

4444
public function __construct(
4545
IRequest $request,
46-
private IConfig $config,
47-
private IAppConfig $appConfig,
48-
private AppManager $appManager,
49-
private DependencyAnalyzer $dependencyAnalyzer,
50-
private CategoryFetcher $categoryFetcher,
51-
private AppFetcher $appFetcher,
52-
private IFactory $l10nFactory,
53-
private BundleFetcher $bundleFetcher,
54-
private Installer $installer,
55-
private IRegistry $subscriptionRegistry,
56-
private LoggerInterface $logger,
46+
private readonly IConfig $config,
47+
private readonly IAppConfig $appConfig,
48+
private readonly AppManager $appManager,
49+
private readonly DependencyAnalyzer $dependencyAnalyzer,
50+
private readonly CategoryFetcher $categoryFetcher,
51+
private readonly AppFetcher $appFetcher,
52+
private readonly IFactory $l10nFactory,
53+
private readonly BundleFetcher $bundleFetcher,
54+
private readonly Installer $installer,
55+
private readonly IRegistry $subscriptionRegistry,
56+
private readonly LoggerInterface $logger,
5757
) {
5858
parent::__construct(Application::APP_ID, $request);
5959
}
@@ -70,33 +70,34 @@ public function listCategories(): DataResponse {
7070
$currentLanguage = substr($this->l10nFactory->findLanguage(), 0, 2);
7171

7272
$categories = $this->categoryFetcher->get();
73-
$categories = array_map(fn ($category) => [
73+
$categories = array_map(fn (array $category): array => [
7474
'id' => $category['id'],
7575
'displayName' => $category['translations'][$currentLanguage]['name'] ?? $category['translations']['en']['name'],
7676
], $categories);
7777

78-
return new DataResponse(array_values($categories));
78+
return new DataResponse($categories);
7979
}
8080

8181
/**
8282
* Get all available apps
8383
*
84-
* @return DataResponse<Http::STATUS_OK, list<array{id: string, name: string, description: string, ...}>, array{}>
84+
* @return DataResponse<Http::STATUS_OK, list<array{id: string, name: string, groups: list<string>, internal: bool, isCompatible: bool, missingDependencies?: list<string>, missingMaxNextcloudVersion: bool, missingMinNextcloudVersion: bool, ...<array-key, mixed>}>, array{}>
8585
*
8686
* 200: The apps were found successfully
8787
*/
8888
#[ApiRoute(verb: 'GET', url: '/api/v1/apps')]
8989
public function listApps(): DataResponse {
9090
$apps = $this->getAllApps();
9191

92+
/** @var array<string>|mixed $ignoreMaxApps */
9293
$ignoreMaxApps = $this->config->getSystemValue('app_install_overwrite', []);
9394
if (!is_array($ignoreMaxApps)) {
9495
$this->logger->warning('The value given for app_install_overwrite is not an array. Ignoring...');
9596
$ignoreMaxApps = [];
9697
}
9798

9899
// Extend existing app details
99-
$apps = array_map(function (array $appData) use ($ignoreMaxApps) {
100+
$apps = array_map(function (array $appData) use ($ignoreMaxApps): array {
100101
if (isset($appData['appstoreData'])) {
101102
$appstoreData = $appData['appstoreData'];
102103
$appData['screenshot'] = $this->createProxyPreviewUrl($appstoreData['screenshots'][0]['url'] ?? '');
@@ -105,7 +106,7 @@ public function listApps(): DataResponse {
105106
}
106107

107108
$newVersion = $this->installer->isUpdateAvailable($appData['id']);
108-
if ($newVersion) {
109+
if ($newVersion !== false) {
109110
$appData['update'] = $newVersion;
110111
}
111112

@@ -119,6 +120,7 @@ public function listApps(): DataResponse {
119120
$groups = [$groups];
120121
}
121122
}
123+
122124
$appData['groups'] = $groups;
123125
$appData['canUninstall'] = !$appData['active'] && $appData['removable'];
124126

@@ -132,6 +134,7 @@ public function listApps(): DataResponse {
132134
$appData['missingMaxNextcloudVersion'] = !isset($appData['dependencies']['nextcloud']['@attributes']['max-version']);
133135
$appData['isCompatible'] = $this->dependencyAnalyzer->isMarkedCompatible($appData);
134136

137+
/** @var array{id: string, name: string, groups: list<string>, internal: bool, isCompatible: bool, missingDependencies?: list<string>, missingMaxNextcloudVersion: bool, missingMinNextcloudVersion: bool, ...<array-key, mixed>} $appData */
135138
return $appData;
136139
}, $apps);
137140

@@ -166,16 +169,17 @@ public function enableApp(string $appId, array $groups = []): DataResponse {
166169

167170
$this->installer->installApp($appId);
168171

169-
if (count($groups) > 0) {
172+
if ($groups !== []) {
170173
$this->appManager->enableAppForGroups($appId, $this->getGroupList($groups));
171174
} else {
172175
$this->appManager->enableApp($appId);
173176
}
177+
174178
$updateRequired = $this->appManager->isUpgradeRequired($appId);
175179
return new DataResponse(['update_required' => $updateRequired]);
176-
} catch (\Throwable $e) {
177-
$this->logger->error('could not enable app', ['exception' => $e]);
178-
throw new OCSException('could not enable app', Http::STATUS_INTERNAL_SERVER_ERROR, $e);
180+
} catch (\Throwable $throwable) {
181+
$this->logger->error('could not enable app', ['exception' => $throwable]);
182+
throw new OCSException('could not enable app', Http::STATUS_INTERNAL_SERVER_ERROR, $throwable);
179183
}
180184
}
181185

@@ -196,9 +200,9 @@ public function disableApp(string $appId): DataResponse {
196200
$appId = $this->appManager->cleanAppId($appId);
197201
$this->appManager->disableApp($appId);
198202
return new DataResponse([]);
199-
} catch (\Exception $e) {
200-
$this->logger->error('could not disable app', ['exception' => $e]);
201-
throw new OCSException('could not disable app', Http::STATUS_INTERNAL_SERVER_ERROR, $e);
203+
} catch (\Exception $exception) {
204+
$this->logger->error('could not disable app', ['exception' => $exception]);
205+
throw new OCSException('could not disable app', Http::STATUS_INTERNAL_SERVER_ERROR, $exception);
202206
}
203207
}
204208

@@ -223,6 +227,7 @@ public function uninstallApp(string $appId): DataResponse {
223227
$this->appManager->clearAppsCache();
224228
return new DataResponse([]);
225229
}
230+
226231
throw new OCSException('could not remove app', Http::STATUS_INTERNAL_SERVER_ERROR);
227232
}
228233

@@ -247,13 +252,14 @@ public function updateApp(string $appId): DataResponse {
247252
if ($result === false) {
248253
throw new \Exception('Update failed');
249254
}
250-
} catch (\Exception $ex) {
255+
} catch (\Exception $exception) {
251256
$this->config->setSystemValue('maintenance', false);
252-
throw new OCSException('could not update app', Http::STATUS_INTERNAL_SERVER_ERROR, $ex);
257+
throw new OCSException('could not update app', Http::STATUS_INTERNAL_SERVER_ERROR, $exception);
253258
}
254259

255260
return new DataResponse([]);
256261
}
262+
257263
/**
258264
* Force enable an app.
259265
* This will override the nextcloud version requirement for an app
@@ -278,10 +284,11 @@ private function createProxyPreviewUrl(string $url): string {
278284
if ($url === '') {
279285
return '';
280286
}
287+
281288
return 'https://usercontent.apps.nextcloud.com/' . base64_encode($url);
282289
}
283290

284-
private function fetchApps() {
291+
private function fetchApps(): void {
285292
$appClass = new \OC_App();
286293
$apps = $appClass->listAllApps();
287294
foreach ($apps as $app) {
@@ -296,6 +303,7 @@ private function fetchApps() {
296303

297304
$app['screenshot'] = $this->createProxyPreviewUrl($appScreenshot);
298305
}
306+
299307
$this->allApps[$app['id']] = $app;
300308
}
301309

@@ -332,17 +340,16 @@ private function getAllApps() {
332340
if (empty($this->allApps)) {
333341
$this->fetchApps();
334342
}
343+
335344
return $this->allApps;
336345
}
337346

338347
/**
339348
* Get all apps for a category from the app store
340349
*
341-
* @param string $requestedCategory
342-
* @return array
343350
* @throws \Exception
344351
*/
345-
private function getAppsForCategory($requestedCategory = ''): array {
352+
private function getAppsForCategory(string $requestedCategory = ''): array {
346353
$versionParser = new VersionParser();
347354
$formattedApps = [];
348355
$apps = $this->appFetcher->get();
@@ -355,6 +362,7 @@ private function getAppsForCategory($requestedCategory = ''): array {
355362
$isInCategory = true;
356363
}
357364
}
365+
358366
if (!$isInCategory) {
359367
continue;
360368
}
@@ -363,14 +371,17 @@ private function getAppsForCategory($requestedCategory = ''): array {
363371
if (!isset($app['releases'][0]['rawPlatformVersionSpec'])) {
364372
continue;
365373
}
374+
366375
$nextcloudVersion = $versionParser->getVersion($app['releases'][0]['rawPlatformVersionSpec']);
367376
$nextcloudVersionDependencies = [];
368377
if ($nextcloudVersion->getMinimumVersion() !== '') {
369378
$nextcloudVersionDependencies['nextcloud']['@attributes']['min-version'] = $nextcloudVersion->getMinimumVersion();
370379
}
380+
371381
if ($nextcloudVersion->getMaximumVersion() !== '') {
372382
$nextcloudVersionDependencies['nextcloud']['@attributes']['max-version'] = $nextcloudVersion->getMaximumVersion();
373383
}
384+
374385
$phpVersion = $versionParser->getVersion($app['releases'][0]['rawPhpVersionSpec']);
375386

376387
try {
@@ -384,12 +395,15 @@ private function getAppsForCategory($requestedCategory = ''): array {
384395
if ($phpVersion->getMinimumVersion() !== '') {
385396
$phpDependencies['php']['@attributes']['min-version'] = $phpVersion->getMinimumVersion();
386397
}
398+
387399
if ($phpVersion->getMaximumVersion() !== '') {
388400
$phpDependencies['php']['@attributes']['max-version'] = $phpVersion->getMaximumVersion();
389401
}
402+
390403
if (isset($app['releases'][0]['minIntSize'])) {
391404
$phpDependencies['php']['@attributes']['min-int-size'] = $app['releases'][0]['minIntSize'];
392405
}
406+
393407
$authors = '';
394408
foreach ($app['authors'] as $key => $author) {
395409
$authors .= $author['name'];
@@ -454,24 +468,28 @@ private function getAppsForCategory($requestedCategory = ''): array {
454468
return $formattedApps;
455469
}
456470

457-
private function getGroupList(array $groups) {
471+
/**
472+
* @param string[] $groups - The group ids to fetch
473+
* @return list<IGroup> - The list of groups matching the given group ids
474+
*/
475+
private function getGroupList(array $groups): array {
458476
$groupManager = Server::get(IGroupManager::class);
459477
$groupsList = [];
460478
foreach ($groups as $group) {
461479
$groupItem = $groupManager->get($group);
462480
if ($groupItem instanceof IGroup) {
463-
$groupsList[] = $groupManager->get($group);
481+
$groupsList[] = $groupItem;
464482
}
465483
}
484+
466485
return $groupsList;
467486
}
468487

469-
private function sortApps($a, $b) {
470-
$a = (string)$a['name'];
471-
$b = (string)$b['name'];
472-
if ($a === $b) {
473-
return 0;
474-
}
475-
return ($a < $b) ? -1 : 1;
488+
/**
489+
* @param array{name: string, ...} $a
490+
* @param array{name: string, ...} $b
491+
*/
492+
private function sortApps(array $a, array $b): int {
493+
return $a['name'] <=> $b['name'];
476494
}
477495
}

apps/appstore/lib/Controller/DiscoverController.php

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@
3232
use Psr\Log\LoggerInterface;
3333

3434
#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
35-
class DiscoverController extends Controller {
35+
final class DiscoverController extends Controller {
3636

37-
private IAppData $appData;
37+
private readonly IAppData $appData;
3838

3939
public function __construct(
4040
IRequest $request,
4141
IAppDataFactory $appDataFactory,
42-
private IClientService $clientService,
43-
private AppDiscoverFetcher $discoverFetcher,
44-
private LoggerInterface $logger,
42+
private readonly IClientService $clientService,
43+
private readonly AppDiscoverFetcher $discoverFetcher,
44+
private readonly LoggerInterface $logger,
4545
) {
4646
parent::__construct(Application::APP_ID, $request);
4747
$this->appData = $appDataFactory->get(Application::APP_ID);
@@ -69,28 +69,25 @@ public function getAppDiscoverMedia(string $fileName, ILimiter $limiter, IUserSe
6969
$getEtag = $this->discoverFetcher->getETag() ?? date('Y-m');
7070
$etag = trim($getEtag, '"');
7171

72-
$folder = null;
7372
try {
7473
$folder = $this->appData->getFolder('app-discover-cache');
7574
$this->cleanUpImageCache($folder, $etag);
76-
} catch (\Throwable $e) {
75+
} catch (\Throwable) {
7776
$folder = $this->appData->newFolder('app-discover-cache');
7877
}
7978

8079
// Get the current cache folder
8180
try {
8281
$folder = $folder->getFolder($etag);
83-
} catch (NotFoundException $e) {
82+
} catch (NotFoundException) {
8483
$folder = $folder->newFolder($etag);
8584
}
8685

8786
$info = pathinfo($fileName);
8887
$hashName = md5($fileName);
8988
$allFiles = $folder->getDirectoryListing();
9089
// Try to find the file
91-
$file = array_filter($allFiles, function (ISimpleFile $file) use ($hashName) {
92-
return str_starts_with($file->getName(), $hashName);
93-
});
90+
$file = array_filter($allFiles, fn (ISimpleFile $file): bool => str_starts_with($file->getName(), $hashName));
9491
// Get the first entry
9592
$file = reset($file);
9693
// If not found request from Web
@@ -150,15 +147,11 @@ private function checkCanDownloadMedia(string $filename): bool {
150147
// Hosts that need further verification
151148
// Github is only allowed if from our organization
152149
$ALLOWED_HOSTS = ['github.com', 'raw.githubusercontent.com'];
153-
if (!in_array($urlInfo['host'], $ALLOWED_HOSTS)) {
150+
if (!in_array($urlInfo['host'], $ALLOWED_HOSTS, true)) {
154151
return false;
155152
}
156153

157-
if (str_starts_with($urlInfo['path'], '/nextcloud/') || str_starts_with($urlInfo['path'], '/nextcloud-gmbh/')) {
158-
return true;
159-
}
160-
161-
return false;
154+
return str_starts_with($urlInfo['path'], '/nextcloud/') || str_starts_with($urlInfo['path'], '/nextcloud-gmbh/');
162155
}
163156

164157
/**
@@ -174,7 +167,7 @@ private function cleanUpImageCache(ISimpleFolder $folder, string $etag): void {
174167
if ($dir->getName() !== $etag) {
175168
$dir->delete();
176169
}
177-
} catch (NotPermittedException $e) {
170+
} catch (NotPermittedException) {
178171
// ignore folder for now
179172
}
180173
}

0 commit comments

Comments
 (0)