Skip to content

Commit c0738eb

Browse files
GromNaNjmikola
andauthored
PHPLIB-1152: replace private static with private const (#1099)
Exception Client::$version which is initialized dynamically Co-authored-by: Jeremy Mikola <jmikola@gmail.com>
1 parent 14c22fd commit c0738eb

17 files changed

+64
-88
lines changed

psalm-baseline.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
<code>$type</code>
1010
</PropertyNotSetInConstructor>
1111
</file>
12+
<file src="src/ChangeStream.php">
13+
<DeprecatedConstant occurrences="1">
14+
<code>self::CURSOR_NOT_FOUND</code>
15+
</DeprecatedConstant>
16+
</file>
1217
<file src="src/Client.php">
1318
<MixedArgument occurrences="1">
1419
<code>$driverOptions['driver'] ?? []</code>

src/ChangeStream.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,11 @@ class ChangeStream implements Iterator
4242
{
4343
/**
4444
* @deprecated 1.4
45-
* @todo Remove this in 2.0 (see: PHPLIB-360)
45+
* @todo make this constant private in 2.0 (see: PHPLIB-360)
4646
*/
4747
public const CURSOR_NOT_FOUND = 43;
4848

49-
/** @var int */
50-
private static $cursorNotFound = 43;
51-
52-
/** @var int[] */
53-
private static $resumableErrorCodes = [
49+
private const RESUMABLE_ERROR_CODES = [
5450
6, // HostUnreachable
5551
7, // HostNotFound
5652
89, // NetworkTimeout
@@ -70,8 +66,7 @@ class ChangeStream implements Iterator
7066
133, // FailedToSatisfyReadPreference
7167
];
7268

73-
/** @var int */
74-
private static $wireVersionForResumableChangeStreamError = 9;
69+
private const WIRE_VERSION_FOR_RESUMABLE_CHANGE_STREAM_ERROR = 9;
7570

7671
/** @var ResumeCallable|null */
7772
private $resumeCallable;
@@ -205,15 +200,15 @@ private function isResumableError(RuntimeException $exception): bool
205200
return false;
206201
}
207202

208-
if ($exception->getCode() === self::$cursorNotFound) {
203+
if ($exception->getCode() === self::CURSOR_NOT_FOUND) {
209204
return true;
210205
}
211206

212-
if (server_supports_feature($this->iterator->getServer(), self::$wireVersionForResumableChangeStreamError)) {
207+
if (server_supports_feature($this->iterator->getServer(), self::WIRE_VERSION_FOR_RESUMABLE_CHANGE_STREAM_ERROR)) {
213208
return $exception->hasErrorLabel('ResumableChangeStreamError');
214209
}
215210

216-
return in_array($exception->getCode(), self::$resumableErrorCodes);
211+
return in_array($exception->getCode(), self::RESUMABLE_ERROR_CODES);
217212
}
218213

219214
/**

src/Client.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,13 @@ class Client
4646
{
4747
public const DEFAULT_URI = 'mongodb://127.0.0.1/';
4848

49-
/** @var array */
50-
private static $defaultTypeMap = [
49+
private const DEFAULT_TYPE_MAP = [
5150
'array' => BSONArray::class,
5251
'document' => BSONDocument::class,
5352
'root' => BSONDocument::class,
5453
];
5554

56-
/** @var string */
57-
private static $handshakeSeparator = ' / ';
55+
private const HANDSHAKE_SEPARATOR = '/';
5856

5957
/** @var string|null */
6058
private static $version;
@@ -102,7 +100,7 @@ class Client
102100
*/
103101
public function __construct(?string $uri = null, array $uriOptions = [], array $driverOptions = [])
104102
{
105-
$driverOptions += ['typeMap' => self::$defaultTypeMap];
103+
$driverOptions += ['typeMap' => self::DEFAULT_TYPE_MAP];
106104

107105
if (! is_array($driverOptions['typeMap'])) {
108106
throw InvalidArgumentException::invalidType('"typeMap" driver option', $driverOptions['typeMap'], 'array');
@@ -405,15 +403,15 @@ private function mergeDriverInfo(array $driver): array
405403
throw InvalidArgumentException::invalidType('"name" handshake option', $driver['name'], 'string');
406404
}
407405

408-
$mergedDriver['name'] .= self::$handshakeSeparator . $driver['name'];
406+
$mergedDriver['name'] .= self::HANDSHAKE_SEPARATOR . $driver['name'];
409407
}
410408

411409
if (isset($driver['version'])) {
412410
if (! is_string($driver['version'])) {
413411
throw InvalidArgumentException::invalidType('"version" handshake option', $driver['version'], 'string');
414412
}
415413

416-
$mergedDriver['version'] .= self::$handshakeSeparator . $driver['version'];
414+
$mergedDriver['version'] .= self::HANDSHAKE_SEPARATOR . $driver['version'];
417415
}
418416

419417
if (isset($driver['platform'])) {

src/Collection.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,13 @@
6969

7070
class Collection
7171
{
72-
/** @var array */
73-
private static $defaultTypeMap = [
72+
private const DEFAULT_TYPE_MAP = [
7473
'array' => BSONArray::class,
7574
'document' => BSONDocument::class,
7675
'root' => BSONDocument::class,
7776
];
7877

79-
/** @var integer */
80-
private static $wireVersionForReadConcernWithWriteStage = 8;
78+
private const WIRE_VERSION_FOR_READ_CONCERN_WITH_WRITE_STAGE = 8;
8179

8280
/** @var string */
8381
private $collectionName;
@@ -158,7 +156,7 @@ public function __construct(Manager $manager, string $databaseName, string $coll
158156
$this->collectionName = $collectionName;
159157
$this->readConcern = $options['readConcern'] ?? $this->manager->getReadConcern();
160158
$this->readPreference = $options['readPreference'] ?? $this->manager->getReadPreference();
161-
$this->typeMap = $options['typeMap'] ?? self::$defaultTypeMap;
159+
$this->typeMap = $options['typeMap'] ?? self::DEFAULT_TYPE_MAP;
162160
$this->writeConcern = $options['writeConcern'] ?? $this->manager->getWriteConcern();
163161
}
164162

@@ -229,7 +227,7 @@ public function aggregate(array $pipeline, array $options = [])
229227
if (
230228
! isset($options['readConcern']) &&
231229
! is_in_transaction($options) &&
232-
( ! $hasWriteStage || server_supports_feature($server, self::$wireVersionForReadConcernWithWriteStage))
230+
( ! $hasWriteStage || server_supports_feature($server, self::WIRE_VERSION_FOR_READ_CONCERN_WITH_WRITE_STAGE))
233231
) {
234232
$options['readConcern'] = $this->readConcern;
235233
}

src/Database.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,13 @@
5353

5454
class Database
5555
{
56-
/** @var array */
57-
private static $defaultTypeMap = [
56+
private const DEFAULT_TYPE_MAP = [
5857
'array' => BSONArray::class,
5958
'document' => BSONDocument::class,
6059
'root' => BSONDocument::class,
6160
];
6261

63-
/** @var integer */
64-
private static $wireVersionForReadConcernWithWriteStage = 8;
62+
private const WIRE_VERSION_FOR_READ_CONCERN_WITH_WRITE_STAGE = 8;
6563

6664
/** @var string */
6765
private $databaseName;
@@ -134,7 +132,7 @@ public function __construct(Manager $manager, string $databaseName, array $optio
134132
$this->databaseName = $databaseName;
135133
$this->readConcern = $options['readConcern'] ?? $this->manager->getReadConcern();
136134
$this->readPreference = $options['readPreference'] ?? $this->manager->getReadPreference();
137-
$this->typeMap = $options['typeMap'] ?? self::$defaultTypeMap;
135+
$this->typeMap = $options['typeMap'] ?? self::DEFAULT_TYPE_MAP;
138136
$this->writeConcern = $options['writeConcern'] ?? $this->manager->getWriteConcern();
139137
}
140138

@@ -217,7 +215,7 @@ public function aggregate(array $pipeline, array $options = [])
217215
if (
218216
! isset($options['readConcern']) &&
219217
! is_in_transaction($options) &&
220-
( ! $hasWriteStage || server_supports_feature($server, self::$wireVersionForReadConcernWithWriteStage))
218+
( ! $hasWriteStage || server_supports_feature($server, self::WIRE_VERSION_FOR_READ_CONCERN_WITH_WRITE_STAGE))
221219
) {
222220
$options['readConcern'] = $this->readConcern;
223221
}

src/GridFS/Bucket.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,17 @@
6262
*/
6363
class Bucket
6464
{
65-
/** @var string */
66-
private static $defaultBucketName = 'fs';
65+
private const DEFAULT_BUCKET_NAME = 'fs';
6766

68-
/** @var integer */
69-
private static $defaultChunkSizeBytes = 261120;
67+
private const DEFAULT_CHUNK_SIZE_BYTES = 261120;
7068

71-
/** @var array */
72-
private static $defaultTypeMap = [
69+
private const DEFAULT_TYPE_MAP = [
7370
'array' => BSONArray::class,
7471
'document' => BSONDocument::class,
7572
'root' => BSONDocument::class,
7673
];
7774

78-
/** @var string */
79-
private static $streamWrapperProtocol = 'gridfs';
75+
private const STREAM_WRAPPER_PROTOCOL = 'gridfs';
8076

8177
/** @var CollectionWrapper */
8278
private $collectionWrapper;
@@ -138,8 +134,8 @@ class Bucket
138134
public function __construct(Manager $manager, string $databaseName, array $options = [])
139135
{
140136
$options += [
141-
'bucketName' => self::$defaultBucketName,
142-
'chunkSizeBytes' => self::$defaultChunkSizeBytes,
137+
'bucketName' => self::DEFAULT_BUCKET_NAME,
138+
'chunkSizeBytes' => self::DEFAULT_CHUNK_SIZE_BYTES,
143139
'disableMD5' => false,
144140
];
145141

@@ -182,7 +178,7 @@ public function __construct(Manager $manager, string $databaseName, array $optio
182178
$this->disableMD5 = $options['disableMD5'];
183179
$this->readConcern = $options['readConcern'] ?? $this->manager->getReadConcern();
184180
$this->readPreference = $options['readPreference'] ?? $this->manager->getReadPreference();
185-
$this->typeMap = $options['typeMap'] ?? self::$defaultTypeMap;
181+
$this->typeMap = $options['typeMap'] ?? self::DEFAULT_TYPE_MAP;
186182
$this->writeConcern = $options['writeConcern'] ?? $this->manager->getWriteConcern();
187183

188184
$collectionOptions = array_intersect_key($options, ['readConcern' => 1, 'readPreference' => 1, 'typeMap' => 1, 'writeConcern' => 1]);
@@ -555,7 +551,7 @@ public function openUploadStream(string $filename, array $options = [])
555551

556552
$path = $this->createPathForUpload();
557553
$context = stream_context_create([
558-
self::$streamWrapperProtocol => [
554+
self::STREAM_WRAPPER_PROTOCOL => [
559555
'collectionWrapper' => $this->collectionWrapper,
560556
'filename' => $filename,
561557
'options' => $options,
@@ -651,7 +647,7 @@ private function createPathForFile(object $file): string
651647

652648
return sprintf(
653649
'%s://%s/%s.files/%s',
654-
self::$streamWrapperProtocol,
650+
self::STREAM_WRAPPER_PROTOCOL,
655651
urlencode($this->databaseName),
656652
urlencode($this->bucketName),
657653
urlencode($id)
@@ -665,7 +661,7 @@ private function createPathForUpload(): string
665661
{
666662
return sprintf(
667663
'%s://%s/%s.files',
668-
self::$streamWrapperProtocol,
664+
self::STREAM_WRAPPER_PROTOCOL,
669665
urlencode($this->databaseName),
670666
urlencode($this->bucketName)
671667
);
@@ -713,7 +709,7 @@ private function openDownloadStreamByFile(object $file)
713709
{
714710
$path = $this->createPathForFile($file);
715711
$context = stream_context_create([
716-
self::$streamWrapperProtocol => [
712+
self::STREAM_WRAPPER_PROTOCOL => [
717713
'collectionWrapper' => $this->collectionWrapper,
718714
'file' => $file,
719715
],
@@ -727,10 +723,10 @@ private function openDownloadStreamByFile(object $file)
727723
*/
728724
private function registerStreamWrapper(): void
729725
{
730-
if (in_array(self::$streamWrapperProtocol, stream_get_wrappers())) {
726+
if (in_array(self::STREAM_WRAPPER_PROTOCOL, stream_get_wrappers())) {
731727
return;
732728
}
733729

734-
StreamWrapper::register(self::$streamWrapperProtocol);
730+
StreamWrapper::register(self::STREAM_WRAPPER_PROTOCOL);
735731
}
736732
}

src/GridFS/WritableStream.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@
4545
*/
4646
class WritableStream
4747
{
48-
/** @var integer */
49-
private static $defaultChunkSizeBytes = 261120;
48+
private const DEFAULT_CHUNK_SIZE_BYTES = 261120;
5049

5150
/** @var string */
5251
private $buffer = '';
@@ -107,7 +106,7 @@ public function __construct(CollectionWrapper $collectionWrapper, string $filena
107106
{
108107
$options += [
109108
'_id' => new ObjectId(),
110-
'chunkSizeBytes' => self::$defaultChunkSizeBytes,
109+
'chunkSizeBytes' => self::DEFAULT_CHUNK_SIZE_BYTES,
111110
'disableMD5' => false,
112111
];
113112

src/Model/BSONIterator.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
*/
3535
class BSONIterator implements Iterator
3636
{
37-
/** @var integer */
38-
private static $bsonSize = 4;
37+
private const BSON_SIZE = 4;
3938

4039
/** @var string */
4140
private $buffer;
@@ -141,11 +140,11 @@ private function advance(): void
141140
return;
142141
}
143142

144-
if ($this->bufferLength - $this->position < self::$bsonSize) {
145-
throw new UnexpectedValueException(sprintf('Expected at least %d bytes; %d remaining', self::$bsonSize, $this->bufferLength - $this->position));
143+
if ($this->bufferLength - $this->position < self::BSON_SIZE) {
144+
throw new UnexpectedValueException(sprintf('Expected at least %d bytes; %d remaining', self::BSON_SIZE, $this->bufferLength - $this->position));
146145
}
147146

148-
[, $documentLength] = unpack('V', substr($this->buffer, $this->position, self::$bsonSize));
147+
[, $documentLength] = unpack('V', substr($this->buffer, $this->position, self::BSON_SIZE));
149148

150149
if ($this->bufferLength - $this->position < $documentLength) {
151150
throw new UnexpectedValueException(sprintf('Expected %d bytes; %d remaining', $documentLength, $this->bufferLength - $this->position));

src/Operation/CreateEncryptedCollection.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@
4949
*/
5050
class CreateEncryptedCollection implements Executable
5151
{
52-
/** @var integer */
53-
private static $wireVersionForQueryableEncryptionV2 = 21;
52+
private const WIRE_VERSION_FOR_QUERYABLE_ENCRYPTION_V2 = 21;
5453

5554
/** @var CreateCollection */
5655
private $createCollection;
@@ -177,7 +176,7 @@ public function createDataKeys(ClientEncryption $clientEncryption, string $kmsPr
177176
*/
178177
public function execute(Server $server)
179178
{
180-
if (! server_supports_feature($server, self::$wireVersionForQueryableEncryptionV2)) {
179+
if (! server_supports_feature($server, self::WIRE_VERSION_FOR_QUERYABLE_ENCRYPTION_V2)) {
181180
throw new UnsupportedException('Driver support of Queryable Encryption is incompatible with server. Upgrade server to use Queryable Encryption.');
182181
}
183182

src/Operation/CreateIndexes.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
*/
4343
class CreateIndexes implements Executable
4444
{
45-
/** @var integer */
46-
private static $wireVersionForCommitQuorum = 9;
45+
private const WIRE_VERSION_FOR_COMMIT_QUORUM = 9;
4746

4847
/** @var string */
4948
private $databaseName;
@@ -188,7 +187,7 @@ private function executeCommand(Server $server): void
188187
if (isset($this->options['commitQuorum'])) {
189188
/* Drivers MUST manually raise an error if this option is specified
190189
* when creating an index on a pre 4.4 server. */
191-
if (! server_supports_feature($server, self::$wireVersionForCommitQuorum)) {
190+
if (! server_supports_feature($server, self::WIRE_VERSION_FOR_COMMIT_QUORUM)) {
192191
throw UnsupportedException::commitQuorumNotSupported();
193192
}
194193

src/Operation/Delete.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343
*/
4444
class Delete implements Executable, Explainable
4545
{
46-
/** @var integer */
47-
private static $wireVersionForHint = 9;
46+
private const WIRE_VERSION_FOR_HINT = 9;
4847

4948
/** @var string */
5049
private $databaseName;
@@ -152,7 +151,7 @@ public function execute(Server $server)
152151
* unacknowledged write concern on an unsupported server. */
153152
if (
154153
isset($this->options['writeConcern']) && ! is_write_concern_acknowledged($this->options['writeConcern']) &&
155-
isset($this->options['hint']) && ! server_supports_feature($server, self::$wireVersionForHint)
154+
isset($this->options['hint']) && ! server_supports_feature($server, self::WIRE_VERSION_FOR_HINT)
156155
) {
157156
throw UnsupportedException::hintNotSupported();
158157
}

src/Operation/DropCollection.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
*/
3939
class DropCollection implements Executable
4040
{
41-
/** @var integer */
42-
private static $errorCodeNamespaceNotFound = 26;
41+
private const ERROR_CODE_NAMESPACE_NOT_FOUND = 26;
4342

4443
/** @var string */
4544
private $databaseName;
@@ -115,7 +114,7 @@ public function execute(Server $server)
115114
/* The server may return an error if the collection does not exist.
116115
* Check for an error code and return the command reply instead of
117116
* throwing. */
118-
if ($e->getCode() === self::$errorCodeNamespaceNotFound) {
117+
if ($e->getCode() === self::ERROR_CODE_NAMESPACE_NOT_FOUND) {
119118
return $e->getResultDocument();
120119
}
121120

0 commit comments

Comments
 (0)