Skip to content

Commit 576c7b3

Browse files
committed
Only support string offsets for LazyBSONDocument
1 parent f9eab8f commit 576c7b3

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

src/Model/LazyBSONDocument.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use function get_object_vars;
3434
use function is_array;
3535
use function is_object;
36+
use function is_string;
3637
use function MongoDB\recursive_copy;
3738
use function sprintf;
3839
use function trigger_error;
@@ -183,7 +184,11 @@ function ($value, string $key) use (&$seen) {
183184
/** @param mixed $offset */
184185
public function offsetExists($offset): bool
185186
{
186-
return $this->__isset((string) $offset);
187+
if (! is_string($offset)) {
188+
return false;
189+
}
190+
191+
return $this->__isset($offset);
187192
}
188193

189194
/**
@@ -193,7 +198,13 @@ public function offsetExists($offset): bool
193198
#[ReturnTypeWillChange]
194199
public function offsetGet($offset)
195200
{
196-
return $this->__get((string) $offset);
201+
if (! is_string($offset)) {
202+
trigger_error(sprintf('Undefined offset: %s', (string) $offset), E_USER_WARNING);
203+
204+
return null;
205+
}
206+
207+
return $this->__get($offset);
197208
}
198209

199210
/**
@@ -202,13 +213,25 @@ public function offsetGet($offset)
202213
*/
203214
public function offsetSet($offset, $value): void
204215
{
205-
$this->__set((string) $offset, $value);
216+
if (! is_string($offset)) {
217+
trigger_error(sprintf('Unsupported offset: %s', (string) $offset), E_USER_WARNING);
218+
219+
return;
220+
}
221+
222+
$this->__set($offset, $value);
206223
}
207224

208225
/** @param mixed $offset */
209226
public function offsetUnset($offset): void
210227
{
211-
$this->__unset((string) $offset);
228+
if (! is_string($offset)) {
229+
trigger_error(sprintf('Undefined offset: %s', (string) $offset), E_USER_WARNING);
230+
231+
return;
232+
}
233+
234+
$this->__unset($offset);
212235
}
213236

214237
private function readFromBson(string $key): void

tests/Model/LazyBSONDocumentTest.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,8 @@ public function testConstructWithWrongType(): void
7373
}
7474

7575
/** @dataProvider provideTestDocumentWithNativeArrays */
76-
public function testConstructWithArrayUsesLiteralValues($value): void
76+
public function testConstructWithArrayUsesLiteralValues($document): void
7777
{
78-
$document = new LazyBSONDocument($value);
79-
8078
$this->assertInstanceOf(stdClass::class, $document->document);
8179
$this->assertIsArray($document->hash);
8280
$this->assertIsArray($document->array);
@@ -143,6 +141,15 @@ public function testOffsetGetForMissingOffset(LazyBSONDocument $document): void
143141
$document['bar'];
144142
}
145143

144+
public function testOffsetGetWithInvalidOffset(): void
145+
{
146+
$document = new LazyBSONDocument(['foo' => 'bar']);
147+
148+
$this->expectWarning();
149+
$this->expectWarningMessage('Undefined offset: 1');
150+
$document[1];
151+
}
152+
146153
/** @dataProvider provideTestDocument */
147154
public function testGetDocument(LazyBSONDocument $document): void
148155
{
@@ -171,6 +178,13 @@ public function testOffsetExists(LazyBSONDocument $document): void
171178
$this->assertFalse(isset($document['bar']));
172179
}
173180

181+
public function testOffsetExistsWithInvalidOffset(): void
182+
{
183+
$document = new LazyBSONDocument(['foo' => 'bar']);
184+
185+
$this->assertFalse(isset($document[1]));
186+
}
187+
174188
/** @dataProvider provideTestDocument */
175189
public function testPropertySet(LazyBSONDocument $document): void
176190
{
@@ -195,6 +209,15 @@ public function testOffsetSet(LazyBSONDocument $document): void
195209
$this->assertSame('baz', $document['foo']);
196210
}
197211

212+
public function testOffsetSetWithInvalidOffset(): void
213+
{
214+
$document = new LazyBSONDocument(['foo' => 'bar']);
215+
216+
$this->expectWarning();
217+
$this->expectWarningMessage('Unsupported offset: 1');
218+
$document[1] = 'foo';
219+
}
220+
198221
/** @dataProvider provideTestDocument */
199222
public function testPropertyUnset(LazyBSONDocument $document): void
200223
{
@@ -229,6 +252,15 @@ public function testOffsetUnset(LazyBSONDocument $document): void
229252
$this->assertFalse(isset($document['document']));
230253
}
231254

255+
public function testOffsetUnsetWithInvalidOffset(): void
256+
{
257+
$document = new LazyBSONDocument(['foo' => 'bar']);
258+
259+
$this->expectWarning();
260+
$this->expectWarningMessage('Undefined offset: 1');
261+
unset($document[1]);
262+
}
263+
232264
/** @dataProvider provideTestDocument */
233265
public function testIterator(LazyBSONDocument $document): void
234266
{

0 commit comments

Comments
 (0)