Skip to content

Commit e1e88af

Browse files
committed
Add type hints, and other type fixes
1 parent c705f5f commit e1e88af

File tree

77 files changed

+612
-1430
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+612
-1430
lines changed

src/Connection/Connector.php

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,15 @@
2424

2525
class Connector implements ConnectorInterface
2626
{
27-
/**
28-
* @var array
29-
*/
30-
protected $config = [];
27+
protected array $config = [];
3128

32-
/**
33-
* @var ClientInterface|null
34-
*/
35-
protected $client;
29+
protected ?ClientInterface $client;
3630

37-
/**
38-
* @var callable|null
39-
*/
4031
protected $oauthMiddleware;
4132

42-
/**
43-
* @var AbstractProvider|null
44-
*/
45-
protected $provider;
33+
protected ?AbstractProvider $provider;
4634

47-
/**
48-
* @var SessionInterface
49-
*/
50-
protected $session;
35+
protected SessionInterface $session;
5136

5237
/**
5338
* @var array
@@ -57,7 +42,7 @@ class Connector implements ConnectorInterface
5742
* the key in the AccessToken constructor. The right-hand side is the key
5843
* that will be stored.
5944
*/
60-
private $storageKeys = [
45+
private array $storageKeys = [
6146
'access_token' => 'accessToken',
6247
'refresh_token' => 'refreshToken',
6348
'token_type' => 'tokenType',
@@ -170,10 +155,7 @@ public function __construct(array $config = [], SessionInterface $session = null
170155
}
171156
}
172157

173-
/**
174-
* @return array
175-
*/
176-
public function getConfig()
158+
public function getConfig(): array
177159
{
178160
return $this->config;
179161
}
@@ -182,28 +164,24 @@ public function getConfig()
182164
* Get the configured accounts endpoint URL.
183165
*
184166
* @deprecated Use Connector::getApiUrl() instead
185-
*
186-
* @return string
187167
*/
188-
public function getAccountsEndpoint()
168+
public function getAccountsEndpoint(): string
189169
{
190170
return $this->config['accounts'];
191171
}
192172

193173
/**
194174
* Get the configured API gateway URL (without trailing slash).
195-
*
196-
* @return string
197175
*/
198-
public function getApiUrl()
176+
public function getApiUrl(): string
199177
{
200178
return rtrim($this->config['api_url'], '/');
201179
}
202180

203181
/**
204182
* @throws \GuzzleHttp\Exception\GuzzleException if tokens cannot be revoked.
205183
*/
206-
public function logOut()
184+
public function logOut(): void
207185
{
208186
$this->client = null;
209187
try {
@@ -222,17 +200,15 @@ public function logOut()
222200
}
223201
}
224202

225-
public function getSession()
203+
public function getSession(): Session|SessionInterface
226204
{
227205
return $this->session;
228206
}
229207

230208
/**
231209
* Returns the access token saved in the session, if any.
232-
*
233-
* @return false|string
234210
*/
235-
public function getAccessToken()
211+
public function getAccessToken(): false|string
236212
{
237213
return $this->session->get('accessToken');
238214
}
@@ -241,7 +217,7 @@ public function getAccessToken()
241217
* @throws \GuzzleHttp\Exception\GuzzleException
242218
* @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException
243219
*/
244-
public function logIn($username, $password, $force = false, $totp = null)
220+
public function logIn(string $username, string $password, bool $force = false, int|string $totp = null): void
245221
{
246222
if (! $force && $this->isLoggedIn() && $this->session->get('username') === $username) {
247223
return;
@@ -261,7 +237,7 @@ public function logIn($username, $password, $force = false, $totp = null)
261237
/**
262238
* Save an access token to the session.
263239
*/
264-
public function saveToken(AccessTokenInterface $token)
240+
public function saveToken(AccessTokenInterface $token): void
265241
{
266242
if ($this->config['api_token'] && $this->config['api_token_type'] === 'access') {
267243
return;
@@ -274,12 +250,12 @@ public function saveToken(AccessTokenInterface $token)
274250
$this->session->save();
275251
}
276252

277-
public function isLoggedIn()
253+
public function isLoggedIn(): bool
278254
{
279255
return $this->session->get($this->storageKeys['access_token']) || $this->config['api_token'];
280256
}
281257

282-
public function setApiToken($token, $type)
258+
public function setApiToken(string $token, string $type): void
283259
{
284260
$this->config['api_token'] = $token;
285261
if (! in_array($type, ['access', 'exchange'], true)) {
@@ -337,10 +313,8 @@ public function getClient(): ClientInterface
337313

338314
/**
339315
* Load the current access token.
340-
*
341-
* @return AccessToken|null
342316
*/
343-
protected function loadToken()
317+
protected function loadToken(): ?AccessToken
344318
{
345319
if ($this->config['api_token'] && $this->config['api_token_type'] === 'access') {
346320
return new AccessToken([
@@ -373,7 +347,7 @@ protected function loadToken()
373347
*
374348
* @return GuzzleMiddleware
375349
*/
376-
protected function getOauthMiddleware()
350+
protected function getOauthMiddleware(): GuzzleMiddleware|callable|null
377351
{
378352
if (! $this->oauthMiddleware) {
379353
if (! $this->isLoggedIn()) {
@@ -417,10 +391,7 @@ protected function getOauthMiddleware()
417391
return $this->oauthMiddleware;
418392
}
419393

420-
/**
421-
* @return string
422-
*/
423-
private function defaultUserAgent()
394+
private function defaultUserAgent(): string
424395
{
425396
$version = trim(file_get_contents(__DIR__ . '/../../version.txt')) ?: '2.0.x';
426397

@@ -438,10 +409,8 @@ private function defaultUserAgent()
438409
* Get a configured OAuth 2.0 URL.
439410
*
440411
* @param string $key Either 'token_url' or 'revoke_url'
441-
*
442-
* @return string
443412
*/
444-
private function getOAuthUrl($key)
413+
private function getOAuthUrl(string $key): string
445414
{
446415
$url = $this->config[$key];
447416

@@ -462,7 +431,7 @@ private function getOAuthUrl($key)
462431
*
463432
* @throws \GuzzleHttp\Exception\GuzzleException
464433
*/
465-
private function revokeTokens()
434+
private function revokeTokens(): void
466435
{
467436
$revocations = array_filter([
468437
'refresh_token' => $this->session->get('refreshToken'),
@@ -483,7 +452,7 @@ private function revokeTokens()
483452
}
484453
}
485454

486-
private function getProvider()
455+
private function getProvider(): AbstractProvider|Platformsh
487456
{
488457
return $this->provider ?: new Platformsh([
489458
'clientId' => $this->config['client_id'],

src/Connection/ConnectorInterface.php

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,19 @@ interface ConnectorInterface
1111
{
1212
/**
1313
* Get the session instance for this connection.
14-
*
15-
* @return SessionInterface
1614
*/
17-
public function getSession();
15+
public function getSession(): SessionInterface;
1816

1917
/**
2018
* Log in to Platform.sh.
2119
*
22-
* @param string $username
23-
* @param string $password
24-
* @param bool $force
20+
* @param bool $force
2521
* Whether to re-authenticate even if the session appears to be logged
2622
* in already.
27-
* @param string|int $totp
23+
* @param int|string|null $totp
2824
* Time-based one-time password (two-factor authentication).
2925
*/
30-
public function logIn($username, $password, $force = false, $totp = null);
26+
public function logIn(string $username, string $password, bool $force = false, int|string $totp = null);
3127

3228
/**
3329
* Log out.
@@ -36,19 +32,15 @@ public function logOut();
3632

3733
/**
3834
* Check whether the user is logged in.
39-
*
40-
* @return bool
4135
*/
42-
public function isLoggedIn();
36+
public function isLoggedIn(): bool;
4337

4438
/**
4539
* Get an authenticated Guzzle client.
4640
*
4741
* This will fail if the user is not logged in.
48-
*
49-
* @return ClientInterface
5042
*/
51-
public function getClient();
43+
public function getClient(): ClientInterface;
5244

5345
/**
5446
* Set the API token to use for Platform.sh requests.
@@ -59,12 +51,10 @@ public function getClient();
5951
* The token type: 'exchange' for an API token (recommended), or 'access'
6052
* for an OAuth 2.0 access token.
6153
*/
62-
public function setApiToken($token, $type);
54+
public function setApiToken(string $token, string $type);
6355

6456
/**
6557
* Get the configured API gateway URL (without trailing slash).
66-
*
67-
* @return string
6858
*/
69-
public function getApiUrl();
59+
public function getApiUrl(): string;
7060
}

src/DataStructure/ReadOnlyStructureTrait.php

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
trait ReadOnlyStructureTrait
1414
{
15-
private $data = [];
15+
private array $data = [];
1616

1717
/**
1818
* Private constructor. Instantiate this object using self::fromData().
@@ -25,11 +25,9 @@ private function __construct(array $data)
2525
/**
2626
* Magic getter.
2727
*
28-
* @param string $name
29-
*
3028
* @return mixed
3129
*/
32-
public function __get($name)
30+
public function __get(string $name)
3331
{
3432
$this->checkExists($name);
3533

@@ -39,61 +37,49 @@ public function __get($name)
3937
/**
4038
* Magic isset() support.
4139
*
42-
* @param string $name
43-
*
4440
* @return bool
4541
*/
46-
public function __isset($name)
42+
public function __isset(string $name)
4743
{
4844
return isset($this->data[$name]);
4945
}
5046

5147
/**
5248
* Magic setter.
5349
*
54-
* @param string $name
55-
* @param mixed $value
56-
*
5750
* @throws \InvalidArgumentException if the property is not found
5851
* @throws \BadMethodCallException if the property is found
5952
*/
60-
public function __set($name, $value)
53+
public function __set(string $name, mixed $value)
6154
{
6255
$this->checkExists($name);
6356
throw new \BadMethodCallException('Property not writable: ' . $name);
6457
}
6558

6659
/**
6760
* Construct from API data.
68-
*
69-
* @return static
7061
*/
71-
public static function fromData(array $data)
62+
public static function fromData(array $data): static
7263
{
7364
return new static($data);
7465
}
7566

7667
/**
7768
* Get all properties.
78-
*
79-
* @return array
8069
*/
81-
public function getProperties()
70+
public function getProperties(): array
8271
{
8372
return $this->data;
8473
}
8574

8675
/**
8776
* Gets a single property.
8877
*
89-
* @param bool $required
90-
*
91-
* @throws \InvalidArgumentException if $required is true and the property is not set
92-
*
9378
* @return mixed|null
9479
* Returns the property value, or null if $required is false and the property is not set.
80+
*@throws \InvalidArgumentException if $required is true and the property is not set
9581
*/
96-
public function getProperty($property, $required = true)
82+
public function getProperty($property, bool $required = true): mixed
9783
{
9884
if (! array_key_exists($property, $this->data)) {
9985
if ($required) {
@@ -108,11 +94,9 @@ public function getProperty($property, $required = true)
10894
/**
10995
* Check if a property exists.
11096
*
111-
* @param string $property
112-
*
11397
* @throws \InvalidArgumentException if the property is not found
11498
*/
115-
private function checkExists($property)
99+
private function checkExists(string $property): void
116100
{
117101
if (! array_key_exists($property, $this->data)) {
118102
throw new \InvalidArgumentException('Property not found: ' . $property);

0 commit comments

Comments
 (0)