Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hartmann <[email protected]>
  • Loading branch information
Chartman123 committed Oct 29, 2024
1 parent cfcde6b commit 841aed3
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 101 deletions.
25 changes: 13 additions & 12 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@
use Psr\Log\LoggerInterface;

/**
* @psalm-import-type FormsPartialForm from ResponseDefinitions
* @psalm-import-type FormsForm from ResponseDefinitions
* @psalm-import-type FormsQuestion from ResponseDefinitions
* @psalm-import-type FormsOption from ResponseDefinitions
* @psalm-import-type FormsOrder from ResponseDefinitions
* @psalm-import-type FormsPartialForm from ResponseDefinitions
* @psalm-import-type FormsQuestion from ResponseDefinitions
* @psalm-import-type FormsSubmissions from ResponseDefinitions
* @psalm-import-type FormsUploadedFile from ResponseDefinitions
*/
Expand Down Expand Up @@ -512,7 +513,7 @@ public function newQuestion(int $formId, ?string $type = null, string $text = ''

$question = $this->questionMapper->insert($question);

$response = $question->read();
$response = $this->formsService->getQuestion($question->getId());

Check warning on line 516 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L516

Added line #L516 was not covered by tests
$response['options'] = [];
$response['accept'] = [];
} else {
Expand Down Expand Up @@ -703,11 +704,11 @@ public function deleteQuestion(int $formId, int $questionId): DataResponse {
* Updates the Order of all Questions of a Form
*
* @param int $formId Id of the form to reorder
* @param array<string, int> $newOrder Array of Question-Ids in new order.
* @return DataResponse<Http::STATUS_OK, array<string, int>, array{}>
* @param list<int> $newOrder Array of Question-Ids in new order.
* @return DataResponse<Http::STATUS_OK, array<string, FormsOrder>, array{}>
* @throws OCSBadRequestException The given array contains duplicates
* @throws OCSBadRequestException The length of the given array does not match the number of stored questions
* @throws OCSBadRequestException Question doesn\'t belong to given Form
* @throws OCSBadRequestException Question doesn't belong to given Form
* @throws OCSBadRequestException One question has already been marked as deleted
* @throws OCSForbiddenException This form is archived and can not be modified
* @throws OCSForbiddenException User has no permissions to get this form
Expand Down Expand Up @@ -786,7 +787,7 @@ public function reorderQuestions(int $formId, array $newOrder): DataResponse {
foreach ($questions as $question) {
$this->questionMapper->update($question);

$response[$question->getId()] = [
$response[(string)$question->getId()] = [

Check warning on line 790 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L790

Added line #L790 was not covered by tests
'order' => $question->getOrder()
];
}
Expand Down Expand Up @@ -881,7 +882,7 @@ public function newOption(int $formId, int $questionId, array $optionTexts): Dat
* @param int $formId id of form
* @param int $questionId id of question
* @param int $optionId id of option to update
* @param array{key: string, value: mixed} $keyValuePairs Array of key=>value pairs to update.
* @param array<string, mixed> $keyValuePairs Array of key=>value pairs to update.
* @return DataResponse<Http::STATUS_OK, int, array{}> Returns the id of the updated option
* @throws OCSBadRequestException The given option id doesn't match the question or form
* @throws OCSForbiddenException This form is archived and can not be modified
Expand Down Expand Up @@ -1008,8 +1009,8 @@ public function deleteOption(int $formId, int $questionId, int $optionId): DataR
* Reorder options for a given question
* @param int $formId id of form
* @param int $questionId id of question
* @param array<string, int> $newOrder Array of option ids in new order.
* @return DataResponse<Http::STATUS_OK, array<string, int>, array{}>
* @param list<int> $newOrder Array of option ids in new order.
* @return DataResponse<Http::STATUS_OK, array<string, FormsOrder>, array{}>
* @throws OCSBadRequestException The given question id doesn't match the form
* @throws OCSBadRequestException The given array contains duplicates
* @throws OCSBadRequestException The length of the given array does not match the number of stored options
Expand Down Expand Up @@ -1112,7 +1113,7 @@ public function reorderOptions(int $formId, int $questionId, array $newOrder) {
* - `csv`: Comma-separated value
* - `ods`: OpenDocument Spreadsheet
* - `xlsx`: Excel Open XML Spreadsheet
* @return DataResponse<Http::STATUS_OK, FormsSubmissions, array{}>|DataDownloadResponse<Http::STATUS_OK, 'text/csv'|'application/vnd.oasis.opendocument.spreadsheet'|'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', array{}>
* @return DataResponse<Http::STATUS_OK, list<FormsSubmissions>, array{}>|DataDownloadResponse<Http::STATUS_OK, 'text/csv'|'application/vnd.oasis.opendocument.spreadsheet'|'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', array{}>

Check failure on line 1116 in lib/Controller/ApiController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable30

MoreSpecificReturnType

lib/Controller/ApiController.php:1116:13: MoreSpecificReturnType: The declared return type 'OCP\AppFramework\Http\DataDownloadResponse<200, 'application/vnd.oasis.opendocument.spreadsheet'|'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'|'text/csv', array<never, never>>|OCP\AppFramework\Http\DataResponse<200, list<array{questions: list<array{accept: list<string>, description: string, extraSettings: array{allowOtherAnswer?: bool|null, allowedFileExtensions?: list<string>|null, allowedFileTypes?: list<string>|null, maxAllowedFilesCount?: int|null, maxFileSize?: int|null, optionsLimitMax?: int|null, optionsLimitMin?: int|null, shuffleOptions?: bool|null, validationRegex?: null|string, validationType?: null|string}, formId: int, id: int, isRequired: bool, name: string, options: list<array{id: int, order: int|null, questionId: int, text: string}>, order: int, text: string, type: 'date'|'dropdown'|'file'|'long'|'multiple'|'multiple_unique'|'short'|'time'}>, submissions: list<array{answers: list<array{id: int, questionId: int, submissionId: int, text: string}>, formId: int, id: int, timestamp: int, userDisplayName: string, userId: string}>}>, array<never, never>>' for OCA\Forms\Controller\ApiController::getSubmissions is more specific than the inferred return type 'OCP\AppFramework\Http\DataDownloadResponse<200, 'application/vnd.oasis.opendocument.spreadsheet'|'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'|'text/csv', array<never, never>>|OCP\AppFramework\Http\DataResponse<200, array{questions: array<array-key, mixed>, submissions: array<array-key, mixed>}, array<never, never>>' (see https://psalm.dev/070)
* @throws OCSNotFoundException Could not find form
* @throws OCSForbiddenException The current user has no permission to get the results for this form
*
Expand Down Expand Up @@ -1396,7 +1397,7 @@ public function exportSubmissionsToCloud(int $formId, string $path, string $file
* @param int $formId id of the form
* @param int $questionId id of the question
* @param string $shareHash hash of the form share
* @return DataResponse<Http::STATUS_OK, FormsUploadedFile, array{}>
* @return DataResponse<Http::STATUS_OK, list<FormsUploadedFile>, array{}>
* @throws OCSBadRequestException No files provided
* @throws OCSBadRequestException Question doesn't belong to the given form
* @throws OCSBadRequestException Invalid file provided
Expand Down
16 changes: 8 additions & 8 deletions lib/Controller/ShareApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ public function __construct(
* @param int $formId The form to share
* @param int $shareType Nextcloud-ShareType
* @param string $shareWith ID of user/group/... to share with. For Empty shareWith and shareType Link, this will be set as RandomID.
* @param array<string> $permissions the permissions granted on the share, defaults to `submit`
* Possible values:
* - `submit` user can submit
* - `results` user can see the results
* - `results_delete` user can see and delete results
* @return DataResponse<Http::STATUS_CREATED, FormsShare, array{}>
* @param list<string> $permissions the permissions granted on the share, defaults to `submit`
* Possible values:
* - `submit` user can submit
* - `results` user can see the results
* - `results_delete` user can see and delete results
* @return DataResponse<Http::STATUS_CREATED, array<FormsShare>, array{}>
* @throws OCSBadRequestException Invalid shareType
* @throws OCSBadRequestException Invalid permission given
* @throws OCSBadRequestException Invalid user to share with
Expand Down Expand Up @@ -231,7 +231,7 @@ public function newShare(int $formId, int $shareType, string $shareWith = '', ar
*
* @param int $formId of the form
* @param int $shareId of the share to update
* @param array{key: string, values: mixed} $keyValuePairs Array of key=>value pairs to update.
* @param array<string, mixed> $keyValuePairs Array of key=>value pairs to update.
* @return DataResponse<Http::STATUS_OK, int, array{}>
* @throws OCSBadRequestException Share doesn't belong to given Form
* @throws OCSBadRequestException Invalid permission given
Expand Down Expand Up @@ -277,7 +277,7 @@ public function updateShare(int $formId, int $shareId, array $keyValuePairs): Da
}

//Don't allow to change other properties than permissions
if (count($keyValuePairs) > 1 || !key_exists('permissions', $keyValuePairs)) {
if (count($keyValuePairs) > 1 || !array_key_exists('permissions', $keyValuePairs)) {
$this->logger->debug('Not allowed to update other properties than permissions');
throw new OCSForbiddenException('Not allowed to update other properties than permissions');

Check warning on line 282 in lib/Controller/ShareApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ShareApiController.php#L282

Added line #L282 was not covered by tests
}
Expand Down
89 changes: 57 additions & 32 deletions lib/ResponseDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,42 @@
namespace OCA\Forms;

/**
* @psalm-type FormsPartialForm = array{
* id: int,
* hash: string,
* title: string,
* expires: int,
* permissions: string[],
* partial: bool,
* state: int
* }
*
* @psalm-type FormsOption = array{
* id: int,
* questionId: int,
* text: string,
* order: ?int
* }
*
* @psalm-type FormsOrder = array{
* order: int
* }
*
* @psalm-type FormsQuestionExtraSettings = array{
* allowOtherAnswer?: ?bool,
* allowedFileExtensions?: ?list<string>,
* allowedFileTypes?: ?list<string>,
* maxAllowedFilesCount?: ?int,
* maxFileSize?: ?int,
* optionsLimitMax?: ?int,
* optionsLimitMin?: ?int,
* shuffleOptions?: ?bool,
* validationRegex?: ?string,
* validationType?: ?string
* }
*
* @psalm-type FormsQuestion = array{
* id: int,
* formId: int,
* order: int,
* type: string,
* type: "dropdown"|"multiple"|"multiple_unique"|"date"|"time"|"short"|"long"|"file",
* isRequired: bool,
* text: string,
* name: string,
* options: array<FormsOption>,
* accept: string[],
* extraSettings: \stdClass
* description: string,
* extraSettings: FormsQuestionExtraSettings|array{},
* options: list<FormsOption>|array{},
* accept: list<string>|array{}
* }
*
* @psalm-type FormsAnswer = array{
Expand All @@ -66,13 +74,39 @@
* formId: int,
* userId: string,
* timestamp: int,
* answers: array<FormsAnswer>,
* answers: list<FormsAnswer>,
* userDisplayName: string
* }
*
* @psalm-type FormsSubmissions = array{
* submissions: array<FormsSubmission>,
* questions: array<FormsQuestion>
* submissions: list<FormsSubmission>,
* questions: list<FormsQuestion>
* }
*
* @psalm-type FormsAccess = array{
* permitAllUsers: bool,
* showToAllUsers: bool
* }
*
* @psalm-type FormsPermission = "edit"|"results"|"results_delete"|"submit"|"embed"
*
* @psalm-type FormsShare = array{
* id: int,
* formId: int,
* shareType: int,
* shareWith: string,
* permissions: list<FormsPermission>,
* displayName: string
* }
*
* @psalm-type FormsPartialForm = array{
* id: int,
* hash: string,
* title: string,
* expires: int,
* permissions: list<FormsPermission>,
* partial: true,
* state: int
* }
*
* @psalm-type FormsForm = array{
Expand All @@ -82,32 +116,23 @@
* description: string,
* ownerId: string,
* created: int,
* access: \stdClass,
* access: FormsAccess,
* expires: int,
* isAnonymous: bool,
* submitMultiple: bool,
* showExpiration: bool,
* canSubmit: bool,
* permissions: string[],
* questions: array<FormsQuestion>,
* state: int,
* shares: string[],
* submissions: array<FormsSubmission>,
* permissions: list<FormsPermission>,
* questions: list<FormsQuestion>,
* state: 0|1|2,
* shares: list<string>,
* submissions: list<FormsSubmission>,
* }
*
* @psalm-type FormsUploadedFile = array{
* uploadedFileId: int,
* fileName: string
* }
*
* @psalm-type FormsShare = array{
* id: int,
* formId: int,
* shareType: int,
* shareWith: string,
* permissions: string[],
* displayName: string
* }
*/
class ResponseDefinitions {
}
Loading

0 comments on commit 841aed3

Please sign in to comment.