Skip to content

Commit 4d40b42

Browse files
authored
chore(Datastore): add types to methods, promote properties to constructor (#8650)
1 parent 6952d41 commit 4d40b42

33 files changed

Lines changed: 399 additions & 482 deletions

Datastore/phpstan.neon.dist

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

Datastore/src/Cursor.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,19 @@
3939
*/
4040
class Cursor
4141
{
42-
/**
43-
* @var string|int
44-
*/
45-
private $cursor;
46-
4742
/**
4843
* @param string|int $cursor The cursor value.
4944
*/
50-
public function __construct($cursor)
51-
{
52-
$this->cursor = $cursor;
45+
public function __construct(
46+
private string|int $cursor
47+
) {
5348
}
5449

5550
/**
5651
* @access private
5752
* @return string|int
5853
*/
59-
public function cursor()
54+
public function cursor(): string|int
6055
{
6156
return $this->cursor;
6257
}

Datastore/src/DatastoreClient.php

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ class DatastoreClient
104104
/**
105105
* @var Operation
106106
*/
107-
protected $operation;
107+
protected Operation $operation;
108108

109109
/**
110110
* @var EntityMapper
111111
*/
112-
private $entityMapper;
112+
private EntityMapper $entityMapper;
113113

114114
/**
115115
* @var GapicDatastoreClient
@@ -234,7 +234,7 @@ public function __construct(array $config = [])
234234
);
235235
}
236236

237-
$this->projectId = $this->detectProjectId($gapicOptions);
237+
$this->projectId = (string) $this->detectProjectId($gapicOptions);
238238
$this->gapicClient = $this->getGapicClient($gapicOptions);
239239

240240
// The second parameter here should change to a variable
@@ -286,7 +286,7 @@ public function __construct(array $config = [])
286286
* }
287287
* @return Key
288288
*/
289-
public function key($kind, $identifier = null, array $options = [])
289+
public function key(string $kind, $identifier = null, array $options = []): Key
290290
{
291291
return $this->operation->key($kind, $identifier, $options);
292292
}
@@ -334,7 +334,7 @@ public function key($kind, $identifier = null, array $options = [])
334334
* }
335335
* @return Key[]
336336
*/
337-
public function keys($kind, array $options = [])
337+
public function keys(string $kind, array $options = []): array
338338
{
339339
return $this->operation->keys($kind, $options);
340340
}
@@ -457,7 +457,7 @@ public function keys($kind, array $options = [])
457457
* }
458458
* @return EntityInterface
459459
*/
460-
public function entity($key = null, array $entity = [], array $options = [])
460+
public function entity(Key|string|null $key = null, array $entity = [], array $options = []): EntityInterface
461461
{
462462
return $this->operation->entity($key, $entity, $options);
463463
}
@@ -478,7 +478,7 @@ public function entity($key = null, array $entity = [], array $options = [])
478478
* **Defaults to** `false`.
479479
* @return GeoPoint
480480
*/
481-
public function geoPoint($latitude, $longitude, $allowNull = false)
481+
public function geoPoint(float $latitude, float $longitude, bool $allowNull = false): GeoPoint
482482
{
483483
return new GeoPoint($latitude, $longitude, $allowNull);
484484
}
@@ -499,7 +499,7 @@ public function geoPoint($latitude, $longitude, $allowNull = false)
499499
* @param string|resource|StreamInterface $value The value to store in a blob.
500500
* @return Blob
501501
*/
502-
public function blob($value)
502+
public function blob(mixed $value): Blob
503503
{
504504
return new Blob($value);
505505
}
@@ -516,7 +516,7 @@ public function blob($value)
516516
* @param string $value
517517
* @return Int64
518518
*/
519-
public function int64($value)
519+
public function int64(string $value): Int64
520520
{
521521
return new Int64($value);
522522
}
@@ -535,7 +535,7 @@ public function int64($value)
535535
* @param string|int $cursorValue
536536
* @return Cursor
537537
*/
538-
public function cursor($cursorValue)
538+
public function cursor(string|int $cursorValue): Cursor
539539
{
540540
return new Cursor($cursorValue);
541541
}
@@ -560,7 +560,7 @@ public function cursor($cursorValue)
560560
* @param array $options [optional] Configuration options.
561561
* @return Key
562562
*/
563-
public function allocateId(Key $key, array $options = [])
563+
public function allocateId(Key $key, array $options = []): Key
564564
{
565565
$res = $this->allocateIds([$key], $options);
566566
return $res[0];
@@ -594,7 +594,7 @@ public function allocateId(Key $key, array $options = [])
594594
* }.
595595
* @return Key[]
596596
*/
597-
public function allocateIds(array $keys, array $options = [])
597+
public function allocateIds(array $keys, array $options = []): array
598598
{
599599
return $this->operation->allocateIds($keys, $options);
600600
}
@@ -620,7 +620,7 @@ public function allocateIds(array $keys, array $options = [])
620620
* @return Transaction
621621
* @codingStandardsIgnoreEnd
622622
*/
623-
public function transaction(array $options = [])
623+
public function transaction(array $options = []): Transaction
624624
{
625625
$transaction = $this->operation->beginTransaction([
626626
// if empty, force request to encode as {} rather than [].
@@ -659,7 +659,7 @@ public function transaction(array $options = [])
659659
* @return ReadOnlyTransaction
660660
* @codingStandardsIgnoreEnd
661661
*/
662-
public function readOnlyTransaction(array $options = [])
662+
public function readOnlyTransaction(array $options = []): ReadOnlyTransaction
663663
{
664664
$transaction = $this->operation->beginTransaction([
665665
// if empty, force request to encode as {} rather than [].
@@ -696,7 +696,7 @@ public function readOnlyTransaction(array $options = [])
696696
* @return string The entity version.
697697
* @throws DomainException If a conflict occurs, fail.
698698
*/
699-
public function insert(EntityInterface $entity, array $options = [])
699+
public function insert(EntityInterface $entity, array $options = []): string
700700
{
701701
$res = $this->insertBatch([$entity], $options);
702702
return $this->parseSingleMutationResult($res);
@@ -727,7 +727,7 @@ public function insert(EntityInterface $entity, array $options = [])
727727
* @param array $options [optional] Configuration options.
728728
* @return array [Response Body](https://cloud.google.com/datastore/reference/rest/v1/projects/commit#response-body)
729729
*/
730-
public function insertBatch(array $entities, array $options = [])
730+
public function insertBatch(array $entities, array $options = []): array
731731
{
732732
$entities = $this->operation->allocateIdsToEntities($entities);
733733
$mutations = [];
@@ -773,7 +773,7 @@ public function insertBatch(array $entities, array $options = [])
773773
* @return string The entity version.
774774
* @throws DomainException If a conflict occurs, fail.
775775
*/
776-
public function update(EntityInterface $entity, array $options = [])
776+
public function update(EntityInterface $entity, array $options = []): string
777777
{
778778
$res = $this->updateBatch([$entity], $options);
779779
return $this->parseSingleMutationResult($res);
@@ -814,7 +814,7 @@ public function update(EntityInterface $entity, array $options = [])
814814
* }
815815
* @return array [Response Body](https://cloud.google.com/datastore/reference/rest/v1/projects/commit#response-body)
816816
*/
817-
public function updateBatch(array $entities, array $options = [])
817+
public function updateBatch(array $entities, array $options = []): array
818818
{
819819
$options += [
820820
'allowOverwrite' => false
@@ -860,7 +860,7 @@ public function updateBatch(array $entities, array $options = [])
860860
* @return string The entity version.
861861
* @throws DomainException If a conflict occurs, fail.
862862
*/
863-
public function upsert(EntityInterface $entity, array $options = [])
863+
public function upsert(EntityInterface $entity, array $options = []): string
864864
{
865865
$res = $this->upsertBatch([$entity], $options);
866866
return $this->parseSingleMutationResult($res);
@@ -903,7 +903,7 @@ public function upsert(EntityInterface $entity, array $options = [])
903903
* @param array $options [optional] Configuration Options.
904904
* @return array [Response Body](https://cloud.google.com/datastore/reference/rest/v1/projects/commit#response-body)
905905
*/
906-
public function upsertBatch(array $entities, array $options = [])
906+
public function upsertBatch(array $entities, array $options = []): array
907907
{
908908
$entities = $this->operation->allocateIdsToEntities($entities);
909909
$mutations = [];
@@ -941,7 +941,7 @@ public function upsertBatch(array $entities, array $options = [])
941941
* @return string The updated entity version number.
942942
* @throws DomainException If a conflict occurs, fail.
943943
*/
944-
public function delete(Key $key, array $options = [])
944+
public function delete(Key $key, array $options = []): string
945945
{
946946
$res = $this->deleteBatch([$key], $options);
947947
return $this->parseSingleMutationResult($res);
@@ -976,7 +976,7 @@ public function delete(Key $key, array $options = [])
976976
* }
977977
* @return array [Response Body](https://cloud.google.com/datastore/reference/rest/v1/projects/commit#response-body)
978978
*/
979-
public function deleteBatch(array $keys, array $options = [])
979+
public function deleteBatch(array $keys, array $options = []): array
980980
{
981981
$options += [
982982
'baseVersion' => null
@@ -1033,7 +1033,7 @@ public function deleteBatch(array $keys, array $options = [])
10331033
* }
10341034
* @return EntityInterface|null
10351035
*/
1036-
public function lookup(Key $key, array $options = [])
1036+
public function lookup(Key $key, array $options = []): ?EntityInterface
10371037
{
10381038
$res = $this->lookupBatch([$key], $options);
10391039

@@ -1100,7 +1100,7 @@ public function lookup(Key $key, array $options = [])
11001100
* {@see \Google\Cloud\Datastore\Entity}. Members of `missing` and
11011101
* `deferred` will be instance of {@see \Google\Cloud\Datastore\Key}.
11021102
*/
1103-
public function lookupBatch(array $keys, array $options = [])
1103+
public function lookupBatch(array $keys, array $options = []): array
11041104
{
11051105
return $this->operation->lookup($keys, $options);
11061106
}
@@ -1119,7 +1119,7 @@ public function lookupBatch(array $keys, array $options = [])
11191119
* @param array $query [Query](https://cloud.google.com/datastore/reference/rest/v1/projects/runQuery#query)
11201120
* @return Query
11211121
*/
1122-
public function query(array $query = [])
1122+
public function query(array $query = []): Query
11231123
{
11241124
return new Query($this->entityMapper, [
11251125
'query' => $query
@@ -1139,7 +1139,7 @@ public function query(array $query = [])
11391139
* @param array $query [Query](https://cloud.google.com/datastore/reference/rest/v1/projects/runQuery#query)
11401140
* @return AggregationQuery
11411141
*/
1142-
public function aggregationQuery(array $query = [])
1142+
public function aggregationQuery(array $query = []): AggregationQuery
11431143
{
11441144
return new AggregationQuery($this->query($query));
11451145
}
@@ -1212,7 +1212,7 @@ public function aggregationQuery(array $query = [])
12121212
* }
12131213
* @return GqlQuery
12141214
*/
1215-
public function gqlQuery($query, array $options = [])
1215+
public function gqlQuery(string $query, array $options = []): GqlQuery
12161216
{
12171217
return new GqlQuery($this->entityMapper, $query, $options);
12181218
}
@@ -1294,7 +1294,7 @@ public function runQuery(QueryInterface $query, array $options = [])
12941294
* }
12951295
* @return AggregationQueryResult
12961296
*/
1297-
public function runAggregationQuery(AggregationQuery $query, array $options = [])
1297+
public function runAggregationQuery(AggregationQuery $query, array $options = []): AggregationQueryResult
12981298
{
12991299
return $this->operation->runAggregationQuery($query, $options);
13001300
}
@@ -1308,7 +1308,7 @@ public function runAggregationQuery(AggregationQuery $query, array $options = []
13081308
* @throws DomainException
13091309
* @codingStandardsIgnoreEnd
13101310
*/
1311-
private function parseSingleMutationResult(array $res)
1311+
private function parseSingleMutationResult(array $res): string
13121312
{
13131313
$mutationResult = $res['mutationResults'][0];
13141314

Datastore/src/DatastoreSessionHandler.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,6 @@ class DatastoreSessionHandler implements SessionHandlerInterface
121121
const NAMESPACE_ALLOWED_PATTERN = '/^[A-Za-z\d\.\-_]{0,100}$/';
122122
const NAMESPACE_RESERVED_PATTERN = '/^__.*__$/';
123123

124-
/* @var int */
125-
private $gcLimit;
126-
127-
/* @var DatastoreClient */
128-
private $datastore;
129-
130124
/* @var string */
131125
private $kind;
132126

@@ -139,9 +133,6 @@ class DatastoreSessionHandler implements SessionHandlerInterface
139133
/* @var Transaction */
140134
private $transaction;
141135

142-
/* @var array */
143-
private $options;
144-
145136
/**
146137
* Create a custom session handler backed by Cloud Datastore.
147138
*
@@ -159,18 +150,14 @@ class DatastoreSessionHandler implements SessionHandlerInterface
159150
* }
160151
*/
161152
public function __construct(
162-
DatastoreClient $datastore,
163-
$gcLimit = self::DEFAULT_GC_LIMIT,
164-
array $options = []
153+
private DatastoreClient $datastore,
154+
private int $gcLimit = self::DEFAULT_GC_LIMIT,
155+
private array $options = []
165156
) {
166-
$this->datastore = $datastore;
167157
// Cut down to 1000
168158
$this->gcLimit = min($gcLimit, 1000);
169159

170-
$options += [
171-
'databaseId' => '',
172-
];
173-
$this->databaseId = $options['databaseId'];
160+
$this->databaseId = $options['databaseId'] ?? '';
174161
if (!isset($options['entityOptions'])) {
175162
$options['entityOptions'] = [
176163
'excludeFromIndexes' => ['data']
@@ -184,7 +171,6 @@ public function __construct(
184171
: gettype($options['entityOptions']))
185172
);
186173
}
187-
188174
$this->options = $options;
189175
}
190176

@@ -325,7 +311,7 @@ public function destroy($id)
325311
public function gc($maxlifetime)
326312
{
327313
if ($this->gcLimit === 0) {
328-
return true;
314+
return 0;
329315
}
330316
try {
331317
$query = $this->datastore->query()
@@ -356,6 +342,6 @@ public function gc($maxlifetime)
356342
);
357343
return false;
358344
}
359-
return true;
345+
return count($keys);
360346
}
361347
}

Datastore/src/DatastoreTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ trait DatastoreTrait
3030
* @param string $databaseId
3131
* @return array
3232
*/
33-
private function partitionId($projectId, $namespaceId, $databaseId = '')
33+
private function partitionId(string $projectId, ?string $namespaceId, string $databaseId = ''): array
3434
{
3535
return array_filter([
3636
'databaseId' => $databaseId,

0 commit comments

Comments
 (0)