From 062a19ca65f581978a75c51f5f7e5d533b171101 Mon Sep 17 00:00:00 2001 From: Michiel Vermeersch Date: Wed, 20 Aug 2025 14:30:03 +0200 Subject: [PATCH] Fix PHP 8.5 deprecations of `(integer)`, `(long)` and `(boolean)` casts (#1758) * build(deps-dev): updated rector/rector to ^2.1.4 * chore: fix PHP 8.5 deprecations. --- composer.json | 2 +- rector.php | 49 ++++++++++--------- src/GridFS/Bucket.php | 1 - src/GridFS/ReadableStream.php | 4 +- src/MapReduceResult.php | 2 +- src/Model/CollectionInfo.php | 4 +- src/Model/DatabaseInfo.php | 4 +- src/Model/IndexInfo.php | 2 +- src/Operation/Count.php | 2 +- src/Operation/CountDocuments.php | 2 +- src/functions.php | 4 +- tests/Collection/CollectionFunctionalTest.php | 2 +- tests/Operation/BulkWriteFunctionalTest.php | 2 +- tests/Operation/DeleteFunctionalTest.php | 2 +- tests/Operation/ExplainFunctionalTest.php | 2 +- tests/Operation/UpdateFunctionalTest.php | 2 +- .../Prose22_RangeExplicitEncryptionTest.php | 2 +- tools/connect.php | 2 +- tools/detect-extension.php | 4 +- 19 files changed, 49 insertions(+), 45 deletions(-) diff --git a/composer.json b/composer.json index 0d1438f0a..d4c327360 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "require-dev": { "doctrine/coding-standard": "^12.0", "phpunit/phpunit": "^10.5.35", - "rector/rector": "^1.2", + "rector/rector": "^2.1.4", "squizlabs/php_codesniffer": "^3.7", "vimeo/psalm": "6.5.*" }, diff --git a/rector.php b/rector.php index edfe66f14..f184fd9c0 100644 --- a/rector.php +++ b/rector.php @@ -1,42 +1,47 @@ paths([ +return RectorConfig::configure() + ->withPaths([ __DIR__ . '/examples', __DIR__ . '/src', __DIR__ . '/tests', __DIR__ . '/tools', - ]); - - // Modernize code - $rectorConfig->sets([ - LevelSetList::UP_TO_PHP_74, - PHPUnitSetList::PHPUNIT_100, - ]); - - $rectorConfig->rule(ChangeSwitchToMatchRector::class); - $rectorConfig->rule(StaticDataProviderClassMethodRector::class); - + ]) + ->withPhpSets(php74: true) + ->withComposerBased(phpunit: true) + ->withRules([ + ChangeSwitchToMatchRector::class, + ]) + // All classes are public API by default, unless marked with @internal. + ->withConfiguredRule(RemoveAnnotationRector::class, ['api']) + // Fix PHP 8.5 deprecations + ->withConfiguredRule( + RenameCastRector::class, + [ + new RenameCast(Int_::class, Int_::KIND_INTEGER, Int_::KIND_INT), + new RenameCast(Bool_::class, Bool_::KIND_BOOLEAN, Bool_::KIND_BOOL), + new RenameCast(Double::class, Double::KIND_DOUBLE, Double::KIND_FLOAT), + ], + ) // phpcs:disable Squiz.Arrays.ArrayDeclaration.KeySpecified - $rectorConfig->skip([ + ->withSkip([ RemoveExtraParametersRector::class, // Do not use ternaries extensively IfIssetToCoalescingRector::class, ChangeSwitchToMatchRector::class => [ __DIR__ . '/tests/SpecTests/Operation.php', ], - ]); + ]) // phpcs:enable - - // All classes are public API by default, unless marked with @internal. - $rectorConfig->ruleWithConfiguration(RemoveAnnotationRector::class, ['api']); -}; + ->withImportNames(importNames: false, removeUnusedImports: true); diff --git a/src/GridFS/Bucket.php b/src/GridFS/Bucket.php index 85046906c..84f09c7b6 100644 --- a/src/GridFS/Bucket.php +++ b/src/GridFS/Bucket.php @@ -35,7 +35,6 @@ use MongoDB\GridFS\Exception\StreamException; use MongoDB\Model\BSONArray; use MongoDB\Model\BSONDocument; -use MongoDB\Operation\Find; use function array_intersect_key; use function array_key_exists; diff --git a/src/GridFS/ReadableStream.php b/src/GridFS/ReadableStream.php index 980e3e19f..94275b9b7 100644 --- a/src/GridFS/ReadableStream.php +++ b/src/GridFS/ReadableStream.php @@ -82,7 +82,7 @@ public function __construct(private CollectionWrapper $collectionWrapper, privat $this->length = $file->length; if ($this->length > 0) { - $this->numChunks = (integer) ceil($this->length / $this->chunkSize); + $this->numChunks = (int) ceil($this->length / $this->chunkSize); $this->expectedLastChunkSize = $this->length - (($this->numChunks - 1) * $this->chunkSize); } } @@ -184,7 +184,7 @@ public function seek(int $offset): void * changed, we'll also need to reset the buffer. */ $lastChunkOffset = $this->chunkOffset; - $this->chunkOffset = (integer) floor($offset / $this->chunkSize); + $this->chunkOffset = (int) floor($offset / $this->chunkSize); $this->bufferOffset = $offset % $this->chunkSize; if ($lastChunkOffset === $this->chunkOffset) { diff --git a/src/MapReduceResult.php b/src/MapReduceResult.php index 00a75116a..62e21d0e5 100644 --- a/src/MapReduceResult.php +++ b/src/MapReduceResult.php @@ -104,7 +104,7 @@ public function getTiming() public function __construct(callable $getIterator, stdClass $result) { $this->getIterator = $getIterator; - $this->executionTimeMS = isset($result->timeMillis) ? (integer) $result->timeMillis : 0; + $this->executionTimeMS = isset($result->timeMillis) ? (int) $result->timeMillis : 0; $this->counts = isset($result->counts) ? (array) $result->counts : []; $this->timing = isset($result->timing) ? (array) $result->timing : []; } diff --git a/src/Model/CollectionInfo.php b/src/Model/CollectionInfo.php index 2850a4be4..893fea6bf 100644 --- a/src/Model/CollectionInfo.php +++ b/src/Model/CollectionInfo.php @@ -62,7 +62,7 @@ public function __debugInfo() public function getCappedMax() { /* The MongoDB server might return this number as an integer or float */ - return isset($this->info['options']['max']) ? (integer) $this->info['options']['max'] : null; + return isset($this->info['options']['max']) ? (int) $this->info['options']['max'] : null; } /** @@ -75,7 +75,7 @@ public function getCappedMax() public function getCappedSize() { /* The MongoDB server might return this number as an integer or float */ - return isset($this->info['options']['size']) ? (integer) $this->info['options']['size'] : null; + return isset($this->info['options']['size']) ? (int) $this->info['options']['size'] : null; } /** diff --git a/src/Model/DatabaseInfo.php b/src/Model/DatabaseInfo.php index 2803532f8..c46bfb7d3 100644 --- a/src/Model/DatabaseInfo.php +++ b/src/Model/DatabaseInfo.php @@ -69,7 +69,7 @@ public function getName() public function getSizeOnDisk() { /* The MongoDB server might return this number as an integer or float */ - return (integer) $this->info['sizeOnDisk']; + return (int) $this->info['sizeOnDisk']; } /** @@ -79,7 +79,7 @@ public function getSizeOnDisk() */ public function isEmpty() { - return (boolean) $this->info['empty']; + return (bool) $this->info['empty']; } /** diff --git a/src/Model/IndexInfo.php b/src/Model/IndexInfo.php index 062c40856..eeb4368a5 100644 --- a/src/Model/IndexInfo.php +++ b/src/Model/IndexInfo.php @@ -111,7 +111,7 @@ public function getNamespace() */ public function getVersion() { - return (integer) $this->info['v']; + return (int) $this->info['v']; } /** diff --git a/src/Operation/Count.php b/src/Operation/Count.php index f3d2b7eb6..1c51aff60 100644 --- a/src/Operation/Count.php +++ b/src/Operation/Count.php @@ -147,7 +147,7 @@ public function execute(Server $server) throw new UnexpectedValueException('count command did not return a numeric "n" value'); } - return (integer) $result->n; + return (int) $result->n; } /** diff --git a/src/Operation/CountDocuments.php b/src/Operation/CountDocuments.php index 954ea7466..76b111ebe 100644 --- a/src/Operation/CountDocuments.php +++ b/src/Operation/CountDocuments.php @@ -128,7 +128,7 @@ public function execute(Server $server) throw new UnexpectedValueException('count command did not return a numeric "n" value'); } - return (integer) $result->n; + return (int) $result->n; } private function createAggregate(): Aggregate diff --git a/src/functions.php b/src/functions.php index 1c23d4ad8..b38fc1fae 100644 --- a/src/functions.php +++ b/src/functions.php @@ -430,8 +430,8 @@ function is_write_concern_acknowledged(WriteConcern $writeConcern): bool function server_supports_feature(Server $server, int $feature): bool { $info = $server->getInfo(); - $maxWireVersion = isset($info['maxWireVersion']) ? (integer) $info['maxWireVersion'] : 0; - $minWireVersion = isset($info['minWireVersion']) ? (integer) $info['minWireVersion'] : 0; + $maxWireVersion = isset($info['maxWireVersion']) ? (int) $info['maxWireVersion'] : 0; + $minWireVersion = isset($info['minWireVersion']) ? (int) $info['minWireVersion'] : 0; return $minWireVersion <= $feature && $maxWireVersion >= $feature; } diff --git a/tests/Collection/CollectionFunctionalTest.php b/tests/Collection/CollectionFunctionalTest.php index fac590048..1c7af5be3 100644 --- a/tests/Collection/CollectionFunctionalTest.php +++ b/tests/Collection/CollectionFunctionalTest.php @@ -852,7 +852,7 @@ private function createFixtures(int $n, array $executeBulkWriteOptions = []): vo for ($i = 1; $i <= $n; $i++) { $bulkWrite->insert([ '_id' => $i, - 'x' => (integer) ($i . $i), + 'x' => (int) ($i . $i), ]); } diff --git a/tests/Operation/BulkWriteFunctionalTest.php b/tests/Operation/BulkWriteFunctionalTest.php index 7a9cb5df9..1322589f4 100644 --- a/tests/Operation/BulkWriteFunctionalTest.php +++ b/tests/Operation/BulkWriteFunctionalTest.php @@ -500,7 +500,7 @@ private function createFixtures(int $n): void for ($i = 1; $i <= $n; $i++) { $bulkWrite->insert([ '_id' => $i, - 'x' => (integer) ($i . $i), + 'x' => (int) ($i . $i), ]); } diff --git a/tests/Operation/DeleteFunctionalTest.php b/tests/Operation/DeleteFunctionalTest.php index b501aa21b..bf59b8822 100644 --- a/tests/Operation/DeleteFunctionalTest.php +++ b/tests/Operation/DeleteFunctionalTest.php @@ -153,7 +153,7 @@ private function createFixtures(int $n): void for ($i = 1; $i <= $n; $i++) { $bulkWrite->insert([ '_id' => $i, - 'x' => (integer) ($i . $i), + 'x' => (int) ($i . $i), ]); } diff --git a/tests/Operation/ExplainFunctionalTest.php b/tests/Operation/ExplainFunctionalTest.php index e40024bff..a7696eeeb 100644 --- a/tests/Operation/ExplainFunctionalTest.php +++ b/tests/Operation/ExplainFunctionalTest.php @@ -406,7 +406,7 @@ private function createFixtures(int $n): void for ($i = 1; $i <= $n; $i++) { $bulkWrite->insert([ '_id' => $i, - 'x' => (integer) ($i . $i), + 'x' => (int) ($i . $i), ]); } diff --git a/tests/Operation/UpdateFunctionalTest.php b/tests/Operation/UpdateFunctionalTest.php index 9415960f5..5d1870172 100644 --- a/tests/Operation/UpdateFunctionalTest.php +++ b/tests/Operation/UpdateFunctionalTest.php @@ -326,7 +326,7 @@ private function createFixtures(int $n): void for ($i = 1; $i <= $n; $i++) { $bulkWrite->insert([ '_id' => $i, - 'x' => (integer) ($i . $i), + 'x' => (int) ($i . $i), ]); } diff --git a/tests/SpecTests/ClientSideEncryption/Prose22_RangeExplicitEncryptionTest.php b/tests/SpecTests/ClientSideEncryption/Prose22_RangeExplicitEncryptionTest.php index f030c8b36..fc8da6446 100644 --- a/tests/SpecTests/ClientSideEncryption/Prose22_RangeExplicitEncryptionTest.php +++ b/tests/SpecTests/ClientSideEncryption/Prose22_RangeExplicitEncryptionTest.php @@ -425,7 +425,7 @@ private static function cast(string $type, int $value): mixed { return match ($type) { 'DecimalNoPrecision', 'DecimalPrecision' => new Decimal128((string) $value), - 'DoubleNoPrecision', 'DoublePrecision' => (double) $value, + 'DoubleNoPrecision', 'DoublePrecision' => (float) $value, 'Date' => new UTCDateTime($value), 'Int' => $value, 'Long' => new Int64($value), diff --git a/tools/connect.php b/tools/connect.php index 136ac106e..2d0ed1ff4 100644 --- a/tools/connect.php +++ b/tools/connect.php @@ -2,7 +2,7 @@ function getHosts(string $uri): array { - if (strpos($uri, '://') === false) { + if (! str_contains($uri, '://')) { return [$uri]; } diff --git a/tools/detect-extension.php b/tools/detect-extension.php index 13eabb5c0..1741a4328 100644 --- a/tools/detect-extension.php +++ b/tools/detect-extension.php @@ -5,11 +5,11 @@ function grepIniFile(string $filename, string $extension): int $lines = []; foreach (new SplFileObject($filename) as $i => $line) { - if (strpos($line, 'extension') === false) { + if (! str_contains($line, 'extension')) { continue; } - if (strpos($line, $extension) === false) { + if (! str_contains($line, $extension)) { continue; }