Skip to content

Commit ec6ff10

Browse files
authored
Merge pull request #4018 from nextcloud/fix/happy-psalm
Fixing psalm hints
2 parents 838e7ac + 614859e commit ec6ff10

14 files changed

Lines changed: 65 additions & 56 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"psalm": "psalm --no-diff",
4949
"test:unit": "phpunit -c tests/phpunit.xml",
5050
"test:integration": "phpunit -c tests/phpunit.xml",
51-
"psalm:fix": "psalm --alter --php-version=8.1 --issues=MissingReturnType,InvalidReturnType,InvalidNullableReturnType,MismatchingDocblockParamType,MismatchingDocblockReturnType,MissingParamType,InvalidFalsableReturnType",
51+
"psalm:fix": "psalm --alter --issues=MissingReturnType,InvalidReturnType,InvalidNullableReturnType,MismatchingDocblockParamType,MismatchingDocblockReturnType,MissingParamType,InvalidFalsableReturnType",
5252
"psalm:info": "psalm --no-diff --show-info=true",
5353
"psalm:baseline": "psalm --set-baseline=psalm-baseline.xml",
5454
"psalm:baseline:update": "psalm --update-baseline",

lib/Controller/BaseApiV2Controller.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* SPDX-FileCopyrightText: 2024 Nextcloud contributors
66
* SPDX-License-Identifier: AGPL-3.0-or-later
77
*/
8-
98
namespace OCA\Polls\Controller;
109

1110
use Closure;
@@ -21,6 +20,7 @@
2120

2221
/**
2322
* @psalm-api
23+
* @psalm-import-type HttpStatusCode from \OCA\Polls\Types
2424
*/
2525
class BaseApiV2Controller extends OCSController {
2626
public function __construct(
@@ -36,6 +36,7 @@ public function __construct(
3636
/**
3737
* response
3838
* @param Closure $callback Callback function
39+
* @psalm-param HttpStatusCode $successStatus HTTP status code for success
3940
*/
4041
#[NoAdminRequired]
4142
protected function response(Closure $callback, int $successStatus = Http::STATUS_OK): DataResponse {
@@ -48,7 +49,7 @@ protected function response(Closure $callback, int $successStatus = Http::STATUS
4849
} catch (Exception $e) {
4950

5051
if ($e->getStatus() === Http::STATUS_NOT_MODIFIED) {
51-
return new DataResponse(statusCode: $e->getStatus());
52+
return new DataResponse(statusCode: Http::STATUS_NOT_MODIFIED);
5253
}
5354

5455
throw new OCSBadRequestException($e->getMessage());

lib/Controller/BaseController.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
/**
2020
* @psalm-api
21+
* @psalm-import-type HttpStatusCode from \OCA\Polls\Types
2122
*/
2223
class BaseController extends Controller {
2324
public function __construct(
@@ -30,7 +31,7 @@ public function __construct(
3031
/**
3132
* response
3233
* @param Closure $callback Callback function
33-
* @param int $successStatus HTTP status code for success
34+
* @psalm-param HttpStatusCode $successStatus HTTP status code for success
3435
*/
3536
#[NoAdminRequired]
3637
protected function response(
@@ -42,10 +43,12 @@ protected function response(
4243
} catch (Exception $e) {
4344

4445
if ($e->getStatus() === Http::STATUS_NOT_MODIFIED) {
45-
return new JSONResponse(statusCode: $e->getStatus());
46+
return new JSONResponse(statusCode: Http::STATUS_NOT_MODIFIED);
4647
}
4748

48-
return new JSONResponse(['message' => $e->getMessage()], $e->getStatus());
49+
/** @var HttpStatusCode $status */
50+
$status = $e->getStatus();
51+
return new JSONResponse(['message' => $e->getMessage()], $status);
4952
}
5053
}
5154

lib/Controller/SystemController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public function __construct(
3636
#[OpenAPI(OpenAPI::SCOPE_IGNORE)]
3737
#[FrontpageRoute(verb: 'GET', url: '/search/users/{query}')]
3838
public function userSearch(string $query, string $types): JSONResponse {
39-
$types = explode(',', $types);
39+
$types = array_map('intval', explode(',', $types));
40+
// $types = explode(',', $types);
4041
return new JSONResponse([
4142
'siteusers' => $this->systemService->getSiteUsersAndGroups($query, $types),
4243
'types' => $types

lib/Exceptions/DuplicateEntryException.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

lib/Model/Settings/SystemSettings.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,20 @@ public function getShareCreateAllowed(): bool {
3939
// get group exceptions
4040
$exceptionGroups = $this->getCoreLimitSharingGroups();
4141

42-
if ($groupExceptionMode === 'allowGroup') {
43-
// exception mode is 'Limit sharing to some groups'
44-
// if user is in exception group, allow share creation
45-
return $this->userSession->getCurrentUser()->getIsInGroupArray($exceptionGroups);
46-
} elseif ($groupExceptionMode === 'denyGroup') {
42+
if ($groupExceptionMode === 'denyGroup') {
4743
// exception mode is 'Exclude some Groups from sharing'
4844
// if user is in exception group, deny share creation
4945
return !$this->userSession->getCurrentUser()->getIsInGroupArray($exceptionGroups);
5046
}
5147

52-
return true;
48+
// exception mode is 'Limit sharing to some groups'
49+
// if user is in exception group, allow share creation
50+
return $this->userSession->getCurrentUser()->getIsInGroupArray($exceptionGroups);
5351
}
5452

5553
/**
5654
* Get share group exception mode
57-
* @return string
55+
* @return 'open'|'closed'|'archived'
5856
* @psalm-return 'denyGroup'|'allowGroup'|'off'
5957
* Take value from the core setting 'shareapi_exclude_groups' and translate
6058
* 'yes' => 'denyGroup' ('Exclude some groups from sharing') existing groups are handeled as deny groups

lib/Service/MailService.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ public static function extractEmailAddressAndName($eMailString): array {
100100
preg_match(self::REGEX_PARSE_MAIL_AND_NAME, $eMailString, $matches);
101101

102102
// Check if the found element is a valid email address
103-
$emailAddress = !empty($matches[1]) ? trim($matches[1]) : null;
104-
103+
$emailAddress = (isset($matches[1]) && trim($matches[1]) !== '') ? trim($matches[1]) : null;
105104
if ($emailAddress !== null && filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
106105
// Extract the name based on the input string
107106
$displayName = trim(str_replace(['<', '>'], '', str_replace($emailAddress, '', $eMailString)));

lib/Service/OptionService.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use OCA\Polls\Event\OptionUnconfirmedEvent;
2121
use OCA\Polls\Event\OptionUpdatedEvent;
2222
use OCA\Polls\Event\PollOptionReorderedEvent;
23-
use OCA\Polls\Exceptions\DuplicateEntryException;
2423
use OCA\Polls\Exceptions\InvalidPollTypeException;
2524
use OCA\Polls\Model\Sequence;
2625
use OCA\Polls\Model\SimpleOption;
@@ -168,11 +167,7 @@ public function addBulk(int $pollId, string $bulkText = ''): array {
168167

169168
foreach ($newOptionsTexts as $pollOptionText) {
170169
if ($pollOptionText) {
171-
try {
172-
$this->add($pollId, new SimpleOption($pollOptionText, 0));
173-
} catch (DuplicateEntryException $e) {
174-
continue;
175-
}
170+
$this->add($pollId, new SimpleOption($pollOptionText, 0));
176171
}
177172
}
178173

lib/Service/PollService.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,22 @@ public function takeover(int $pollId, ?UserBase $targetUser = null): Poll {
135135
/**
136136
* Transfer ownership of a poll
137137
* @param int|Poll $poll poll or pollId of poll to transfer ownership
138-
* @param null|string|UserBase $targetUser User to transfer polls to. If null the current user will be used
138+
* @param string|UserBase $targetUser User to transfer polls to. If null the current user will be used
139139
*/
140-
public function transferPoll(int|Poll $poll, null|string|UserBase $targetUser): Poll {
140+
public function transferPoll(int|Poll $poll, string|UserBase $targetUser): Poll {
141141
if (!($poll instanceof Poll)) {
142142
$poll = $this->pollMapper->find($poll);
143143
}
144144

145145
$poll->request(Poll::PERMISSION_POLL_CHANGE_OWNER);
146146

147147
if (!($targetUser instanceof UserBase)) {
148+
$userId = $targetUser;
148149
try {
149-
$targetUser = $this->userMapper->getUserFromUserBase($targetUser);
150+
$targetUser = $this->userMapper->getUserFromUserBase($userId);
150151
} catch (UserNotFoundException $e) {
151-
throw new InvalidUsernameException('The user id "' . $targetUser . '" for the target user is not valid.');
152+
// to keep psalm quiet
153+
throw new InvalidUsernameException('The user id "' . $userId . '" for the target user is not valid.');
152154
}
153155
}
154156

lib/Service/SystemService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function getGroups(string $query = ''): array {
7070
/**
7171
* Get a combined list of users, groups, circles, contact groups and contacts
7272
*
73+
* @param list<int> $types list of types to search for
7374
* @return (Circle|Email|Group|User|Contact|ContactGroup|mixed)[]
7475
*
7576
* @psalm-return array<array-key, Circle|Email|Group|User|Contact|ContactGroup|mixed>

0 commit comments

Comments
 (0)