Skip to content

PHPLIB-1152: replace private static with private const #1099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<code>$type</code>
</PropertyNotSetInConstructor>
</file>
<file src="src/ChangeStream.php">
<DeprecatedConstant occurrences="1">
<code>self::CURSOR_NOT_FOUND</code>
</DeprecatedConstant>
</file>
<file src="src/Client.php">
<MixedArgument occurrences="1">
<code>$driverOptions['driver'] ?? []</code>
Expand Down
17 changes: 6 additions & 11 deletions src/ChangeStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,11 @@ class ChangeStream implements Iterator
{
/**
* @deprecated 1.4
* @todo Remove this in 2.0 (see: PHPLIB-360)
* @todo make this constant private in 2.0 (see: PHPLIB-360)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I understand why this was changed. You couldn't convert private static $cursorNotFound to a private const because of this existing definition.

I just revised PHPLIB-360 accordingly to capture this change.

*/
public const CURSOR_NOT_FOUND = 43;

/** @var int */
private static $cursorNotFound = 43;

/** @var int[] */
private static $resumableErrorCodes = [
private const RESUMABLE_ERROR_CODES = [
6, // HostUnreachable
7, // HostNotFound
89, // NetworkTimeout
Expand All @@ -70,8 +66,7 @@ class ChangeStream implements Iterator
133, // FailedToSatisfyReadPreference
];

/** @var int */
private static $wireVersionForResumableChangeStreamError = 9;
private const WIRE_VERSION_FOR_RESUMABLE_CHANGE_STREAM_ERROR = 9;

/** @var ResumeCallable|null */
private $resumeCallable;
Expand Down Expand Up @@ -205,15 +200,15 @@ private function isResumableError(RuntimeException $exception): bool
return false;
}

if ($exception->getCode() === self::$cursorNotFound) {
if ($exception->getCode() === self::CURSOR_NOT_FOUND) {
return true;
}

if (server_supports_feature($this->iterator->getServer(), self::$wireVersionForResumableChangeStreamError)) {
if (server_supports_feature($this->iterator->getServer(), self::WIRE_VERSION_FOR_RESUMABLE_CHANGE_STREAM_ERROR)) {
return $exception->hasErrorLabel('ResumableChangeStreamError');
}

return in_array($exception->getCode(), self::$resumableErrorCodes);
return in_array($exception->getCode(), self::RESUMABLE_ERROR_CODES);
}

/**
Expand Down
12 changes: 5 additions & 7 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,13 @@ class Client
{
public const DEFAULT_URI = 'mongodb://127.0.0.1/';

/** @var array */
private static $defaultTypeMap = [
private const DEFAULT_TYPE_MAP = [
'array' => BSONArray::class,
'document' => BSONDocument::class,
'root' => BSONDocument::class,
];

/** @var string */
private static $handshakeSeparator = ' / ';
private const HANDSHAKE_SEPARATOR = '/';

/** @var string|null */
private static $version;
Expand Down Expand Up @@ -102,7 +100,7 @@ class Client
*/
public function __construct(?string $uri = null, array $uriOptions = [], array $driverOptions = [])
{
$driverOptions += ['typeMap' => self::$defaultTypeMap];
$driverOptions += ['typeMap' => self::DEFAULT_TYPE_MAP];

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

$mergedDriver['name'] .= self::$handshakeSeparator . $driver['name'];
$mergedDriver['name'] .= self::HANDSHAKE_SEPARATOR . $driver['name'];
}

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

$mergedDriver['version'] .= self::$handshakeSeparator . $driver['version'];
$mergedDriver['version'] .= self::HANDSHAKE_SEPARATOR . $driver['version'];
}

if (isset($driver['platform'])) {
Expand Down
10 changes: 4 additions & 6 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,13 @@

class Collection
{
/** @var array */
private static $defaultTypeMap = [
private const DEFAULT_TYPE_MAP = [
'array' => BSONArray::class,
'document' => BSONDocument::class,
'root' => BSONDocument::class,
];

/** @var integer */
private static $wireVersionForReadConcernWithWriteStage = 8;
private const WIRE_VERSION_FOR_READ_CONCERN_WITH_WRITE_STAGE = 8;

/** @var string */
private $collectionName;
Expand Down Expand Up @@ -158,7 +156,7 @@ public function __construct(Manager $manager, string $databaseName, string $coll
$this->collectionName = $collectionName;
$this->readConcern = $options['readConcern'] ?? $this->manager->getReadConcern();
$this->readPreference = $options['readPreference'] ?? $this->manager->getReadPreference();
$this->typeMap = $options['typeMap'] ?? self::$defaultTypeMap;
$this->typeMap = $options['typeMap'] ?? self::DEFAULT_TYPE_MAP;
$this->writeConcern = $options['writeConcern'] ?? $this->manager->getWriteConcern();
}

Expand Down Expand Up @@ -229,7 +227,7 @@ public function aggregate(array $pipeline, array $options = [])
if (
! isset($options['readConcern']) &&
! is_in_transaction($options) &&
( ! $hasWriteStage || server_supports_feature($server, self::$wireVersionForReadConcernWithWriteStage))
( ! $hasWriteStage || server_supports_feature($server, self::WIRE_VERSION_FOR_READ_CONCERN_WITH_WRITE_STAGE))
) {
$options['readConcern'] = $this->readConcern;
}
Expand Down
10 changes: 4 additions & 6 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@

class Database
{
/** @var array */
private static $defaultTypeMap = [
private const DEFAULT_TYPE_MAP = [
'array' => BSONArray::class,
'document' => BSONDocument::class,
'root' => BSONDocument::class,
];

/** @var integer */
private static $wireVersionForReadConcernWithWriteStage = 8;
private const WIRE_VERSION_FOR_READ_CONCERN_WITH_WRITE_STAGE = 8;

/** @var string */
private $databaseName;
Expand Down Expand Up @@ -134,7 +132,7 @@ public function __construct(Manager $manager, string $databaseName, array $optio
$this->databaseName = $databaseName;
$this->readConcern = $options['readConcern'] ?? $this->manager->getReadConcern();
$this->readPreference = $options['readPreference'] ?? $this->manager->getReadPreference();
$this->typeMap = $options['typeMap'] ?? self::$defaultTypeMap;
$this->typeMap = $options['typeMap'] ?? self::DEFAULT_TYPE_MAP;
$this->writeConcern = $options['writeConcern'] ?? $this->manager->getWriteConcern();
}

Expand Down Expand Up @@ -217,7 +215,7 @@ public function aggregate(array $pipeline, array $options = [])
if (
! isset($options['readConcern']) &&
! is_in_transaction($options) &&
( ! $hasWriteStage || server_supports_feature($server, self::$wireVersionForReadConcernWithWriteStage))
( ! $hasWriteStage || server_supports_feature($server, self::WIRE_VERSION_FOR_READ_CONCERN_WITH_WRITE_STAGE))
) {
$options['readConcern'] = $this->readConcern;
}
Expand Down
30 changes: 13 additions & 17 deletions src/GridFS/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,17 @@
*/
class Bucket
{
/** @var string */
private static $defaultBucketName = 'fs';
private const DEFAULT_BUCKET_NAME = 'fs';

/** @var integer */
private static $defaultChunkSizeBytes = 261120;
private const DEFAULT_CHUNK_SIZE_BYTES = 261120;

/** @var array */
private static $defaultTypeMap = [
private const DEFAULT_TYPE_MAP = [
'array' => BSONArray::class,
'document' => BSONDocument::class,
'root' => BSONDocument::class,
];

/** @var string */
private static $streamWrapperProtocol = 'gridfs';
private const STREAM_WRAPPER_PROTOCOL = 'gridfs';

/** @var CollectionWrapper */
private $collectionWrapper;
Expand Down Expand Up @@ -138,8 +134,8 @@ class Bucket
public function __construct(Manager $manager, string $databaseName, array $options = [])
{
$options += [
'bucketName' => self::$defaultBucketName,
'chunkSizeBytes' => self::$defaultChunkSizeBytes,
'bucketName' => self::DEFAULT_BUCKET_NAME,
'chunkSizeBytes' => self::DEFAULT_CHUNK_SIZE_BYTES,
'disableMD5' => false,
];

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

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

$path = $this->createPathForUpload();
$context = stream_context_create([
self::$streamWrapperProtocol => [
self::STREAM_WRAPPER_PROTOCOL => [
'collectionWrapper' => $this->collectionWrapper,
'filename' => $filename,
'options' => $options,
Expand Down Expand Up @@ -651,7 +647,7 @@ private function createPathForFile(object $file): string

return sprintf(
'%s://%s/%s.files/%s',
self::$streamWrapperProtocol,
self::STREAM_WRAPPER_PROTOCOL,
urlencode($this->databaseName),
urlencode($this->bucketName),
urlencode($id)
Expand All @@ -665,7 +661,7 @@ private function createPathForUpload(): string
{
return sprintf(
'%s://%s/%s.files',
self::$streamWrapperProtocol,
self::STREAM_WRAPPER_PROTOCOL,
urlencode($this->databaseName),
urlencode($this->bucketName)
);
Expand Down Expand Up @@ -713,7 +709,7 @@ private function openDownloadStreamByFile(object $file)
{
$path = $this->createPathForFile($file);
$context = stream_context_create([
self::$streamWrapperProtocol => [
self::STREAM_WRAPPER_PROTOCOL => [
'collectionWrapper' => $this->collectionWrapper,
'file' => $file,
],
Expand All @@ -727,10 +723,10 @@ private function openDownloadStreamByFile(object $file)
*/
private function registerStreamWrapper(): void
{
if (in_array(self::$streamWrapperProtocol, stream_get_wrappers())) {
if (in_array(self::STREAM_WRAPPER_PROTOCOL, stream_get_wrappers())) {
return;
}

StreamWrapper::register(self::$streamWrapperProtocol);
StreamWrapper::register(self::STREAM_WRAPPER_PROTOCOL);
}
}
5 changes: 2 additions & 3 deletions src/GridFS/WritableStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
*/
class WritableStream
{
/** @var integer */
private static $defaultChunkSizeBytes = 261120;
private const DEFAULT_CHUNK_SIZE_BYTES = 261120;

/** @var string */
private $buffer = '';
Expand Down Expand Up @@ -107,7 +106,7 @@ public function __construct(CollectionWrapper $collectionWrapper, string $filena
{
$options += [
'_id' => new ObjectId(),
'chunkSizeBytes' => self::$defaultChunkSizeBytes,
'chunkSizeBytes' => self::DEFAULT_CHUNK_SIZE_BYTES,
'disableMD5' => false,
];

Expand Down
9 changes: 4 additions & 5 deletions src/Model/BSONIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
*/
class BSONIterator implements Iterator
{
/** @var integer */
private static $bsonSize = 4;
private const BSON_SIZE = 4;

/** @var string */
private $buffer;
Expand Down Expand Up @@ -141,11 +140,11 @@ private function advance(): void
return;
}

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

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

if ($this->bufferLength - $this->position < $documentLength) {
throw new UnexpectedValueException(sprintf('Expected %d bytes; %d remaining', $documentLength, $this->bufferLength - $this->position));
Expand Down
5 changes: 2 additions & 3 deletions src/Operation/CreateEncryptedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
*/
class CreateEncryptedCollection implements Executable
{
/** @var integer */
private static $wireVersionForQueryableEncryptionV2 = 21;
private const WIRE_VERSION_FOR_QUERYABLE_ENCRYPTION_V2 = 21;

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

Expand Down
5 changes: 2 additions & 3 deletions src/Operation/CreateIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
*/
class CreateIndexes implements Executable
{
/** @var integer */
private static $wireVersionForCommitQuorum = 9;
private const WIRE_VERSION_FOR_COMMIT_QUORUM = 9;

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

Expand Down
5 changes: 2 additions & 3 deletions src/Operation/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
*/
class Delete implements Executable, Explainable
{
/** @var integer */
private static $wireVersionForHint = 9;
private const WIRE_VERSION_FOR_HINT = 9;

/** @var string */
private $databaseName;
Expand Down Expand Up @@ -152,7 +151,7 @@ public function execute(Server $server)
* unacknowledged write concern on an unsupported server. */
if (
isset($this->options['writeConcern']) && ! is_write_concern_acknowledged($this->options['writeConcern']) &&
isset($this->options['hint']) && ! server_supports_feature($server, self::$wireVersionForHint)
isset($this->options['hint']) && ! server_supports_feature($server, self::WIRE_VERSION_FOR_HINT)
) {
throw UnsupportedException::hintNotSupported();
}
Expand Down
5 changes: 2 additions & 3 deletions src/Operation/DropCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
*/
class DropCollection implements Executable
{
/** @var integer */
private static $errorCodeNamespaceNotFound = 26;
private const ERROR_CODE_NAMESPACE_NOT_FOUND = 26;

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

Expand Down
Loading