diff --git a/composer.json b/composer.json index 6ba77e202..285255e48 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,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 8eee0b356..3210f8b42 100644 --- a/src/GridFS/Bucket.php +++ b/src/GridFS/Bucket.php @@ -34,7 +34,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 4e9b1ec72..cdc5d2d8d 100644 --- a/src/GridFS/ReadableStream.php +++ b/src/GridFS/ReadableStream.php @@ -80,7 +80,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); } } @@ -182,7 +182,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/Model/CollectionInfo.php b/src/Model/CollectionInfo.php index 717e19d84..e48a2ed15 100644 --- a/src/Model/CollectionInfo.php +++ b/src/Model/CollectionInfo.php @@ -58,7 +58,7 @@ public function __debugInfo(): array public function getCappedMax(): ?int { /* 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; } /** @@ -69,7 +69,7 @@ public function getCappedMax(): ?int public function getCappedSize(): ?int { /* 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 2a9d01204..4e8fd8c51 100644 --- a/src/Model/DatabaseInfo.php +++ b/src/Model/DatabaseInfo.php @@ -63,7 +63,7 @@ public function getName(): string public function getSizeOnDisk(): int { /* The MongoDB server might return this number as an integer or float */ - return (integer) $this->info['sizeOnDisk']; + return (int) $this->info['sizeOnDisk']; } /** @@ -71,7 +71,7 @@ public function getSizeOnDisk(): int */ public function isEmpty(): bool { - return (boolean) $this->info['empty']; + return (bool) $this->info['empty']; } /** diff --git a/src/Model/IndexInfo.php b/src/Model/IndexInfo.php index ce6e04be5..a06d7a7be 100644 --- a/src/Model/IndexInfo.php +++ b/src/Model/IndexInfo.php @@ -84,7 +84,7 @@ public function getName(): string */ public function getVersion(): int { - return (integer) $this->info['v']; + return (int) $this->info['v']; } /** diff --git a/src/Operation/Count.php b/src/Operation/Count.php index a6b03c57d..ae40c790f 100644 --- a/src/Operation/Count.php +++ b/src/Operation/Count.php @@ -143,7 +143,7 @@ public function execute(Server $server): int 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 7d00e8bed..54591a5af 100644 --- a/src/Operation/CountDocuments.php +++ b/src/Operation/CountDocuments.php @@ -124,7 +124,7 @@ public function execute(Server $server): int 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 d33760c81..109826e2a 100644 --- a/src/functions.php +++ b/src/functions.php @@ -409,8 +409,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 a4ee7b148..a1799f7f9 100644 --- a/tests/Collection/CollectionFunctionalTest.php +++ b/tests/Collection/CollectionFunctionalTest.php @@ -819,7 +819,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 a597024e2..9fce1c8ac 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 33b000478..2aceafa1b 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 a4ec42f44..772479deb 100644 --- a/tests/Operation/ExplainFunctionalTest.php +++ b/tests/Operation/ExplainFunctionalTest.php @@ -380,7 +380,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 55c71c7e2..b9424975b 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; }