Skip to content

Commit

Permalink
Merge v1.x into v2.x (#1397)
Browse files Browse the repository at this point in the history
  • Loading branch information
mongodb-php-bot authored Sep 13, 2024
2 parents 087680f + 4c2b2f5 commit d1f7cce
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 232 deletions.
9 changes: 5 additions & 4 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassLike\RemoveAnnotationRector;
use Rector\Php70\Rector\StmtsAwareInterface\IfIssetToCoalescingRector;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
use Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
Expand All @@ -17,13 +17,14 @@
// Modernize code
$rectorConfig->sets([LevelSetList::UP_TO_PHP_74]);

$rectorConfig->rule(ChangeSwitchToMatchRector::class);

// phpcs:disable Squiz.Arrays.ArrayDeclaration.KeySpecified
$rectorConfig->skip([
// Do not use ternaries extensively
IfIssetToCoalescingRector::class,
// Not necessary in documentation examples
JsonThrowOnErrorRector::class => [
__DIR__ . '/tests/DocumentationExamplesTest.php',
ChangeSwitchToMatchRector::class => [
__DIR__ . '/tests/SpecTests/Operation.php',
],
]);
// phpcs:enable
Expand Down
33 changes: 11 additions & 22 deletions src/Builder/Encoder/OperatorEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,14 @@ public function encode(mixed $value): stdClass
throw UnsupportedValueException::invalidEncodableValue($value);
}

switch ($value::ENCODE) {
case Encode::Single:
return $this->encodeAsSingle($value);

case Encode::Array:
return $this->encodeAsArray($value);

case Encode::Object:
case Encode::FlatObject:
return $this->encodeAsObject($value);

case Encode::DollarObject:
return $this->encodeAsDollarObject($value);

case Encode::Group:
assert($value instanceof GroupStage);

return $this->encodeAsGroup($value);
}

throw new LogicException(sprintf('Class "%s" does not have a valid ENCODE constant.', $value::class));
return match ($value::ENCODE) {
Encode::Single => $this->encodeAsSingle($value),
Encode::Array => $this->encodeAsArray($value),
Encode::Object, Encode::FlatObject => $this->encodeAsObject($value),
Encode::DollarObject => $this->encodeAsDollarObject($value),
Encode::Group => $this->encodeAsGroup($value),
default => throw new LogicException(sprintf('Class "%s" does not have a valid ENCODE constant.', $value::class)),
};
}

/**
Expand Down Expand Up @@ -111,8 +98,10 @@ private function encodeAsDollarObject(OperatorInterface $value): stdClass
/**
* $group stage have a specific encoding because the _id argument is required and others are variadic
*/
private function encodeAsGroup(GroupStage $value): stdClass
private function encodeAsGroup(OperatorInterface $value): stdClass
{
assert($value instanceof GroupStage);

$result = new stdClass();
$result->_id = $this->recursiveEncode($value->_id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,26 +451,13 @@ private function assertMultipleDocumentsMatch(array $expectedDocuments, Iterator

private static function getCastCallableForType(string $type): callable
{
switch ($type) {
case 'DecimalNoPrecision':
case 'DecimalPrecision':
return fn (int $value) => new Decimal128((string) $value);

case 'DoubleNoPrecision':
case 'DoublePrecision':
return fn (int $value) => (double) $value;

case 'Date':
return fn (int $value) => new UTCDateTime($value);

case 'Int':
return fn (int $value) => $value;

case 'Long':
return fn (int $value) => new Int64($value);

default:
throw new LogicException('Unsupported type: ' . $type);
}
return match ($type) {
'DecimalNoPrecision', 'DecimalPrecision' => fn (int $value) => new Decimal128((string) $value),
'DoubleNoPrecision', 'DoublePrecision' => fn (int $value) => (double) $value,
'Date' => fn (int $value) => new UTCDateTime($value),
'Int' => fn (int $value) => $value,
'Long' => fn (int $value) => new Int64($value),
default => throw new LogicException('Unsupported type: ' . $type),
};
}
}
28 changes: 9 additions & 19 deletions tests/SpecTests/CommandExpectations.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,15 @@ class CommandExpectations implements CommandSubscriber
private function __construct(private Client $observedClient, array $events)
{
foreach ($events as $event) {
switch (key((array) $event)) {
case 'command_failed_event':
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$this->expectedEvents[] = [$event->command_failed_event, CommandFailedEvent::class];
break;

case 'command_started_event':
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$this->expectedEvents[] = [$event->command_started_event, CommandStartedEvent::class];
break;

case 'command_succeeded_event':
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$this->expectedEvents[] = [$event->command_succeeded_event, CommandSucceededEvent::class];
break;

default:
throw new LogicException('Unsupported event type: ' . key($event));
}
$this->expectedEvents[] = match (key((array) $event)) {
// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
'command_failed_event' => [$event->command_failed_event, CommandFailedEvent::class],
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
'command_started_event' => [$event->command_started_event, CommandStartedEvent::class],
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
'command_succeeded_event' => [$event->command_succeeded_event, CommandSucceededEvent::class],
default => throw new LogicException('Unsupported event type: ' . key($event)),
};
}
}

Expand Down
34 changes: 10 additions & 24 deletions tests/SpecTests/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,18 +358,11 @@ public function replaceArgumentSessionPlaceholder(array &$args): void
return;
}

switch ($args['session']) {
case 'session0':
$args['session'] = $this->session0;
break;

case 'session1':
$args['session'] = $this->session1;
break;

default:
throw new LogicException('Unsupported session placeholder: ' . $args['session']);
}
$args['session'] = match ($args['session']) {
'session0' => $this->session0,
'session1' => $this->session1,
default => throw new LogicException('Unsupported session placeholder: ' . $args['session']),
};
}

/**
Expand All @@ -386,18 +379,11 @@ public function replaceCommandSessionPlaceholder(stdClass $command): void
return;
}

switch ($command->lsid) {
case 'session0':
$command->lsid = $this->session0Lsid;
break;

case 'session1':
$command->lsid = $this->session1Lsid;
break;

default:
throw new LogicException('Unsupported session placeholder: ' . $command->lsid);
}
$command->lsid = match ($command->lsid) {
'session0' => $this->session0Lsid,
'session1' => $this->session1Lsid,
default => throw new LogicException('Unsupported session placeholder: ' . $command->lsid),
};
}

public function selectCollection($databaseName, $collectionName, array $collectionOptions = [], array $databaseOptions = [])
Expand Down
35 changes: 11 additions & 24 deletions tests/SpecTests/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,11 @@ protected function assertOutcomeCollectionData(array $expectedDocuments, int $re
$this->assertNotNull($expectedDocument);
$this->assertNotNull($actualDocument);

switch ($resultExpectation) {
case ResultExpectation::ASSERT_SAME_DOCUMENT:
$this->assertSameDocument($expectedDocument, $actualDocument);
break;

case ResultExpectation::ASSERT_DOCUMENTS_MATCH:
$this->assertDocumentsMatch($expectedDocument, $actualDocument);
break;

default:
$this->fail(sprintf('Invalid result expectation "%d" for %s', $resultExpectation, __METHOD__));
}
match ($resultExpectation) {
ResultExpectation::ASSERT_SAME_DOCUMENT => $this->assertSameDocument($expectedDocument, $actualDocument),
ResultExpectation::ASSERT_DOCUMENTS_MATCH => $this->assertDocumentsMatch($expectedDocument, $actualDocument),
default => $this->fail(sprintf('Invalid result expectation "%d" for %s', $resultExpectation, __METHOD__)),
};
}
}

Expand Down Expand Up @@ -284,18 +277,12 @@ private function isServerlessRequirementSatisfied(?string $serverlessMode): bool
return true;
}

switch ($serverlessMode) {
case self::SERVERLESS_ALLOW:
return true;

case self::SERVERLESS_FORBID:
return ! static::isServerless();

case self::SERVERLESS_REQUIRE:
return static::isServerless();
}

throw new UnexpectedValueException(sprintf('Invalid serverless requirement "%s" found.', $serverlessMode));
return match ($serverlessMode) {
self::SERVERLESS_ALLOW => true,
self::SERVERLESS_FORBID => ! static::isServerless(),
self::SERVERLESS_REQUIRE => static::isServerless(),
default => throw new UnexpectedValueException(sprintf('Invalid serverless requirement "%s" found.', $serverlessMode)),
};
}

/**
Expand Down
97 changes: 26 additions & 71 deletions tests/UnifiedSpecTests/Constraint/IsBsonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,77 +90,32 @@ public static function anyOf(string ...$types): Constraint

private function doMatches($other): bool
{
switch ($this->type) {
case 'double':
return is_float($other);

case 'string':
return is_string($other);

case 'object':
return self::isObject($other);

case 'array':
return self::isArray($other);

case 'binData':
return $other instanceof BinaryInterface;

case 'undefined':
return $other instanceof Undefined;

case 'objectId':
return $other instanceof ObjectIdInterface;

case 'bool':
return is_bool($other);

case 'date':
return $other instanceof UTCDateTimeInterface;

case 'null':
return $other === null;

case 'regex':
return $other instanceof RegexInterface;

case 'dbPointer':
return $other instanceof DBPointer;

case 'javascript':
return $other instanceof JavascriptInterface && $other->getScope() === null;

case 'symbol':
return $other instanceof Symbol;

case 'javascriptWithScope':
return $other instanceof JavascriptInterface && $other->getScope() !== null;

case 'int':
return is_int($other);

case 'timestamp':
return $other instanceof TimestampInterface;

case 'long':
return is_int($other) || $other instanceof Int64;

case 'decimal':
return $other instanceof Decimal128Interface;

case 'minKey':
return $other instanceof MinKeyInterface;

case 'maxKey':
return $other instanceof MaxKeyInterface;

case 'number':
return is_int($other) || $other instanceof Int64 || is_float($other) || $other instanceof Decimal128Interface;

default:
// This should already have been caught in the constructor
throw new LogicException('Unsupported type: ' . $this->type);
}
return match ($this->type) {
'double' => is_float($other),
'string' => is_string($other),
'object' => self::isObject($other),
'array' => self::isArray($other),
'binData' => $other instanceof BinaryInterface,
'undefined' => $other instanceof Undefined,
'objectId' => $other instanceof ObjectIdInterface,
'bool' => is_bool($other),
'date' => $other instanceof UTCDateTimeInterface,
'null' => $other === null,
'regex' => $other instanceof RegexInterface,
'dbPointer' => $other instanceof DBPointer,
'javascript' => $other instanceof JavascriptInterface && $other->getScope() === null,
'symbol' => $other instanceof Symbol,
'javascriptWithScope' => $other instanceof JavascriptInterface && $other->getScope() !== null,
'int' => is_int($other),
'timestamp' => $other instanceof TimestampInterface,
'long' => is_int($other) || $other instanceof Int64,
'decimal' => $other instanceof Decimal128Interface,
'minKey' => $other instanceof MinKeyInterface,
'maxKey' => $other instanceof MaxKeyInterface,
'number' => is_int($other) || $other instanceof Int64 || is_float($other) || $other instanceof Decimal128Interface,
// This should already have been caught in the constructor
default => throw new LogicException('Unsupported type: ' . $this->type),
};
}

private function doToString(): string
Expand Down
37 changes: 9 additions & 28 deletions tests/UnifiedSpecTests/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,34 +95,15 @@ public function createEntities(array $entities): void
$id = $def->id ?? null;
assertIsString($id);

switch ($type) {
case 'client':
$this->createClient($id, $def);
break;

case 'clientEncryption':
$this->createClientEncryption($id, $def);
break;

case 'database':
$this->createDatabase($id, $def);
break;

case 'collection':
$this->createCollection($id, $def);
break;

case 'session':
$this->createSession($id, $def);
break;

case 'bucket':
$this->createBucket($id, $def);
break;

default:
throw new LogicException('Unsupported entity type: ' . $type);
}
match ($type) {
'client' => $this->createClient($id, $def),
'clientEncryption' => $this->createClientEncryption($id, $def),
'database' => $this->createDatabase($id, $def),
'collection' => $this->createCollection($id, $def),
'session' => $this->createSession($id, $def),
'bucket' => $this->createBucket($id, $def),
default => throw new LogicException('Unsupported entity type: ' . $type),
};
}
}

Expand Down
Loading

0 comments on commit d1f7cce

Please sign in to comment.