Skip to content

Commit 000f463

Browse files
committed
Fixes for coding standards and static analysis
1 parent 7056c97 commit 000f463

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

examples/changestream.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77

88
use MongoDB\Client;
99

10+
use function assert;
1011
use function fprintf;
1112
use function getenv;
12-
use function hrtime;
13+
use function is_object;
1314
use function MongoDB\BSON\fromPHP;
1415
use function MongoDB\BSON\toRelaxedExtendedJSON;
1516
use function printf;
17+
use function time;
1618

1719
use const STDERR;
1820

@@ -21,7 +23,7 @@ function toJSON(object $document): string
2123
return toRelaxedExtendedJSON(fromPHP($document));
2224
}
2325

24-
$client = new Client(getenv('MONGODB_URI') ?? null);
26+
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');
2527

2628
$collection = $client->test->coll;
2729
$collection->drop();
@@ -38,16 +40,18 @@ function toJSON(object $document): string
3840

3941
$changeStream->rewind();
4042

41-
$startTime = hrtime(true);
43+
$startTime = time();
4244

4345
while (true) {
4446
if ($changeStream->valid()) {
45-
printf("%s\n", toJSON($changeStream->current()));
47+
$event = $changeStream->current();
48+
assert(is_object($event));
49+
printf("%s\n", toJSON($event));
4650
}
4751

4852
$changeStream->next();
4953

50-
if (hrtime(true) - $startTime > 5e+9) {
54+
if (time() - $startTime > 5e+9) {
5155
fprintf(STDERR, "Aborting after 5 seconds...\n");
5256
break;
5357
}

examples/command_logger.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22
declare(strict_types=1);
33

4-
// phpcs:disable Squiz.Classes.ClassFileName.NoMatch
5-
64
namespace MongoDB\Examples;
75

86
require '../vendor/autoload.php';
@@ -13,9 +11,11 @@
1311
use MongoDB\Driver\Monitoring\CommandSubscriber;
1412
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
1513

14+
use function assert;
1615
use function fprintf;
1716
use function get_class;
1817
use function getenv;
18+
use function is_object;
1919
use function MongoDB\BSON\fromPHP;
2020
use function MongoDB\BSON\toRelaxedExtendedJSON;
2121
use function printf;
@@ -27,6 +27,7 @@ function toJSON(object $document): string
2727
return toRelaxedExtendedJSON(fromPHP($document));
2828
}
2929

30+
// phpcs:disable Squiz.Classes.ClassFileName.NoMatch
3031
class CommandLogger implements CommandSubscriber
3132
{
3233
public function commandStarted(CommandStartedEvent $event): void
@@ -56,9 +57,8 @@ public function commandFailed(CommandFailedEvent $event): void
5657
fprintf(STDERR, "\n");
5758
}
5859
}
59-
// phpcs:enable
6060

61-
$client = new Client(getenv('MONGODB_URI') ?? null);
61+
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');
6262

6363
$client->getManager()->addSubscriber(new CommandLogger());
6464

@@ -79,5 +79,6 @@ public function commandFailed(CommandFailedEvent $event): void
7979
$cursor = $collection->find([], ['batchSize' => 2]);
8080

8181
foreach ($cursor as $document) {
82+
assert(is_object($document));
8283
printf("%s\n", toJSON($document));
8384
}

0 commit comments

Comments
 (0)