diff --git a/application/controllers/ApiV1ContactgroupsController.php b/application/controllers/ApiV1ContactgroupsController.php index 31650a960..505a361de 100644 --- a/application/controllers/ApiV1ContactgroupsController.php +++ b/application/controllers/ApiV1ContactgroupsController.php @@ -17,6 +17,7 @@ use ipl\Web\Filter\QueryString; use ipl\Web\Url; use Ramsey\Uuid\Uuid; +use stdClass; class ApiV1ContactgroupsController extends CompatController { @@ -44,6 +45,8 @@ public function indexAction(): void $results = []; $responseCode = 200; $db = Database::get(); + + /** @var ?string $identifier */ $identifier = $request->getParam('identifier'); if ($identifier && ! Uuid::isValid($identifier)) { @@ -87,6 +90,8 @@ function (Filter\Condition $condition) { if ($identifier !== null) { $stmt->where(['external_uuid = ?' => $identifier]); + + /** @var stdClass|false $result */ $result = $db->fetchOne($stmt); if ($result === false) { @@ -123,6 +128,7 @@ function (Filter\Condition $condition) { $res = $db->select($stmt->offset($offset)); do { + /** @var stdClass $row */ foreach ($res as $i => $row) { $users = $this->fetchUserIdentifiers($row->contactgroup_id); if ($users) { @@ -150,9 +156,7 @@ function (Filter\Condition $condition) { $this->httpBadRequest('Cannot filter on POST'); } - $data = $request->getPost(); - - $this->assertValidData($data); + $data = $this->getValidatedData(); $db->beginTransaction(); @@ -187,9 +191,7 @@ function (Filter\Condition $condition) { $this->httpBadRequest('Identifier is required'); } - $data = $request->getPost(); - - $this->assertValidData($data); + $data = $this->getValidatedData(); if ($identifier !== $data['id']) { $this->httpBadRequest('Identifier mismatch'); @@ -278,6 +280,7 @@ private function fetchUserIdentifiers(int $contactgroupId): ?array */ private function getUserId(string $identifier): int { + /** @var stdClass|false $user */ $user = Database::get()->fetchOne( (new Select()) ->from('contact') @@ -301,6 +304,7 @@ private function getUserId(string $identifier): int */ private function getContactgroupId(string $identifier): ?int { + /** @var stdClass|false $contactgroup */ $contactgroup = Database::get()->fetchOne( (new Select()) ->from('contactgroup') @@ -314,7 +318,11 @@ private function getContactgroupId(string $identifier): ?int /** * Add a new contactgroup with the given data * - * @param array $data + * @param array{ + * id: string, + * name: string, + * users?: string[], + * } $data */ private function addContactgroup(array $data): void { @@ -364,12 +372,17 @@ private function removeContactgroup(int $id): void /** * Assert that the given data contains the required fields * - * @param array $data + * @return array{ + * id: string, + * name: string, + * users?: string[], + * } * * @throws HttpBadRequestException */ - private function assertValidData(array $data): void + private function getValidatedData(): array { + $data = $this->getRequest()->getPost(); $msgPrefix = 'Invalid request body: '; if (! isset($data['id'], $data['name']) @@ -398,5 +411,12 @@ private function assertValidData(array $data): void //TODO: check if users exist, here? } } + + /** @var array{ + * id: string, + * name: string, + * users?: string[], + * } $data */ + return $data; } } diff --git a/application/controllers/ApiV1ContactsController.php b/application/controllers/ApiV1ContactsController.php index 117dffcfb..32d25e7d0 100644 --- a/application/controllers/ApiV1ContactsController.php +++ b/application/controllers/ApiV1ContactsController.php @@ -18,6 +18,7 @@ use ipl\Web\Filter\QueryString; use ipl\Web\Url; use Ramsey\Uuid\Uuid; +use stdClass; class ApiV1ContactsController extends CompatController { @@ -44,6 +45,8 @@ public function indexAction(): void $results = []; $responseCode = 200; $db = Database::get(); + + /** @var ?string $identifier */ $identifier = $request->getParam('identifier'); if ($identifier && ! Uuid::isValid($identifier)) { @@ -91,6 +94,8 @@ function (Filter\Condition $condition) { if ($identifier !== null) { $stmt->where(['external_uuid = ?' => $identifier]); + + /** @var stdClass|false $result */ $result = $db->fetchOne($stmt); if ($result === false) { @@ -136,6 +141,7 @@ function (Filter\Condition $condition) { $res = $db->select($stmt->offset($offset)); do { + /** @var stdClass $row */ foreach ($res as $i => $row) { if ($row->username === null) { unset($row->username); @@ -172,9 +178,7 @@ function (Filter\Condition $condition) { $this->httpBadRequest('Cannot filter on POST'); } - $data = $request->getPost(); - - $this->assertValidData($data); + $data = $this->getValidatedData(); $db->beginTransaction(); @@ -209,9 +213,7 @@ function (Filter\Condition $condition) { $this->httpBadRequest('Identifier is required'); } - $data = $request->getPost(); - - $this->assertValidData($data); + $data = $this->getValidatedData(); if ($identifier !== $data['id']) { $this->httpBadRequest('Identifier mismatch'); @@ -295,6 +297,7 @@ function (Filter\Condition $condition) { */ private function getChannelId(string $channelName): int { + /** @var stdClass|false $channel */ $channel = Database::get()->fetchOne( (new Select()) ->from('channel') @@ -318,6 +321,7 @@ private function getChannelId(string $channelName): int */ private function fetchContactAddresses(int $contactId): ?string { + /** @var array $addresses */ $addresses = Database::get()->fetchPairs( (new Select()) ->from('contact_address') @@ -325,7 +329,11 @@ private function fetchContactAddresses(int $contactId): ?string ->where(['contact_id = ?' => $contactId]) ); - return ! empty($addresses) ? json_encode($addresses) : null; + if (! empty($addresses)) { + return json_encode($addresses) ?: null; + } + + return null; } /** @@ -360,6 +368,7 @@ private function fetchGroupIdentifiers(int $contactId): ?array */ private function getGroupId(string $identifier): int { + /** @var stdClass|false $group */ $group = Database::get()->fetchOne( (new Select()) ->from('contactgroup') @@ -383,6 +392,7 @@ private function getGroupId(string $identifier): int */ protected function getContactId(string $identifier): ?int { + /** @var stdClass|false $contact */ $contact = Database::get()->fetchOne( (new Select()) ->from('contact') @@ -396,7 +406,14 @@ protected function getContactId(string $identifier): ?int /** * Add a new contact with the given data * - * @param array $data + * @param array{ + * id: string, + * full_name: string, + * default_channel: string, + * username?: string, + * groups?: string[], + * addresses?: array + * } $data * * @return void */ @@ -438,7 +455,7 @@ private function assertUniqueUsername(string $username): void $user = Database::get()->fetchOne( (new Select()) ->from('contact') - ->columns(1) + ->columns('1') ->where(['username = ?' => $username]) ); @@ -503,14 +520,20 @@ private function removeContact(int $id): void /** * Assert that the given data contains the required fields * - * @param array $data - * - * @return void + * @return array{ + * id: string, + * full_name: string, + * default_channel: string, + * username?: string, + * groups?: string[], + * addresses?: array + * } * * @throws HttpBadRequestException */ - private function assertValidData(array $data): void + private function getValidatedData(): array { + $data = $this->getRequest()->getPost(); $msgPrefix = 'Invalid request body: '; if (! isset($data['id'], $data['full_name'], $data['default_channel']) @@ -574,5 +597,16 @@ private function assertValidData(array $data): void $this->httpBadRequest($msgPrefix . 'an invalid email address given'); } } + + /** @var array{ + * id: string, + * full_name: string, + * default_channel: string, + * username?: string, + * groups?: string[], + * addresses?: array + * } $data + */ + return $data; } }