Skip to content

Commit 9e05a40

Browse files
committed
Harden checks for writeConcern options
* Throw an exception when wTimeout or journal are passed without a w option * Cast w to int if it's numeric to ensure correct handling of writeConcern values in the driver
1 parent 0204d40 commit 9e05a40

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

lib/Doctrine/ODM/MongoDB/Tools/Console/Command/Schema/AbstractCommand.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
use Doctrine\ODM\MongoDB\SchemaManager;
1010
use MongoDB\Driver\WriteConcern;
1111
use Symfony\Component\Console\Command\Command;
12+
use Symfony\Component\Console\Exception\InvalidOptionException;
1213
use Symfony\Component\Console\Input\InputInterface;
1314
use Symfony\Component\Console\Input\InputOption;
15+
use function is_numeric;
1416
use function is_string;
1517

1618
abstract class AbstractCommand extends Command
@@ -26,8 +28,8 @@ protected function configure()
2628
$this
2729
->addOption('maxTimeMs', null, InputOption::VALUE_REQUIRED, 'An optional maxTimeMs that will be used for all schema operations.')
2830
->addOption('w', null, InputOption::VALUE_REQUIRED, 'An optional w option for the write concern that will be used for all schema operations.')
29-
->addOption('wTimeout', null, InputOption::VALUE_REQUIRED, 'An optional wTimeout option for the write concern that will be used for all schema operations. This option will be ignored if no w option was specified.')
30-
->addOption('journal', null, InputOption::VALUE_REQUIRED, 'An optional journal option for the write concern that will be used for all schema operations. This option will be ignored if no w option was specified.');
31+
->addOption('wTimeout', null, InputOption::VALUE_REQUIRED, 'An optional wTimeout option for the write concern that will be used for all schema operations. Using this option without a w option will cause an exception to be thrown.')
32+
->addOption('journal', null, InputOption::VALUE_REQUIRED, 'An optional journal option for the write concern that will be used for all schema operations. Using this option without a w option will cause an exception to be thrown.');
3133
}
3234

3335
abstract protected function processDocumentCollection(SchemaManager $sm, string $document, ?int $maxTimeMs, ?WriteConcern $writeConcern);
@@ -75,14 +77,23 @@ protected function getMaxTimeMsFromInput(InputInterface $input) : ?int
7577

7678
protected function getWriteConcernFromInput(InputInterface $input) : ?WriteConcern
7779
{
78-
$w = $input->getOption('w');
80+
$w = $input->getOption('w');
81+
$wTimeout = $input->getOption('wTimeout');
82+
$journal = $input->getOption('journal');
83+
7984
if (! is_string($w)) {
85+
if ($wTimeout !== null || $journal !== null) {
86+
throw new InvalidOptionException('The "wTimeout" or "journal" options can only be used when passing a "w" option.');
87+
}
88+
8089
return null;
8190
}
8291

83-
$wTimeout = $input->getOption('wTimeout');
92+
if (is_numeric($w)) {
93+
$w = (int) $w;
94+
}
95+
8496
$wTimeout = is_string($wTimeout) ? (int) $wTimeout : 0;
85-
$journal = $input->getOption('journal');
8697
$journal = is_string($journal) ? (bool) $journal : false;
8798

8899
return new WriteConcern($w, $wTimeout, $journal);

phpstan.neon.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ parameters:
66
ignoreErrors:
77
# See https://github.com/mongodb/mongo-php-driver/issues/940
88
- '#^PHPDoc tag @throws with type MongoDB\\Driver\\Exception\\Exception is not subtype of Throwable$#'
9+
# See https://github.com/phpstan/phpstan/pull/1730
10+
- '#^Parameter \#1 $wstring of class MongoDB\\Driver\\WriteConcern constructor expects string, int|string given\.$#'

0 commit comments

Comments
 (0)