Skip to content

Commit

Permalink
Merge branch 'v1.17'
Browse files Browse the repository at this point in the history
* v1.17:
  PHPLIB-1315: Fix psalm errors (mongodb#1198)
  Fix wrong data type in codec tutorial (mongodb#1194)
  • Loading branch information
alcaeus committed Nov 23, 2023
2 parents b455ab7 + 3fb7ebb commit e03ac44
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 61 deletions.
2 changes: 1 addition & 1 deletion docs/examples/codecs/handling-data-types/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ final class Person
{
public function __construct(
public string $name,
public readonly DateTime $createdAt = new DateTime(),
public readonly DateTimeImmutable $createdAt = new DateTimeImmutable(),
public readonly ObjectId $id = new ObjectId(),
) {
}
Expand Down
7 changes: 6 additions & 1 deletion docs/tutorial/codecs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ BSON date and accompanying timezone string. Those same embedded documents can th
.. literalinclude:: /examples/codecs/handling-data-types/DateTimeCodec.php
:language: php

This codec can now be leveraged by other codecs handle date fields.
.. note::
When writing a codec, you should be as lenient as possible when it comes to handling data. In this case, the codec
handles any ``DateTimeInterface`` when encoding to BSON, as a ``UTCDateTime`` instance can be created from any such
object. When decoding data from BSON, it will always decode to a ``DateTimeImmutable`` instance.

This codec can now be leveraged by other codecs that handle date fields.

First, we add a ``createdAt`` field to the ``Person`` class:

Expand Down
33 changes: 8 additions & 25 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.13.1@086b94371304750d1c673315321a55d15fc59015">
<files psalm-version="5.16.0@2897ba636551a8cb61601cc26f6ccfbba6c36591">
<file src="docs/examples/encryption/csfle-explicit_encryption.php">
<MixedArgument>
<code><![CDATA[$clientEncryption->decrypt($document->encryptedField)]]></code>
Expand Down Expand Up @@ -148,14 +148,6 @@
<code><![CDATA[$this->current()]]></code>
</PossiblyNullArgument>
</file>
<file src="src/Model/CollectionInfo.php">
<MixedArgument>
<code>$key</code>
</MixedArgument>
<MixedArrayOffset>
<code><![CDATA[$this->info[$key]]]></code>
</MixedArrayOffset>
</file>
<file src="src/Model/CollectionInfoCommandIterator.php">
<MixedArrayAssignment>
<code><![CDATA[$info['idIndex']['ns']]]></code>
Expand All @@ -164,14 +156,6 @@
<code><![CDATA[$info['name']]]></code>
</MixedOperand>
</file>
<file src="src/Model/DatabaseInfo.php">
<MixedArgument>
<code>$key</code>
</MixedArgument>
<MixedArrayOffset>
<code><![CDATA[$this->info[$key]]]></code>
</MixedArrayOffset>
</file>
<file src="src/Model/DatabaseInfoLegacyIterator.php">
<MixedArgument>
<code><![CDATA[current($this->databases)]]></code>
Expand All @@ -181,14 +165,6 @@
<code><![CDATA[key($this->databases)]]></code>
</MixedReturnTypeCoercion>
</file>
<file src="src/Model/IndexInfo.php">
<MixedArgument>
<code>$key</code>
</MixedArgument>
<MixedArrayOffset>
<code><![CDATA[$this->info[$key]]]></code>
</MixedArrayOffset>
</file>
<file src="src/Model/IndexInput.php">
<LessSpecificReturnStatement>
<code><![CDATA[(object) $this->index]]></code>
Expand Down Expand Up @@ -663,6 +639,13 @@
<code><![CDATA[$this->changeStreamOptions['startAfter']]]></code>
</MixedReturnStatement>
</file>
<file src="src/PsrLogAdapter.php">
<MethodSignatureMismatch>
<code>$domain</code>
<code>$level</code>
<code>$message</code>
</MethodSignatureMismatch>
</file>
<file src="src/UpdateResult.php">
<MixedAssignment>
<code>$id</code>
Expand Down
22 changes: 12 additions & 10 deletions src/Model/CollectionInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,39 +147,41 @@ public function isCapped()
* Check whether a field exists in the collection information.
*
* @see https://php.net/arrayaccess.offsetexists
* @param mixed $key
* @param mixed $offset
* @return boolean
* @psalm-param array-key $offset
*/
#[ReturnTypeWillChange]
public function offsetExists($key)
public function offsetExists($offset)
{
return array_key_exists($key, $this->info);
return array_key_exists($offset, $this->info);
}

/**
* Return the field's value from the collection information.
*
* @see https://php.net/arrayaccess.offsetget
* @param mixed $key
* @param mixed $offset
* @return mixed
* @psalm-param array-key $offset
*/
#[ReturnTypeWillChange]
public function offsetGet($key)
public function offsetGet($offset)
{
return $this->info[$key];
return $this->info[$offset];
}

/**
* Not supported.
*
* @see https://php.net/arrayaccess.offsetset
* @param mixed $key
* @param mixed $offset
* @param mixed $value
* @throws BadMethodCallException
* @return void
*/
#[ReturnTypeWillChange]
public function offsetSet($key, $value)
public function offsetSet($offset, $value)
{
throw BadMethodCallException::classIsImmutable(self::class);
}
Expand All @@ -188,12 +190,12 @@ public function offsetSet($key, $value)
* Not supported.
*
* @see https://php.net/arrayaccess.offsetunset
* @param mixed $key
* @param mixed $offset
* @throws BadMethodCallException
* @return void
*/
#[ReturnTypeWillChange]
public function offsetUnset($key)
public function offsetUnset($offset)
{
throw BadMethodCallException::classIsImmutable(self::class);
}
Expand Down
22 changes: 12 additions & 10 deletions src/Model/DatabaseInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,39 +89,41 @@ public function isEmpty()
* Check whether a field exists in the database information.
*
* @see https://php.net/arrayaccess.offsetexists
* @param mixed $key
* @param mixed $offset
* @return boolean
* @psalm-param array-key $offset
*/
#[ReturnTypeWillChange]
public function offsetExists($key)
public function offsetExists($offset)
{
return array_key_exists($key, $this->info);
return array_key_exists($offset, $this->info);
}

/**
* Return the field's value from the database information.
*
* @see https://php.net/arrayaccess.offsetget
* @param mixed $key
* @param mixed $offset
* @return mixed
* @psalm-param array-key $offset
*/
#[ReturnTypeWillChange]
public function offsetGet($key)
public function offsetGet($offset)
{
return $this->info[$key];
return $this->info[$offset];
}

/**
* Not supported.
*
* @see https://php.net/arrayaccess.offsetset
* @param mixed $key
* @param mixed $offset
* @param mixed $value
* @throws BadMethodCallException
* @return void
*/
#[ReturnTypeWillChange]
public function offsetSet($key, $value)
public function offsetSet($offset, $value)
{
throw BadMethodCallException::classIsImmutable(self::class);
}
Expand All @@ -130,12 +132,12 @@ public function offsetSet($key, $value)
* Not supported.
*
* @see https://php.net/arrayaccess.offsetunset
* @param mixed $key
* @param mixed $offset
* @throws BadMethodCallException
* @return void
*/
#[ReturnTypeWillChange]
public function offsetUnset($key)
public function offsetUnset($offset)
{
throw BadMethodCallException::classIsImmutable(self::class);
}
Expand Down
22 changes: 12 additions & 10 deletions src/Model/IndexInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,14 @@ public function isUnique()
* Check whether a field exists in the index information.
*
* @see https://php.net/arrayaccess.offsetexists
* @param mixed $key
* @param mixed $offset
* @return boolean
* @psalm-param array-key $offset
*/
#[ReturnTypeWillChange]
public function offsetExists($key)
public function offsetExists($offset)
{
return array_key_exists($key, $this->info);
return array_key_exists($offset, $this->info);
}

/**
Expand All @@ -201,26 +202,27 @@ public function offsetExists($key)
*
* @see https://php.net/arrayaccess.offsetget
* @see https://github.com/mongodb/specifications/blob/master/source/enumerate-indexes.rst#getting-full-index-information
* @param mixed $key
* @param mixed $offset
* @return mixed
* @psalm-param array-key $offset
*/
#[ReturnTypeWillChange]
public function offsetGet($key)
public function offsetGet($offset)
{
return $this->info[$key];
return $this->info[$offset];
}

/**
* Not supported.
*
* @see https://php.net/arrayaccess.offsetset
* @param mixed $key
* @param mixed $offset
* @param mixed $value
* @throws BadMethodCallException
* @return void
*/
#[ReturnTypeWillChange]
public function offsetSet($key, $value)
public function offsetSet($offset, $value)
{
throw BadMethodCallException::classIsImmutable(self::class);
}
Expand All @@ -229,12 +231,12 @@ public function offsetSet($key, $value)
* Not supported.
*
* @see https://php.net/arrayaccess.offsetunset
* @param mixed $key
* @param mixed $offset
* @throws BadMethodCallException
* @return void
*/
#[ReturnTypeWillChange]
public function offsetUnset($key)
public function offsetUnset($offset)
{
throw BadMethodCallException::classIsImmutable(self::class);
}
Expand Down
8 changes: 4 additions & 4 deletions src/PsrLogAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,21 @@ public static function addLogger(LoggerInterface $logger): void
*
* @see LogSubscriber::log()
*/
public function log(int $mongocLevel, string $domain, string $message): void
public function log(int $level, string $domain, string $message): void
{
if (! isset(self::MONGOC_TO_PSR[$mongocLevel])) {
if (! isset(self::MONGOC_TO_PSR[$level])) {
throw new UnexpectedValueException(sprintf(
'Expected level to be >= %d and <= %d, %d given for domain "%s" and message: %s',
LogSubscriber::LEVEL_ERROR,
LogSubscriber::LEVEL_DEBUG,
$mongocLevel,
$level,
$domain,
$message,
));
}

$instance = self::getInstance();
$psrLevel = self::MONGOC_TO_PSR[$mongocLevel];
$psrLevel = self::MONGOC_TO_PSR[$level];
$context = ['domain' => $domain];

foreach ($instance->loggers as $logger) {
Expand Down

0 comments on commit e03ac44

Please sign in to comment.