Skip to content

Commit

Permalink
Type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Nov 25, 2024
1 parent baee809 commit 6d5e117
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 51 deletions.
6 changes: 2 additions & 4 deletions src/Connection/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Connector implements ConnectorInterface

protected ?ClientInterface $client = null;

protected $oauthMiddleware;
protected ?GuzzleMiddleware $oauthMiddleware = null;

protected ?Platformsh $provider = null;

Expand Down Expand Up @@ -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()) {
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/ApiResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 5 additions & 10 deletions src/Model/ApiResourceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand All @@ -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);
Expand Down Expand Up @@ -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');

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Catalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
3 changes: 1 addition & 2 deletions src/Model/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ public static function validateId(string $id): bool
*
* @throws EnvironmentStateException
*/
#[\ReturnTypeWillChange]
public function delete(): Result
{
if ($this->isActive()) {
Expand Down Expand Up @@ -654,7 +653,7 @@ public function addBackupPolicy(Policy $policy): Result
/**
* Lists source operations.
*
* @return []SourceOperation
* @return SourceOperation[]
*/
public function getSourceOperations(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Git/Blob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
3 changes: 1 addition & 2 deletions src/Model/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Project/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -17,8 +18,7 @@
*/
class Settings extends ApiResourceBase
{
#[\ReturnTypeWillChange]
public function delete()
public function delete(): Result
{
throw new \BadMethodCallException('Settings cannot be deleted');
}
Expand Down
5 changes: 2 additions & 3 deletions src/Model/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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().');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/SetupOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Model/SshKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
6 changes: 3 additions & 3 deletions src/Model/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ 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.
*/
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);
Expand Down Expand Up @@ -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;
}
}
23 changes: 8 additions & 15 deletions src/PlatformClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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()) {
Expand Down

0 comments on commit 6d5e117

Please sign in to comment.