From 6d5e117f952a513a8673d29b83ef2c505cf5c693 Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Mon, 25 Nov 2024 20:42:00 +0000 Subject: [PATCH] Type fixes --- src/Connection/Connector.php | 6 ++---- src/Exception/ApiResponseException.php | 4 +--- src/Model/Activity.php | 4 ++-- src/Model/ApiResourceBase.php | 15 +++++---------- src/Model/Catalog.php | 2 +- src/Model/Environment.php | 3 +-- src/Model/Git/Blob.php | 2 +- src/Model/Plan.php | 2 +- src/Model/Project.php | 3 +-- src/Model/Project/Settings.php | 4 ++-- src/Model/Result.php | 5 ++--- src/Model/SetupOptions.php | 2 +- src/Model/SshKey.php | 2 +- src/Model/Subscription.php | 6 +++--- src/PlatformClient.php | 23 ++++++++--------------- 15 files changed, 32 insertions(+), 51 deletions(-) diff --git a/src/Connection/Connector.php b/src/Connection/Connector.php index bbc164a..232ae9a 100644 --- a/src/Connection/Connector.php +++ b/src/Connection/Connector.php @@ -27,7 +27,7 @@ class Connector implements ConnectorInterface protected ?ClientInterface $client = null; - protected $oauthMiddleware; + protected ?GuzzleMiddleware $oauthMiddleware = null; protected ?Platformsh $provider = null; @@ -356,10 +356,8 @@ protected function loadToken(): ?AccessToken * Get an OAuth2 middleware to add to Guzzle clients. * * @throws \RuntimeException - * - * @return GuzzleMiddleware */ - protected function getOauthMiddleware(): GuzzleMiddleware|callable|null + protected function getOauthMiddleware(): GuzzleMiddleware { if (! $this->oauthMiddleware) { if (! $this->isLoggedIn()) { diff --git a/src/Exception/ApiResponseException.php b/src/Exception/ApiResponseException.php index 77dd6a1..c0d0f93 100644 --- a/src/Exception/ApiResponseException.php +++ b/src/Exception/ApiResponseException.php @@ -17,10 +17,8 @@ class ApiResponseException extends RequestException { /** * Wraps a GuzzleException. - * - * @return GuzzleException */ - public static function wrapGuzzleException(GuzzleException $e): RequestException|GuzzleException + public static function wrapGuzzleException(GuzzleException $e): GuzzleException { return $e instanceof RequestException ? self::alterMessage($e) : $e; } diff --git a/src/Model/Activity.php b/src/Model/Activity.php index 237bcaf..b08b8dd 100644 --- a/src/Model/Activity.php +++ b/src/Model/Activity.php @@ -51,11 +51,11 @@ class Activity extends ApiResourceBase /** * Wait for the activity to complete. * - * @param callable $onPoll A function that will be called every time + * @param callable|null $onPoll A function that will be called every time * the activity is polled for updates. It * will be passed one argument: the * Activity object. - * @param callable $onLog A function that will print new activity log + * @param callable|null $onLog A function that will print new activity log * messages as they are received. It will be * passed one argument: the message as a * string. Deprecated: use readLog() instead. diff --git a/src/Model/ApiResourceBase.php b/src/Model/ApiResourceBase.php index 97b9df0..a339b10 100644 --- a/src/Model/ApiResourceBase.php +++ b/src/Model/ApiResourceBase.php @@ -157,8 +157,7 @@ public static function get(string $id, ?string $collectionUrl, ClientInterface $ return new static($data, $url, $client, true); } catch (BadResponseException $e) { - $response = $e->getResponse(); - if ($response && $response->getStatusCode() === 404) { + if ($e->getResponse()->getStatusCode() === 404) { return false; } throw $e; @@ -169,6 +168,7 @@ public static function get(string $id, ?string $collectionUrl, ClientInterface $ * Create a resource. * * @return Result + * @noinspection PhpMissingReturnTypeInspection */ public static function create(array $body, string $collectionUrl, ClientInterface $client) { @@ -179,7 +179,7 @@ public static function create(array $body, string $collectionUrl, ClientInterfac $request = new Request('post', $collectionUrl, [ 'Content-Type' => 'application/json', - ], \GuzzleHttp\json_encode($body)); + ], \GuzzleHttp\Utils::jsonEncode($body)); $data = self::send($request, $client); return new Result($data, $collectionUrl, $client, static::class); @@ -349,11 +349,8 @@ public function getProperty(string $property, bool $required = true, bool $lazyL /** * Delete the resource. - * - * @return Result */ - #[\ReturnTypeWillChange] - public function delete() + public function delete(): Result { $data = $this->sendRequest($this->getUri(), 'delete'); @@ -364,10 +361,8 @@ public function delete() * Update the resource. * * This updates the resource's internal data with the API response. - * - * @return Result */ - public function update(array $values) + public function update(array $values): Result { if ($errors = $this->checkUpdate($values)) { $message = 'Cannot update resource due to validation error(s): ' . implode('; ', $errors); diff --git a/src/Model/Catalog.php b/src/Model/Catalog.php index b7e774b..2cb40c3 100644 --- a/src/Model/Catalog.php +++ b/src/Model/Catalog.php @@ -22,7 +22,7 @@ public static function create(array $data, string $url, ClientInterface $client) { $request = new Request('post', $url, [ 'Content-Type' => 'application/json', - ], \GuzzleHttp\json_encode($data)); + ], Utils::jsonEncode($data)); $response = $client->send($request); $data = Utils::jsonDecode($response->getBody()->__toString(), true); $items = []; diff --git a/src/Model/Environment.php b/src/Model/Environment.php index 502f06a..db7c24c 100644 --- a/src/Model/Environment.php +++ b/src/Model/Environment.php @@ -309,7 +309,6 @@ public static function validateId(string $id): bool * * @throws EnvironmentStateException */ - #[\ReturnTypeWillChange] public function delete(): Result { if ($this->isActive()) { @@ -654,7 +653,7 @@ public function addBackupPolicy(Policy $policy): Result /** * Lists source operations. * - * @return []SourceOperation + * @return SourceOperation[] */ public function getSourceOperations(): array { diff --git a/src/Model/Git/Blob.php b/src/Model/Git/Blob.php index 4b41d4c..d79ed69 100644 --- a/src/Model/Git/Blob.php +++ b/src/Model/Git/Blob.php @@ -12,7 +12,7 @@ * Git blob resource. * * @property-read string $sha - * @property-read string $size + * @property-read int|float $size * @property-read string $encoding * @property-read string $content */ diff --git a/src/Model/Plan.php b/src/Model/Plan.php index 6695517..eaa7ada 100644 --- a/src/Model/Plan.php +++ b/src/Model/Plan.php @@ -24,7 +24,7 @@ public function __get(string $name) return parent::__get($name); } - public function update(array $values) + public function update(array $values): Result { throw new \BadMethodCallException('Update is not available for plans'); } diff --git a/src/Model/Project.php b/src/Model/Project.php index 5484e21..471766d 100644 --- a/src/Model/Project.php +++ b/src/Model/Project.php @@ -51,8 +51,7 @@ public function __construct(array $data, $baseUrl = null, ClientInterface $clien * * @internal */ - #[\ReturnTypeWillChange] - public function delete() + public function delete(): Result { throw new \BadMethodCallException('Projects should not be deleted directly. Delete the subscription instead.'); } diff --git a/src/Model/Project/Settings.php b/src/Model/Project/Settings.php index dc45a0e..644d780 100644 --- a/src/Model/Project/Settings.php +++ b/src/Model/Project/Settings.php @@ -5,6 +5,7 @@ namespace Platformsh\Client\Model\Project; use Platformsh\Client\Model\ApiResourceBase; +use Platformsh\Client\Model\Result; /** * Settings represent various flags on a project. @@ -17,8 +18,7 @@ */ class Settings extends ApiResourceBase { - #[\ReturnTypeWillChange] - public function delete() + public function delete(): Result { throw new \BadMethodCallException('Settings cannot be deleted'); } diff --git a/src/Model/Result.php b/src/Model/Result.php index 78d1d25..619f00b 100644 --- a/src/Model/Result.php +++ b/src/Model/Result.php @@ -84,13 +84,12 @@ public function getEntity(): ApiResourceBase return new $resourceClass($data, $this->baseUrl, $this->client); } - public function update(array $values) + public function update(array $values): self { throw new \BadMethodCallException('Cannot update() a Result instance directly. Perhaps use getEntity().'); } - #[\ReturnTypeWillChange] - public function delete() + public function delete(): self { throw new \BadMethodCallException('Cannot delete() a Result instance directly. Perhaps use getEntity().'); } diff --git a/src/Model/SetupOptions.php b/src/Model/SetupOptions.php index eca5522..cfeacd4 100644 --- a/src/Model/SetupOptions.php +++ b/src/Model/SetupOptions.php @@ -32,7 +32,7 @@ public static function create(array $body, string $url, ClientInterface $client) { $request = new Request('post', $url, [ 'Content-Type' => 'application/json', - ], \GuzzleHttp\json_encode($body)); + ], Utils::jsonEncode($body)); $response = $client->send($request); $data = Utils::jsonDecode((string) $response->getBody(), true); return new self($data); diff --git a/src/Model/SshKey.php b/src/Model/SshKey.php index 70250e7..f59124d 100644 --- a/src/Model/SshKey.php +++ b/src/Model/SshKey.php @@ -44,7 +44,7 @@ public static function validatePublicKey(string $value): bool /** * @throws \BadMethodCallException */ - public function update(array $values) + public function update(array $values): Result { throw new \BadMethodCallException('Update is not implemented for SSH keys'); } diff --git a/src/Model/Subscription.php b/src/Model/Subscription.php index 8c12346..dc2ebf8 100644 --- a/src/Model/Subscription.php +++ b/src/Model/Subscription.php @@ -75,7 +75,7 @@ public static function create(array $body, string $collectionUrl, ClientInterfac /** * Wait for the subscription's project to be provisioned. * - * @param callable $onPoll A function that will be called every time the + * @param callable|null $onPoll A function that will be called every time the * subscription is refreshed. It will be passed * one argument: the Subscription object. * @param int $interval The polling interval, in seconds. @@ -83,7 +83,7 @@ public static function create(array $body, string $collectionUrl, ClientInterfac public function wait(callable $onPoll = null, int $interval = 2): void { while ($this->isPending()) { - sleep($interval > 1 ? $interval : 1); + sleep(max($interval, 1)); $this->refresh(); if ($onPoll !== null) { $onPoll($this); @@ -199,7 +199,7 @@ protected static function checkProperty(string $property, mixed $value): array protected function setData(array $data): void { - $data = isset($data['subscriptions'][0]) ? $data['subscriptions'][0] : $data; + $data = $data['subscriptions'][0] ?? $data; $this->data = $data; } } diff --git a/src/PlatformClient.php b/src/PlatformClient.php index c216832..bbe9cbd 100644 --- a/src/PlatformClient.php +++ b/src/PlatformClient.php @@ -50,10 +50,7 @@ public function __construct(ConnectorInterface $connector = null) $this->connector = $connector ?: new Connector(); } - /** - * @return ConnectorInterface - */ - public function getConnector(): ConnectorInterface|Connector + public function getConnector(): ConnectorInterface { return $this->connector; } @@ -262,12 +259,12 @@ public function addSshKey(string $value, string $title = null): Result * @param string|SubscriptionOptions $options * Subscription request options, which override the other arguments. * If a string is passed, it will be used as the region ID (deprecated). See getRegions(). - * @param string|null $plan The plan. See getPlans(). @deprecated - * @param string|null $title The project title. @deprecated - * @param int|null $storage The storage of each environment, in MiB. @deprecated - * @param int|null $environments The number of available environments. @deprecated - * @param array $activation_callback An activation callback for the subscription. @deprecated - * @param string|null $options_url The catalog options URL. See getCatalog(). @deprecated + * @param string|null $plan The plan. See getPlans(). @deprecated + * @param string|null $title The project title. @deprecated + * @param int|null $storage The storage of each environment, in MiB. @deprecated + * @param int|null $environments The number of available environments. @deprecated + * @param array|null $activation_callback An activation callback for the subscription. @deprecated + * @param string|null $options_url The catalog options URL. See getCatalog(). @deprecated * * @return Subscription * A subscription, representing a project. Use Subscription::wait() or @@ -277,14 +274,12 @@ public function addSshKey(string $value, string $title = null): Result * @see PlatformClient::getCatalog() * @see PlatformClient::getRegions() * @see Subscription::wait() - * - * @noinspection PhpTooManyParametersInspection */ public function createSubscription(SubscriptionOptions|string $options, string $plan = null, string $title = null, int $storage = null, int $environments = null, array $activation_callback = null, string $options_url = null): Subscription { if ($options instanceof SubscriptionOptions) { $values = $options->toArray(); - } elseif (\is_string($options)) { + } else { \trigger_error('The previous arguments list has been replaced by a single SubscriptionOptions argument', E_USER_DEPRECATED); if ($plan === null) { // Backwards-compatible default. @@ -299,8 +294,6 @@ public function createSubscription(SubscriptionOptions|string $options, string $ 'activation_callback' => $activation_callback, 'options_url' => $options_url, ]); - } else { - throw new \InvalidArgumentException('The first argument must be a SubscriptionOptions object or a string'); } if ($id = $options->organizationId()) {