Skip to content

Commit e03ac44

Browse files
committed
Merge branch 'v1.17'
* v1.17: PHPLIB-1315: Fix psalm errors (#1198) Fix wrong data type in codec tutorial (#1194)
2 parents b455ab7 + 3fb7ebb commit e03ac44

File tree

7 files changed

+55
-61
lines changed

7 files changed

+55
-61
lines changed

docs/examples/codecs/handling-data-types/Person.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class Person
66
{
77
public function __construct(
88
public string $name,
9-
public readonly DateTime $createdAt = new DateTime(),
9+
public readonly DateTimeImmutable $createdAt = new DateTimeImmutable(),
1010
public readonly ObjectId $id = new ObjectId(),
1111
) {
1212
}

docs/tutorial/codecs.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ BSON date and accompanying timezone string. Those same embedded documents can th
7474
.. literalinclude:: /examples/codecs/handling-data-types/DateTimeCodec.php
7575
:language: php
7676

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

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

psalm-baseline.xml

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="5.13.1@086b94371304750d1c673315321a55d15fc59015">
2+
<files psalm-version="5.16.0@2897ba636551a8cb61601cc26f6ccfbba6c36591">
33
<file src="docs/examples/encryption/csfle-explicit_encryption.php">
44
<MixedArgument>
55
<code><![CDATA[$clientEncryption->decrypt($document->encryptedField)]]></code>
@@ -148,14 +148,6 @@
148148
<code><![CDATA[$this->current()]]></code>
149149
</PossiblyNullArgument>
150150
</file>
151-
<file src="src/Model/CollectionInfo.php">
152-
<MixedArgument>
153-
<code>$key</code>
154-
</MixedArgument>
155-
<MixedArrayOffset>
156-
<code><![CDATA[$this->info[$key]]]></code>
157-
</MixedArrayOffset>
158-
</file>
159151
<file src="src/Model/CollectionInfoCommandIterator.php">
160152
<MixedArrayAssignment>
161153
<code><![CDATA[$info['idIndex']['ns']]]></code>
@@ -164,14 +156,6 @@
164156
<code><![CDATA[$info['name']]]></code>
165157
</MixedOperand>
166158
</file>
167-
<file src="src/Model/DatabaseInfo.php">
168-
<MixedArgument>
169-
<code>$key</code>
170-
</MixedArgument>
171-
<MixedArrayOffset>
172-
<code><![CDATA[$this->info[$key]]]></code>
173-
</MixedArrayOffset>
174-
</file>
175159
<file src="src/Model/DatabaseInfoLegacyIterator.php">
176160
<MixedArgument>
177161
<code><![CDATA[current($this->databases)]]></code>
@@ -181,14 +165,6 @@
181165
<code><![CDATA[key($this->databases)]]></code>
182166
</MixedReturnTypeCoercion>
183167
</file>
184-
<file src="src/Model/IndexInfo.php">
185-
<MixedArgument>
186-
<code>$key</code>
187-
</MixedArgument>
188-
<MixedArrayOffset>
189-
<code><![CDATA[$this->info[$key]]]></code>
190-
</MixedArrayOffset>
191-
</file>
192168
<file src="src/Model/IndexInput.php">
193169
<LessSpecificReturnStatement>
194170
<code><![CDATA[(object) $this->index]]></code>
@@ -663,6 +639,13 @@
663639
<code><![CDATA[$this->changeStreamOptions['startAfter']]]></code>
664640
</MixedReturnStatement>
665641
</file>
642+
<file src="src/PsrLogAdapter.php">
643+
<MethodSignatureMismatch>
644+
<code>$domain</code>
645+
<code>$level</code>
646+
<code>$message</code>
647+
</MethodSignatureMismatch>
648+
</file>
666649
<file src="src/UpdateResult.php">
667650
<MixedAssignment>
668651
<code>$id</code>

src/Model/CollectionInfo.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,39 +147,41 @@ public function isCapped()
147147
* Check whether a field exists in the collection information.
148148
*
149149
* @see https://php.net/arrayaccess.offsetexists
150-
* @param mixed $key
150+
* @param mixed $offset
151151
* @return boolean
152+
* @psalm-param array-key $offset
152153
*/
153154
#[ReturnTypeWillChange]
154-
public function offsetExists($key)
155+
public function offsetExists($offset)
155156
{
156-
return array_key_exists($key, $this->info);
157+
return array_key_exists($offset, $this->info);
157158
}
158159

159160
/**
160161
* Return the field's value from the collection information.
161162
*
162163
* @see https://php.net/arrayaccess.offsetget
163-
* @param mixed $key
164+
* @param mixed $offset
164165
* @return mixed
166+
* @psalm-param array-key $offset
165167
*/
166168
#[ReturnTypeWillChange]
167-
public function offsetGet($key)
169+
public function offsetGet($offset)
168170
{
169-
return $this->info[$key];
171+
return $this->info[$offset];
170172
}
171173

172174
/**
173175
* Not supported.
174176
*
175177
* @see https://php.net/arrayaccess.offsetset
176-
* @param mixed $key
178+
* @param mixed $offset
177179
* @param mixed $value
178180
* @throws BadMethodCallException
179181
* @return void
180182
*/
181183
#[ReturnTypeWillChange]
182-
public function offsetSet($key, $value)
184+
public function offsetSet($offset, $value)
183185
{
184186
throw BadMethodCallException::classIsImmutable(self::class);
185187
}
@@ -188,12 +190,12 @@ public function offsetSet($key, $value)
188190
* Not supported.
189191
*
190192
* @see https://php.net/arrayaccess.offsetunset
191-
* @param mixed $key
193+
* @param mixed $offset
192194
* @throws BadMethodCallException
193195
* @return void
194196
*/
195197
#[ReturnTypeWillChange]
196-
public function offsetUnset($key)
198+
public function offsetUnset($offset)
197199
{
198200
throw BadMethodCallException::classIsImmutable(self::class);
199201
}

src/Model/DatabaseInfo.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,39 +89,41 @@ public function isEmpty()
8989
* Check whether a field exists in the database information.
9090
*
9191
* @see https://php.net/arrayaccess.offsetexists
92-
* @param mixed $key
92+
* @param mixed $offset
9393
* @return boolean
94+
* @psalm-param array-key $offset
9495
*/
9596
#[ReturnTypeWillChange]
96-
public function offsetExists($key)
97+
public function offsetExists($offset)
9798
{
98-
return array_key_exists($key, $this->info);
99+
return array_key_exists($offset, $this->info);
99100
}
100101

101102
/**
102103
* Return the field's value from the database information.
103104
*
104105
* @see https://php.net/arrayaccess.offsetget
105-
* @param mixed $key
106+
* @param mixed $offset
106107
* @return mixed
108+
* @psalm-param array-key $offset
107109
*/
108110
#[ReturnTypeWillChange]
109-
public function offsetGet($key)
111+
public function offsetGet($offset)
110112
{
111-
return $this->info[$key];
113+
return $this->info[$offset];
112114
}
113115

114116
/**
115117
* Not supported.
116118
*
117119
* @see https://php.net/arrayaccess.offsetset
118-
* @param mixed $key
120+
* @param mixed $offset
119121
* @param mixed $value
120122
* @throws BadMethodCallException
121123
* @return void
122124
*/
123125
#[ReturnTypeWillChange]
124-
public function offsetSet($key, $value)
126+
public function offsetSet($offset, $value)
125127
{
126128
throw BadMethodCallException::classIsImmutable(self::class);
127129
}
@@ -130,12 +132,12 @@ public function offsetSet($key, $value)
130132
* Not supported.
131133
*
132134
* @see https://php.net/arrayaccess.offsetunset
133-
* @param mixed $key
135+
* @param mixed $offset
134136
* @throws BadMethodCallException
135137
* @return void
136138
*/
137139
#[ReturnTypeWillChange]
138-
public function offsetUnset($key)
140+
public function offsetUnset($offset)
139141
{
140142
throw BadMethodCallException::classIsImmutable(self::class);
141143
}

src/Model/IndexInfo.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,14 @@ public function isUnique()
183183
* Check whether a field exists in the index information.
184184
*
185185
* @see https://php.net/arrayaccess.offsetexists
186-
* @param mixed $key
186+
* @param mixed $offset
187187
* @return boolean
188+
* @psalm-param array-key $offset
188189
*/
189190
#[ReturnTypeWillChange]
190-
public function offsetExists($key)
191+
public function offsetExists($offset)
191192
{
192-
return array_key_exists($key, $this->info);
193+
return array_key_exists($offset, $this->info);
193194
}
194195

195196
/**
@@ -201,26 +202,27 @@ public function offsetExists($key)
201202
*
202203
* @see https://php.net/arrayaccess.offsetget
203204
* @see https://github.com/mongodb/specifications/blob/master/source/enumerate-indexes.rst#getting-full-index-information
204-
* @param mixed $key
205+
* @param mixed $offset
205206
* @return mixed
207+
* @psalm-param array-key $offset
206208
*/
207209
#[ReturnTypeWillChange]
208-
public function offsetGet($key)
210+
public function offsetGet($offset)
209211
{
210-
return $this->info[$key];
212+
return $this->info[$offset];
211213
}
212214

213215
/**
214216
* Not supported.
215217
*
216218
* @see https://php.net/arrayaccess.offsetset
217-
* @param mixed $key
219+
* @param mixed $offset
218220
* @param mixed $value
219221
* @throws BadMethodCallException
220222
* @return void
221223
*/
222224
#[ReturnTypeWillChange]
223-
public function offsetSet($key, $value)
225+
public function offsetSet($offset, $value)
224226
{
225227
throw BadMethodCallException::classIsImmutable(self::class);
226228
}
@@ -229,12 +231,12 @@ public function offsetSet($key, $value)
229231
* Not supported.
230232
*
231233
* @see https://php.net/arrayaccess.offsetunset
232-
* @param mixed $key
234+
* @param mixed $offset
233235
* @throws BadMethodCallException
234236
* @return void
235237
*/
236238
#[ReturnTypeWillChange]
237-
public function offsetUnset($key)
239+
public function offsetUnset($offset)
238240
{
239241
throw BadMethodCallException::classIsImmutable(self::class);
240242
}

src/PsrLogAdapter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,21 @@ public static function addLogger(LoggerInterface $logger): void
9090
*
9191
* @see LogSubscriber::log()
9292
*/
93-
public function log(int $mongocLevel, string $domain, string $message): void
93+
public function log(int $level, string $domain, string $message): void
9494
{
95-
if (! isset(self::MONGOC_TO_PSR[$mongocLevel])) {
95+
if (! isset(self::MONGOC_TO_PSR[$level])) {
9696
throw new UnexpectedValueException(sprintf(
9797
'Expected level to be >= %d and <= %d, %d given for domain "%s" and message: %s',
9898
LogSubscriber::LEVEL_ERROR,
9999
LogSubscriber::LEVEL_DEBUG,
100-
$mongocLevel,
100+
$level,
101101
$domain,
102102
$message,
103103
));
104104
}
105105

106106
$instance = self::getInstance();
107-
$psrLevel = self::MONGOC_TO_PSR[$mongocLevel];
107+
$psrLevel = self::MONGOC_TO_PSR[$level];
108108
$context = ['domain' => $domain];
109109

110110
foreach ($instance->loggers as $logger) {

0 commit comments

Comments
 (0)