Skip to content

Commit 0adc38b

Browse files
committed
Ignore certain phpcs errors for all examples
1 parent 999fa8b commit 0adc38b

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

examples/command_logger.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ function toJSON(object $document): string
2828
return toRelaxedExtendedJSON(fromPHP($document));
2929
}
3030

31-
// phpcs:disable Squiz.Classes.ClassFileName.NoMatch
3231
class CommandLogger implements CommandSubscriber
3332
{
3433
public function commandStarted(CommandStartedEvent $event): void

examples/persistable.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
use MongoDB\BSON\ObjectId;
77
use MongoDB\BSON\Persistable;
88
use MongoDB\Client;
9-
use stdClass;
9+
use MongoDB\Model\BSONArray;
10+
use UnexpectedValueException;
1011

1112
use function array_search;
1213
use function dirname;
@@ -15,15 +16,15 @@
1516

1617
require dirname(__FILE__) . '/../vendor/autoload.php';
1718

18-
class Entry implements Persistable
19+
class PersistableEntry implements Persistable
1920
{
2021
/** @var ObjectId */
2122
private $id;
2223

2324
/** @var string */
2425
private $name;
2526

26-
/** @var array<Email> */
27+
/** @var array<PersistableEmail> */
2728
private $emails = [];
2829

2930
public function __construct(string $name)
@@ -52,12 +53,12 @@ public function getEmails(): array
5253
return $this->emails;
5354
}
5455

55-
public function addEmail(Email $email): void
56+
public function addEmail(PersistableEmail $email): void
5657
{
5758
$this->emails[] = $email;
5859
}
5960

60-
public function deleteEmail(Email $email): void
61+
public function deleteEmail(PersistableEmail $email): void
6162
{
6263
$index = array_search($email, $this->emails, true);
6364
if ($index === false) {
@@ -67,7 +68,7 @@ public function deleteEmail(Email $email): void
6768
unset($this->emails[$index]);
6869
}
6970

70-
public function bsonSerialize(): stdClass
71+
public function bsonSerialize(): object
7172
{
7273
return (object) [
7374
'_id' => $this->id,
@@ -78,13 +79,23 @@ public function bsonSerialize(): stdClass
7879

7980
public function bsonUnserialize(array $data): void
8081
{
82+
if (! $data['_id'] instanceof ObjectId) {
83+
throw new UnexpectedValueException('_id field is not of the expected type');
84+
}
85+
86+
if (! $data['emails'] instanceof BSONArray) {
87+
throw new UnexpectedValueException('emails field is not of the expected type');
88+
}
89+
8190
$this->id = $data['_id'];
8291
$this->name = (string) $data['name'];
92+
93+
/** @psalm-suppress MixedPropertyTypeCoercion */
8394
$this->emails = $data['emails']->getArrayCopy(); // Emails will be passed as a BSONArray instance
8495
}
8596
}
8697

87-
class Email implements Persistable
98+
class PersistableEmail implements Persistable
8899
{
89100
/** @var string */
90101
private $type;
@@ -108,7 +119,7 @@ public function getAddress(): string
108119
return $this->address;
109120
}
110121

111-
public function bsonSerialize(): stdClass
122+
public function bsonSerialize(): object
112123
{
113124
return (object) [
114125
'type' => $this->type,
@@ -123,9 +134,9 @@ public function bsonUnserialize(array $data): void
123134
}
124135
}
125136

126-
$entry = new Entry('alcaeus');
127-
$entry->addEmail(new Email('work', '[email protected]'));
128-
$entry->addEmail(new Email('private', '[email protected]'));
137+
$entry = new PersistableEntry('alcaeus');
138+
$entry->addEmail(new PersistableEmail('work', '[email protected]'));
139+
$entry->addEmail(new PersistableEmail('private', '[email protected]'));
129140

130141
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');
131142

@@ -136,4 +147,5 @@ public function bsonUnserialize(array $data): void
136147

137148
$foundEntry = $collection->findOne([]);
138149

150+
/** @psalm-suppress ForbiddenCode */
139151
var_dump($foundEntry);

phpcs.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@
176176
<exclude-pattern>/tests/GridFS/UnusableStream.php</exclude-pattern>
177177
</rule>
178178
<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
179+
<exclude-pattern>/examples</exclude-pattern>
179180
<exclude-pattern>/tests/PHPUnit/ConstraintTrait.php</exclude-pattern>
180181
</rule>
182+
<rule ref="Squiz.Classes.ClassFileName.NoMatch">
183+
<exclude-pattern>/examples</exclude-pattern>
184+
</rule>
181185
</ruleset>

0 commit comments

Comments
 (0)