Skip to content

Commit 84034cc

Browse files
committed
types cleanup
- per-component phpstan - upgrade spanner to phpstan level 3 - improved typing - address review comments
1 parent e74b497 commit 84034cc

61 files changed

Lines changed: 532 additions & 750 deletions

Some content is hidden

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

.github/workflows/emulator-system-tests-spanner.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
- name: Install dependencies
5050
run: |
5151
# ensure composer uses local Core instead of pulling from packagist
52-
# composer config repositories.local --json '{"type":"path", "url": "../Core", "options": {"versions": {"google/cloud-core": "1.100"}}}' -d Spanner
52+
composer config repositories.local --json '{"type":"path", "url": "../Core", "options": {"versions": {"google/cloud-core": "1.100"}},"canonical":false}' -d Spanner
5353
composer update --prefer-dist --no-interaction --no-suggest -d Spanner/
5454
5555
- name: Run system tests

Core/src/LongRunning/LongRunningOperation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/**
2121
* Represent and interact with a Long Running Operation.
22+
* @template T
2223
*/
2324
class LongRunningOperation
2425
{
@@ -180,7 +181,7 @@ public function state(array $options = [])
180181
* ```
181182
*
182183
* @param array $options [optional] Configuration options.
183-
* @return mixed|null
184+
* @return T|mixed|null
184185
*/
185186
public function result(array $options = [])
186187
{

Core/src/RequestHandler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ class RequestHandler
4646

4747
/**
4848
* @param Serializer $serializer
49-
* @param array<string|object> $clientClasses
49+
* @param array<string|object> $clients
5050
* @param array $clientConfig
5151
*/
5252
public function __construct(
5353
Serializer $serializer,
54-
array $clientClasses,
54+
array $clients,
5555
array $clientConfig = []
5656
) {
5757
//@codeCoverageIgnoreStart
@@ -75,7 +75,7 @@ public function __construct(
7575
//@codeCoverageIgnoreEnd
7676

7777
// Initialize the client classes and store them in memory
78-
foreach ($clientClasses as $client) {
78+
foreach ($clients as $client) {
7979
if (is_object($client)) {
8080
$this->clients[get_class($client)] = $client;
8181
} else {

Core/src/Timestamp.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace Google\Cloud\Core;
1919

20+
use DateTimeInterface;
21+
2022
/**
2123
* Represents a Timestamp value.
2224
*
@@ -85,9 +87,9 @@ public function __construct(\DateTimeInterface $value, $nanoSeconds = null)
8587
* $dateTime = $timestamp->get();
8688
* ```
8789
*
88-
* @return \DateTimeInterface
90+
* @return DateTimeInterface
8991
*/
90-
public function get()
92+
public function get(): DateTimeInterface
9193
{
9294
return $this->value;
9395
}
@@ -102,7 +104,7 @@ public function get()
102104
*
103105
* @return int
104106
*/
105-
public function nanoSeconds()
107+
public function nanoSeconds(): int
106108
{
107109
return $this->nanoSeconds === null
108110
? (int) $this->value->format('u') * 1000
@@ -119,7 +121,7 @@ public function nanoSeconds()
119121
*
120122
* @return string
121123
*/
122-
public function formatAsString()
124+
public function formatAsString(): string
123125
{
124126
return $this->formatTimeAsString(
125127
$this->value,
@@ -143,7 +145,7 @@ public function __toString()
143145
*
144146
* @return array
145147
*/
146-
public function formatForApi()
148+
public function formatForApi(): array
147149
{
148150
return $this->formatTimeAsArray($this->value, $this->nanoSeconds());
149151
}
@@ -155,7 +157,7 @@ public function formatForApi()
155157
* @access private
156158
*/
157159
#[\ReturnTypeWillChange]
158-
public function jsonSerialize()
160+
public function jsonSerialize(): string
159161
{
160162
return $this->formatAsString();
161163
}

Core/tests/Unit/ServiceBuilderTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use Google\Cloud\Firestore\FirestoreClient;
2525
use Google\Cloud\Language\LanguageClient;
2626
use Google\Cloud\Logging\LoggingClient;
27-
use Google\Cloud\Speech\SpeechClient;
2827
use Google\Cloud\Storage\StorageClient;
2928
use Google\Cloud\Core\Tests\Unit\Fixtures;
3029
use GuzzleHttp\Psr7\Response;
@@ -161,10 +160,6 @@ public function serviceProvider()
161160
], [
162161
'language',
163162
LanguageClient::class
164-
], [
165-
'speech',
166-
SpeechClient::class,
167-
['languageCode' => 'en-US']
168163
], [
169164
'storage',
170165
StorageClient::class

Spanner/phpstan.neon.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
parameters:
2+
level: 3
3+
treatPhpDocTypesAsCertain: false
4+
excludePaths:
5+
# Ignore classes not compatible with Monolog2
6+
- src/V1/*
7+
- src/*/*/V1/*
8+
ignoreErrors:
9+
-
10+
identifier: new.static # Ignore this error because changing it would be a breaking change.
11+
includes:
12+
- ../dev/vendor/phpstan/phpstan/conf/bleedingEdge.neon
13+

Spanner/src/ArrayType.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,11 @@
6161
*/
6262
class ArrayType
6363
{
64-
/**
65-
* @var int|null
66-
*/
67-
private $type;
68-
69-
/**
70-
* @var StructType|null
71-
*/
72-
private $structType;
64+
private int|string|null $type;
65+
private StructType|null $structType;
7366

7467
/**
75-
* @param int|string|null|StructType $type A value type code or nested struct
68+
* @param int|string|StructType|null $type A value type code or nested struct
7669
* definition. Accepted integer and string values are defined as constants on
7770
* {@see \Google\Cloud\Spanner\Database}, and are as follows:
7871
* `Database::TYPE_BOOL`, `Database::TYPE_INT64`,
@@ -87,7 +80,7 @@ class ArrayType
8780
* a struct is defined but the given type is not
8881
* `Database::TYPE_STRUCT`.
8982
*/
90-
public function __construct($type)
83+
public function __construct(int|string|StructType|null $type)
9184
{
9285
if ($type === Database::TYPE_STRUCT) {
9386
throw new \InvalidArgumentException(

Spanner/src/Backup.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class Backup
5858
*
5959
* @internal Backup is constructed by the {@see Instance} class.
6060
*
61-
* @param DatabaseAdminClient The database admin client to make backup RPC calls.
61+
* @param DatabaseAdminClient $databaseAdminClient The database admin client to make backup RPC
62+
* calls.
6263
* @param Serializer $serializer The serializer instance to encode/decode messages.
6364
* @param Instance $instance The instance in which the backup exists.
6465
* @param string $projectId The project ID.
@@ -100,7 +101,7 @@ public function __construct(
100101
* @throws \InvalidArgumentException
101102
*/
102103
public function create(
103-
$database,
104+
string $database,
104105
DateTimeInterface $expireTime,
105106
array $options = []
106107
): LongRunningOperation {
@@ -362,7 +363,7 @@ public function updateExpireTime(DateTimeInterface $newTimestamp, array $options
362363
* @param string $operationName The Long Running Operation name.
363364
* @return LongRunningOperation
364365
*/
365-
public function resumeOperation($operationName, array $options = []): LongRunningOperation
366+
public function resumeOperation(string $operationName, array $options = []): LongRunningOperation
366367
{
367368
return new LongRunningOperation(
368369
new LongRunningClientConnection($this->databaseAdminClient, $this->serializer),
@@ -423,7 +424,7 @@ function (OperationProto $operation) {
423424
*
424425
* @return string
425426
*/
426-
private function fullyQualifiedBackupName($name): string
427+
private function fullyQualifiedBackupName(string $name): string
427428
{
428429
$instance = DatabaseAdminClient::parseName($this->instance->name())['instance'];
429430

Spanner/src/Batch/BatchClient.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -108,30 +108,15 @@ class BatchClient
108108
use TransactionConfigurationTrait;
109109

110110
const PARTITION_TYPE_KEY = '__partitionTypeName';
111-
112-
/**
113-
* @var Operation
114-
*/
115-
private $operation;
116-
117-
/**
118-
* @var string
119-
*/
120-
private $databaseName;
121-
122-
/**
123-
* @var string|null
124-
*/
125-
private $databaseRole;
126-
127-
/**
128-
* @var array
129-
*/
130-
private $allowedPartitionTypes = [
111+
private const ALLOWED_PARTITION_TYPES = [
131112
QueryPartition::class,
132113
ReadPartition::class
133114
];
134115

116+
private Operation $operation;
117+
private string $databaseName;
118+
private string|null $databaseRole;
119+
135120
/**
136121
* @param Operation $operation A Cloud Spanner Operations wrapper.
137122
* @param string $databaseName The database name to which the batch client
@@ -198,6 +183,7 @@ public function snapshot(array $options = [])
198183
$sessionOptions
199184
);
200185

186+
/** @var BatchSnapshot */
201187
return $this->operation->snapshot($session, [
202188
'className' => BatchSnapshot::class,
203189
'transactionOptions' => $transactionOptions
@@ -233,7 +219,7 @@ public function snapshotFromString($identifier)
233219

234220
$session = $this->operation->session($data['sessionName']);
235221

236-
$readTime = $this->parseTimeString($data['readTimestamp']);
222+
/** @var BatchSnapshot */
237223
return $this->operation->createSnapshot($session, [
238224
'id' => $data['transactionId'],
239225
'readTimestamp' => $data['readTimestamp']
@@ -262,7 +248,7 @@ public function partitionFromString($partition)
262248
}
263249

264250
$class = $data[self::PARTITION_TYPE_KEY];
265-
if (!in_array($class, $this->allowedPartitionTypes)) {
251+
if (!in_array($class, self::ALLOWED_PARTITION_TYPES)) {
266252
throw new \InvalidArgumentException('Invalid partition type.');
267253
}
268254

Spanner/src/Batch/BatchSnapshot.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function __construct(Operation $operation, Session $session, array $optio
9292
* @param array $options [optional] Configuration Options
9393
* @return void
9494
*/
95-
public function close(array $options = [])
95+
public function close(array $options = []): void
9696
{
9797
$this->session->delete($options);
9898
}
@@ -131,7 +131,7 @@ public function close(array $options = [])
131131
* }
132132
* @return ReadPartition[]
133133
*/
134-
public function partitionRead($table, KeySet $keySet, array $columns, array $options = [])
134+
public function partitionRead($table, KeySet $keySet, array $columns, array $options = []): array
135135
{
136136
return $this->operation->partitionRead(
137137
$this->session,
@@ -194,7 +194,7 @@ public function partitionRead($table, KeySet $keySet, array $columns, array $opt
194194
* }
195195
* @return QueryPartition[]
196196
*/
197-
public function partitionQuery($sql, array $options = [])
197+
public function partitionQuery($sql, array $options = []): array
198198
{
199199
return $this->operation->partitionQuery(
200200
$this->session,
@@ -218,11 +218,10 @@ public function partitionQuery($sql, array $options = [])
218218
* ```
219219
*
220220
* @param PartitionInterface $partition The partition to read.
221-
* @param array $options Configuration Options.
222221
* @return Result
223222
* @throws \BadMethodCallException If an invalid partition type is given.
224223
*/
225-
public function executePartition(PartitionInterface $partition, array $options = [])
224+
public function executePartition(PartitionInterface $partition): Result
226225
{
227226
if ($partition instanceof QueryPartition) {
228227
return $this->executeQuery($partition);
@@ -243,7 +242,7 @@ public function executePartition(PartitionInterface $partition, array $options =
243242
*
244243
* @return string
245244
*/
246-
public function serialize()
245+
public function serialize(): string
247246
{
248247
return base64_encode(json_encode([
249248
'sessionName' => $this->session->name(),
@@ -269,7 +268,7 @@ public function __toString()
269268
* @param QueryPartition $partition The partition.
270269
* @return Result
271270
*/
272-
private function executeQuery(QueryPartition $partition)
271+
private function executeQuery(QueryPartition $partition): Result
273272
{
274273
return $this->execute($partition->sql(), [
275274
'partitionToken' => $partition->token()
@@ -282,7 +281,7 @@ private function executeQuery(QueryPartition $partition)
282281
* @param ReadPartition $partition The partition.
283282
* @return Result
284283
*/
285-
private function executeRead(ReadPartition $partition)
284+
private function executeRead(ReadPartition $partition): Result
286285
{
287286
return $this->read($partition->table(), $partition->keySet(), $partition->columns(), [
288287
'partitionToken' => $partition->token()

0 commit comments

Comments
 (0)