Skip to content

Fix PHP 8.5 deprecations of (integer), (long) and (boolean) casts #1758

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 2 commits into from
Aug 20, 2025
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*"
},
Expand Down
49 changes: 27 additions & 22 deletions rector.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
<?php

use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\Cast\Double;
use PhpParser\Node\Expr\Cast\Int_;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassLike\RemoveAnnotationRector;
use Rector\Php70\Rector\StmtsAwareInterface\IfIssetToCoalescingRector;
use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
use Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector;
use Rector\PHPUnit\PHPUnit100\Rector\Class_\StaticDataProviderClassMethodRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Renaming\Rector\Cast\RenameCastRector;
use Rector\Renaming\ValueObject\RenameCast;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->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)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

php74: true could be dropped in order to automatically upgrade the code to the PHP version defined in composer.json, 8.1 in this case. Doing this changes over 250 files.

->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);
1 change: 0 additions & 1 deletion src/GridFS/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/GridFS/ReadableStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/MapReduceResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 : [];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Model/CollectionInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Model/DatabaseInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

/**
Expand All @@ -79,7 +79,7 @@ public function getSizeOnDisk()
*/
public function isEmpty()
{
return (boolean) $this->info['empty'];
return (bool) $this->info['empty'];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Model/IndexInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function getNamespace()
*/
public function getVersion()
{
return (integer) $this->info['v'];
return (int) $this->info['v'];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/CountDocuments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Collection/CollectionFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Operation/BulkWriteFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Operation/DeleteFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Operation/ExplainFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Operation/UpdateFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion tools/connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

function getHosts(string $uri): array
{
if (strpos($uri, '://') === false) {
if (! str_contains($uri, '://')) {
return [$uri];
}

Expand Down
4 changes: 2 additions & 2 deletions tools/detect-extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down