Skip to content

Commit f4815a1

Browse files
authored
Remove usage of deprecated BSON functions (#1361)
* Remove usage of deprecated BSON functions * Fix psalm issues * Refactor test
1 parent de3aaf8 commit f4815a1

20 files changed

+88
-133
lines changed

docs/tutorial/custom-types.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ back to ``LocalDateTime``.
151151
.. code-block:: php
152152

153153
<?php
154-
$bson = MongoDB\BSON\fromPHP(['date' => new LocalDateTime]);
155-
$document = MongoDB\BSON\toPHP($bson);
154+
$bson = MongoDB\BSON\Document::fromPHP(['date' => new LocalDateTime]);
155+
$document = $bson->toPHP();
156156

157157
var_dump($document);
158158
var_dump($document->date->toDateTime());

docs/tutorial/example-data.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ example imports the ``zips.json`` file into a ``test.zips`` collection using the
2222
$bulk = new MongoDB\Driver\BulkWrite;
2323

2424
foreach ($lines as $line) {
25-
$bson = MongoDB\BSON\fromJSON($line);
26-
$document = MongoDB\BSON\toPHP($bson);
25+
$bson = MongoDB\BSON\Document::fromJSON($line);
26+
$document = $bson->toPHP();
2727
$bulk->insert($document);
2828
}
2929

examples/aggregate.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33

44
namespace MongoDB\Examples\Aggregate;
55

6+
use MongoDB\BSON\Document;
67
use MongoDB\Client;
78

89
use function assert;
910
use function getenv;
1011
use function is_object;
11-
use function MongoDB\BSON\fromPHP;
12-
use function MongoDB\BSON\toRelaxedExtendedJSON;
1312
use function printf;
1413
use function random_int;
1514

1615
require __DIR__ . '/../vendor/autoload.php';
1716

1817
function toJSON(object $document): string
1918
{
20-
return toRelaxedExtendedJSON(fromPHP($document));
19+
return Document::fromPHP($document)->toRelaxedExtendedJSON();
2120
}
2221

2322
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');

examples/bulk.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33

44
namespace MongoDB\Examples\Bulk;
55

6+
use MongoDB\BSON\Document;
67
use MongoDB\Client;
78
use MongoDB\Driver\WriteConcern;
89

910
use function assert;
1011
use function getenv;
1112
use function is_object;
12-
use function MongoDB\BSON\fromPHP;
13-
use function MongoDB\BSON\toRelaxedExtendedJSON;
1413
use function printf;
1514

1615
require __DIR__ . '/../vendor/autoload.php';
1716

1817
function toJSON(object $document): string
1918
{
20-
return toRelaxedExtendedJSON(fromPHP($document));
19+
return Document::fromPHP($document)->toRelaxedExtendedJSON();
2120
}
2221

2322
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');

examples/changestream.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33

44
namespace MongoDB\Examples\ChangeStream;
55

6+
use MongoDB\BSON\Document;
67
use MongoDB\Client;
78

89
use function assert;
910
use function getenv;
1011
use function is_object;
11-
use function MongoDB\BSON\fromPHP;
12-
use function MongoDB\BSON\toRelaxedExtendedJSON;
1312
use function printf;
1413
use function time;
1514

1615
require __DIR__ . '/../vendor/autoload.php';
1716

1817
function toJSON(object $document): string
1918
{
20-
return toRelaxedExtendedJSON(fromPHP($document));
19+
return Document::fromPHP($document)->toRelaxedExtendedJSON();
2120
}
2221

2322
// Change streams require a replica set or sharded cluster

examples/command_logger.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace MongoDB\Examples\CommandLogger;
55

6+
use MongoDB\BSON\Document;
67
use MongoDB\Client;
78
use MongoDB\Driver\Monitoring\CommandFailedEvent;
89
use MongoDB\Driver\Monitoring\CommandStartedEvent;
@@ -13,15 +14,13 @@
1314
use function get_class;
1415
use function getenv;
1516
use function is_object;
16-
use function MongoDB\BSON\fromPHP;
17-
use function MongoDB\BSON\toRelaxedExtendedJSON;
1817
use function printf;
1918

2019
require __DIR__ . '/../vendor/autoload.php';
2120

2221
function toJSON(object $document): string
2322
{
24-
return toRelaxedExtendedJSON(fromPHP($document));
23+
return Document::fromPHP($document)->toRelaxedExtendedJSON();
2524
}
2625

2726
class CommandLogger implements CommandSubscriber

examples/sdam_logger.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace MongoDB\Examples;
55

6+
use MongoDB\BSON\Document;
67
use MongoDB\Client;
78
use MongoDB\Driver\Monitoring\SDAMSubscriber;
89
use MongoDB\Driver\Monitoring\ServerChangedEvent;
@@ -17,16 +18,14 @@
1718

1819
use function get_class;
1920
use function getenv;
20-
use function MongoDB\BSON\fromPHP;
21-
use function MongoDB\BSON\toRelaxedExtendedJSON;
2221
use function printf;
2322

2423
require __DIR__ . '/../vendor/autoload.php';
2524

2625
/** @param array|object $document */
2726
function toJSON($document): string
2827
{
29-
return toRelaxedExtendedJSON(fromPHP($document));
28+
return Document::fromPHP($document)->toRelaxedExtendedJSON();
3029
}
3130

3231
class SDAMLogger implements SDAMSubscriber

examples/with_transaction.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@
33

44
namespace MongoDB\Examples\WithTransaction;
55

6+
use MongoDB\BSON\Document;
67
use MongoDB\Client;
78
use MongoDB\Driver\Session;
89

910
use function assert;
1011
use function getenv;
1112
use function is_object;
12-
use function MongoDB\BSON\fromPHP;
13-
use function MongoDB\BSON\toRelaxedExtendedJSON;
1413
use function MongoDB\with_transaction;
1514
use function printf;
1615

1716
require __DIR__ . '/../vendor/autoload.php';
1817

1918
function toJSON(object $document): string
2019
{
21-
return toRelaxedExtendedJSON(fromPHP($document));
20+
return Document::fromPHP($document)->toRelaxedExtendedJSON();
2221
}
2322

2423
// Transactions require a replica set (MongoDB >= 4.0) or sharded cluster (MongoDB >= 4.2)

psalm-baseline.xml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,6 @@
135135
<code>stdClass</code>
136136
</MoreSpecificReturnType>
137137
</file>
138-
<file src="src/Model/BSONIterator.php">
139-
<MixedArgument>
140-
<code>$documentLength</code>
141-
<code>$documentLength</code>
142-
<code><![CDATA[$this->options['typeMap']]]></code>
143-
</MixedArgument>
144-
<MixedAssignment>
145-
<code><![CDATA[$this->position]]></code>
146-
</MixedAssignment>
147-
<MixedOperand>
148-
<code>$documentLength</code>
149-
</MixedOperand>
150-
</file>
151138
<file src="src/Model/ChangeStreamIterator.php">
152139
<MixedArgument>
153140
<code><![CDATA[$reply->cursor->nextBatch]]></code>

src/GridFS/Bucket.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
use function is_string;
5353
use function method_exists;
5454
use function MongoDB\apply_type_map_to_document;
55-
use function MongoDB\BSON\fromPHP;
56-
use function MongoDB\BSON\toJSON;
5755
use function property_exists;
5856
use function sprintf;
5957
use function str_contains;
@@ -705,7 +703,7 @@ public function uploadFromStream(string $filename, $source, array $options = [])
705703
private function createPathForFile(object $file): string
706704
{
707705
if (is_array($file->_id) || (is_object($file->_id) && ! method_exists($file->_id, '__toString'))) {
708-
$id = toJSON(fromPHP(['_id' => $file->_id]));
706+
$id = Document::fromPHP(['_id' => $file->_id])->toRelaxedExtendedJSON();
709707
} else {
710708
$id = (string) $file->_id;
711709
}

src/GridFS/Exception/FileNotFoundException.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717

1818
namespace MongoDB\GridFS\Exception;
1919

20+
use MongoDB\BSON\Document;
2021
use MongoDB\Exception\RuntimeException;
2122

22-
use function MongoDB\BSON\fromPHP;
23-
use function MongoDB\BSON\toJSON;
2423
use function sprintf;
2524

2625
class FileNotFoundException extends RuntimeException
@@ -58,7 +57,7 @@ public static function byFilenameAndRevision(string $filename, int $revision, st
5857
*/
5958
public static function byId($id, string $namespace)
6059
{
61-
$json = toJSON(fromPHP(['_id' => $id]));
60+
$json = Document::fromPHP(['_id' => $id])->toRelaxedExtendedJSON();
6261

6362
return new self(sprintf('File "%s" not found in "%s"', $json, $namespace));
6463
}

src/GridFS/Exception/StreamException.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace MongoDB\GridFS\Exception;
44

5+
use MongoDB\BSON\Document;
56
use MongoDB\Exception\RuntimeException;
67

7-
use function MongoDB\BSON\fromPHP;
8-
use function MongoDB\BSON\toJSON;
98
use function sprintf;
109
use function stream_get_meta_data;
1110

@@ -30,7 +29,7 @@ public static function downloadFromFilenameFailed(string $filename, $source, $de
3029
*/
3130
public static function downloadFromIdFailed($id, $source, $destination): self
3231
{
33-
$idString = toJSON(fromPHP(['_id' => $id]));
32+
$idString = Document::fromPHP(['_id' => $id])->toRelaxedExtendedJSON();
3433
$sourceMetadata = stream_get_meta_data($source);
3534
$destinationMetadata = stream_get_meta_data($destination);
3635

src/Model/BSONIterator.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
namespace MongoDB\Model;
1919

2020
use Iterator;
21+
use MongoDB\BSON\Document;
2122
use MongoDB\Exception\InvalidArgumentException;
2223
use MongoDB\Exception\UnexpectedValueException;
2324
use ReturnTypeWillChange;
2425

26+
use function assert;
2527
use function is_array;
26-
use function MongoDB\BSON\toPHP;
28+
use function is_int;
2729
use function sprintf;
2830
use function strlen;
2931
use function substr;
@@ -49,6 +51,7 @@ class BSONIterator implements Iterator
4951

5052
private int $position = 0;
5153

54+
/** @var array{typeMap: array, ...} */
5255
private array $options;
5356

5457
/**
@@ -142,12 +145,13 @@ private function advance(): void
142145
}
143146

144147
[, $documentLength] = unpack('V', substr($this->buffer, $this->position, self::BSON_SIZE));
148+
assert(is_int($documentLength));
145149

146150
if ($this->bufferLength - $this->position < $documentLength) {
147151
throw new UnexpectedValueException(sprintf('Expected %d bytes; %d remaining', $documentLength, $this->bufferLength - $this->position));
148152
}
149153

150-
$this->current = toPHP(substr($this->buffer, $this->position, $documentLength), $this->options['typeMap']);
154+
$this->current = Document::fromBSON(substr($this->buffer, $this->position, $documentLength))->toPHP($this->options['typeMap']);
151155
$this->position += $documentLength;
152156
}
153157
}

src/functions.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343
use function is_array;
4444
use function is_object;
4545
use function is_string;
46-
use function MongoDB\BSON\fromPHP;
47-
use function MongoDB\BSON\toPHP;
4846
use function str_ends_with;
4947
use function substr;
5048

@@ -115,7 +113,7 @@ function apply_type_map_to_document($document, array $typeMap)
115113
throw InvalidArgumentException::expectedDocumentType('$document', $document);
116114
}
117115

118-
return toPHP(fromPHP($document), $typeMap);
116+
return Document::fromPHP($document)->toPHP($typeMap);
119117
}
120118

121119
/**

0 commit comments

Comments
 (0)