diff --git a/lib/private/Remote/Api/ApiBase.php b/lib/private/Remote/Api/ApiBase.php index af114bd949801..e885c36313d7f 100644 --- a/lib/private/Remote/Api/ApiBase.php +++ b/lib/private/Remote/Api/ApiBase.php @@ -22,29 +22,24 @@ */ namespace OC\Remote\Api; +use OCP\Http\Client\IClient; use OCP\Http\Client\IClientService; use OCP\Remote\ICredentials; use OCP\Remote\IInstance; class ApiBase { - /** @var IInstance */ - private $instance; - /** @var ICredentials */ - private $credentials; - /** @var IClientService */ - private $clientService; - - public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) { - $this->instance = $instance; - $this->credentials = $credentials; - $this->clientService = $clientService; + public function __construct( + private IInstance $instance, + private ICredentials $credentials, + private IClientService $clientService, + ) { } - protected function getHttpClient() { + protected function getHttpClient(): IClient { return $this->clientService->newClient(); } - protected function addDefaultHeaders(array $headers) { + protected function addDefaultHeaders(array $headers): array { return array_merge([ 'OCS-APIREQUEST' => 'true', 'Accept' => 'application/json' @@ -58,9 +53,9 @@ protected function addDefaultHeaders(array $headers) { * @param array $query * @param array $headers * @return resource|string - * @throws \InvalidArgumentException + * @throws \InvalidArgumentException|\Exception */ - protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { + protected function request(string $method, string $url, array $body = [], array $query = [], array $headers = []) { $fullUrl = trim($this->instance->getFullUrl(), '/') . '/' . $url; $options = [ 'query' => $query, @@ -73,25 +68,14 @@ protected function request($method, $url, array $body = [], array $query = [], a $client = $this->getHttpClient(); - switch ($method) { - case 'get': - $response = $client->get($fullUrl, $options); - break; - case 'post': - $response = $client->post($fullUrl, $options); - break; - case 'put': - $response = $client->put($fullUrl, $options); - break; - case 'delete': - $response = $client->delete($fullUrl, $options); - break; - case 'options': - $response = $client->options($fullUrl, $options); - break; - default: - throw new \InvalidArgumentException('Invalid method ' . $method); - } + $response = match ($method) { + 'get' => $client->get($fullUrl, $options), + 'post' => $client->post($fullUrl, $options), + 'put' => $client->put($fullUrl, $options), + 'delete' => $client->delete($fullUrl, $options), + 'options' => $client->options($fullUrl, $options), + default => throw new \InvalidArgumentException('Invalid method ' . $method), + }; return $response->getBody(); } diff --git a/lib/private/Remote/Api/ApiCollection.php b/lib/private/Remote/Api/ApiCollection.php index 261f7e6c9b07f..124a20bebd394 100644 --- a/lib/private/Remote/Api/ApiCollection.php +++ b/lib/private/Remote/Api/ApiCollection.php @@ -28,24 +28,18 @@ use OCP\Remote\IInstance; class ApiCollection implements IApiCollection { - /** @var IInstance */ - private $instance; - /** @var ICredentials */ - private $credentials; - /** @var IClientService */ - private $clientService; - - public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) { - $this->instance = $instance; - $this->credentials = $credentials; - $this->clientService = $clientService; + public function __construct( + private IInstance $instance, + private ICredentials $credentials, + private IClientService $clientService, + ) { } - public function getCapabilitiesApi() { + public function getCapabilitiesApi(): OCS { return new OCS($this->instance, $this->credentials, $this->clientService); } - public function getUserApi() { + public function getUserApi(): OCS { return new OCS($this->instance, $this->credentials, $this->clientService); } } diff --git a/lib/private/Remote/Api/ApiFactory.php b/lib/private/Remote/Api/ApiFactory.php index 27eaa163496bd..561dad8c71209 100644 --- a/lib/private/Remote/Api/ApiFactory.php +++ b/lib/private/Remote/Api/ApiFactory.php @@ -28,14 +28,12 @@ use OCP\Remote\IInstance; class ApiFactory implements IApiFactory { - /** @var IClientService */ - private $clientService; - - public function __construct(IClientService $clientService) { - $this->clientService = $clientService; + public function __construct( + private IClientService $clientService, + ) { } - public function getApiCollection(IInstance $instance, ICredentials $credentials) { + public function getApiCollection(IInstance $instance, ICredentials $credentials): ApiCollection { return new ApiCollection($instance, $credentials, $this->clientService); } } diff --git a/lib/private/Remote/Api/OCS.php b/lib/private/Remote/Api/OCS.php index e6ee65378c445..65c57192c9078 100644 --- a/lib/private/Remote/Api/OCS.php +++ b/lib/private/Remote/Api/OCS.php @@ -43,7 +43,7 @@ class OCS extends ApiBase implements ICapabilitiesApi, IUserApi { * @throws NotFoundException * @throws \Exception */ - protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { + protected function request(string $method, string $url, array $body = [], array $query = [], array $headers = []): array { try { $response = json_decode(parent::request($method, 'ocs/v2.php/' . $url, $body, $query, $headers), true); } catch (ClientException $e) { @@ -77,7 +77,7 @@ protected function request($method, $url, array $body = [], array $query = [], a * @param string[] $keys * @throws \Exception */ - private function checkResponseArray(array $data, $type, array $keys) { + private function checkResponseArray(array $data, string $type, array $keys): void { foreach ($keys as $key) { if (!array_key_exists($key, $data)) { throw new \Exception('Invalid ' . $type . ' response, expected field ' . $key . ' not found'); @@ -85,7 +85,12 @@ private function checkResponseArray(array $data, $type, array $keys) { } } - public function getUser($userId) { + /** + * @throws ForbiddenException + * @throws NotFoundException + * @throws \Exception + */ + public function getUser($userId): User { $result = $this->request('get', 'cloud/users/' . $userId); $this->checkResponseArray($result, 'user', User::EXPECTED_KEYS); return new User($result); @@ -93,8 +98,10 @@ public function getUser($userId) { /** * @return array The capabilities in the form of [$appId => [$capability => $value]] + * @throws ForbiddenException + * @throws NotFoundException */ - public function getCapabilities() { + public function getCapabilities(): array { $result = $this->request('get', 'cloud/capabilities'); return $result['capabilities']; } diff --git a/lib/private/Remote/Credentials.php b/lib/private/Remote/Credentials.php index 56cf745c34c80..7119dfae680ff 100644 --- a/lib/private/Remote/Credentials.php +++ b/lib/private/Remote/Credentials.php @@ -25,31 +25,17 @@ use OCP\Remote\ICredentials; class Credentials implements ICredentials { - /** @var string */ - private $user; - /** @var string */ - private $password; - - /** - * @param string $user - * @param string $password - */ - public function __construct($user, $password) { - $this->user = $user; - $this->password = $password; + public function __construct( + private string $user, + private string $password, + ) { } - /** - * @return string - */ - public function getUsername() { + public function getUsername(): string { return $this->user; } - /** - * @return string - */ - public function getPassword() { + public function getPassword(): string { return $this->password; } } diff --git a/lib/private/Remote/Instance.php b/lib/private/Remote/Instance.php index 591bc3d6ae018..c8b0b41a5ba8b 100644 --- a/lib/private/Remote/Instance.php +++ b/lib/private/Remote/Instance.php @@ -32,55 +32,53 @@ */ class Instance implements IInstance { /** @var string */ - private $url; - - /** @var ICache */ - private $cache; - - /** @var IClientService */ - private $clientService; + private string $url; /** @var array|null */ - private $status; + private ?array $status; /** * @param string $url * @param ICache $cache * @param IClientService $clientService */ - public function __construct($url, ICache $cache, IClientService $clientService) { + public function __construct( + string $url, + private ICache $cache, + private IClientService $clientService, + ) { $url = str_replace('https://', '', $url); $this->url = str_replace('http://', '', $url); - $this->cache = $cache; - $this->clientService = $clientService; } /** * @return string The url of the remote server without protocol */ - public function getUrl() { + public function getUrl(): string { return $this->url; } /** * @return string The of of the remote server with protocol */ - public function getFullUrl() { + public function getFullUrl(): string { return $this->getProtocol() . '://' . $this->getUrl(); } /** - * @return string The full version string in '13.1.2.3' format + * @return string|null The full version string in '13.1.2.3' format + * @throws NotFoundException */ - public function getVersion() { + public function getVersion(): ?string { $status = $this->getStatus(); return $status['version']; } /** - * @return string 'http' or 'https' + * @return string|null 'http' or 'https' + * @throws NotFoundException */ - public function getProtocol() { + public function getProtocol(): ?string { $status = $this->getStatus(); return $status['protocol']; } @@ -89,8 +87,9 @@ public function getProtocol() { * Check that the remote server is installed and not in maintenance mode * * @return bool + * @throws NotFoundException */ - public function isActive() { + public function isActive(): bool { $status = $this->getStatus(); return $status['installed'] && !$status['maintenance']; } @@ -100,7 +99,7 @@ public function isActive() { * @throws NotFoundException * @throws \Exception */ - private function getStatus() { + private function getStatus(): ?array { if ($this->status) { return $this->status; } @@ -137,7 +136,7 @@ private function getStatus() { * @param string $url * @return bool|string */ - private function downloadStatus($url) { + private function downloadStatus(string $url): bool|string { try { $request = $this->clientService->newClient()->get($url); return $request->getBody(); diff --git a/lib/private/Remote/InstanceFactory.php b/lib/private/Remote/InstanceFactory.php index 6941d06cb2ee3..72cbc9582d5d8 100644 --- a/lib/private/Remote/InstanceFactory.php +++ b/lib/private/Remote/InstanceFactory.php @@ -27,17 +27,13 @@ use OCP\Remote\IInstanceFactory; class InstanceFactory implements IInstanceFactory { - /** @var ICache */ - private $cache; - /** @var IClientService */ - private $clientService; - - public function __construct(ICache $cache, IClientService $clientService) { - $this->cache = $cache; - $this->clientService = $clientService; + public function __construct( + private ICache $cache, + private IClientService $clientService + ) { } - public function getInstance($url) { + public function getInstance($url): Instance { return new Instance($url, $this->cache, $this->clientService); } } diff --git a/lib/private/Remote/User.php b/lib/private/Remote/User.php index 5590fcfba3814..2ed276a944052 100644 --- a/lib/private/Remote/User.php +++ b/lib/private/Remote/User.php @@ -38,102 +38,94 @@ class User implements IUser { 'quota' ]; - /** @var array */ - private $data; - - public function __construct(array $data) { - $this->data = $data; + public function __construct( + private array $data, + ) { } - /** - * @return string - */ - public function getUserId() { + public function getUserId(): string { return $this->data['id']; } - /** - * @return string - */ - public function getEmail() { + public function getEmail(): string { return $this->data['email']; } /** * @return string */ - public function getDisplayName() { + public function getDisplayName(): string { return $this->data['displayname']; } /** * @return string */ - public function getPhone() { + public function getPhone(): string { return $this->data['phone']; } /** * @return string */ - public function getAddress() { + public function getAddress(): string { return $this->data['address']; } /** * @return string */ - public function getWebsite() { + public function getWebsite(): string { return $this->data['website']; } /** * @return string */ - public function getTwitter() { + public function getTwitter(): string { return isset($this->data['twitter']) ? $this->data['twitter'] : ''; } /** * @return string[] */ - public function getGroups() { + public function getGroups(): array { return $this->data['groups']; } /** * @return string */ - public function getLanguage() { + public function getLanguage(): string { return $this->data['language']; } /** - * @return int + * @return int|float */ - public function getUsedSpace() { + public function getUsedSpace(): int|float { return $this->data['quota']['used']; } /** - * @return int + * @return int|float */ - public function getFreeSpace() { + public function getFreeSpace(): int|float { return $this->data['quota']['free']; } /** - * @return int + * @return int|float */ - public function getTotalSpace() { + public function getTotalSpace(): int|float { return $this->data['quota']['total']; } /** - * @return int + * @return int|float */ - public function getQuota() { + public function getQuota(): int|float { return $this->data['quota']['quota']; } } diff --git a/lib/public/Remote/IInstance.php b/lib/public/Remote/IInstance.php index 8c048805434e0..a8216bca3a55e 100644 --- a/lib/public/Remote/IInstance.php +++ b/lib/public/Remote/IInstance.php @@ -46,7 +46,7 @@ public function getUrl(); public function getFullUrl(); /** - * @return string The full version string in '13.1.2.3' format + * @return string|null The full version string in '13.1.2.3' format * * @since 13.0.0 * @deprecated 23.0.0 diff --git a/lib/public/Remote/IUser.php b/lib/public/Remote/IUser.php index 27fa8bc8b43af..2eff18c6c1dac 100644 --- a/lib/public/Remote/IUser.php +++ b/lib/public/Remote/IUser.php @@ -102,7 +102,7 @@ public function getGroups(); public function getLanguage(); /** - * @return int + * @return int|float * * @since 13.0.0 * @deprecated 23.0.0 @@ -110,7 +110,7 @@ public function getLanguage(); public function getUsedSpace(); /** - * @return int + * @return int|float * * @since 13.0.0 * @deprecated 23.0.0 @@ -118,7 +118,7 @@ public function getUsedSpace(); public function getFreeSpace(); /** - * @return int + * @return int|float * * @since 13.0.0 * @deprecated 23.0.0 @@ -126,7 +126,7 @@ public function getFreeSpace(); public function getTotalSpace(); /** - * @return int + * @return int|float * * @since 13.0.0 * @deprecated 23.0.0