Skip to content

Commit c900a95

Browse files
committed
Use constructor property promotion where possible
1 parent a8fae58 commit c900a95

File tree

78 files changed

+78
-602
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+78
-602
lines changed

examples/persistable.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@ class PersistableEntry implements Persistable
1919
{
2020
private ObjectId $id;
2121

22-
public string $name;
23-
2422
/** @var array<PersistableEmail> */
2523
public array $emails = [];
2624

27-
public function __construct(string $name)
25+
public function __construct(public string $name)
2826
{
2927
$this->id = new ObjectId();
30-
$this->name = $name;
3128
}
3229

3330
public function getId(): ObjectId
@@ -64,14 +61,8 @@ public function bsonUnserialize(array $data): void
6461

6562
class PersistableEmail implements Persistable
6663
{
67-
public string $type;
68-
69-
public string $address;
70-
71-
public function __construct(string $type, string $address)
64+
public function __construct(public string $type, public string $address)
7265
{
73-
$this->type = $type;
74-
$this->address = $address;
7566
}
7667

7768
public function bsonSerialize(): stdClass

phpcs.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
<exclude name="Generic.Formatting.MultipleStatementAlignment" />
4242
<exclude name="Squiz.Commenting.FunctionComment.ThrowsNoFullStop" />
4343
<exclude name="SlevomatCodingStandard.TypeHints.UnionTypeHintFormat.DisallowedShortNullable" />
44-
<exclude name="SlevomatCodingStandard.Classes.RequireConstructorPropertyPromotion" />
4544

4645
<!-- Keep long typehints (for now) -->
4746
<exclude name="PSR12.Keywords.ShortFormTypeKeywords" />

src/BulkWriteResult.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,10 @@
2525
*/
2626
class BulkWriteResult
2727
{
28-
private WriteResult $writeResult;
29-
30-
private array $insertedIds;
31-
3228
private bool $isAcknowledged;
3329

34-
public function __construct(WriteResult $writeResult, array $insertedIds)
30+
public function __construct(private WriteResult $writeResult, private array $insertedIds)
3531
{
36-
$this->writeResult = $writeResult;
37-
$this->insertedIds = $insertedIds;
3832
$this->isAcknowledged = $writeResult->isAcknowledged();
3933
}
4034

src/ChangeStream.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ class ChangeStream implements Iterator
7575
/** @var ResumeCallable|null */
7676
private $resumeCallable;
7777

78-
private ChangeStreamIterator $iterator;
79-
8078
private int $key = 0;
8179

8280
/**
@@ -85,8 +83,6 @@ class ChangeStream implements Iterator
8583
*/
8684
private bool $hasAdvanced = false;
8785

88-
private ?DocumentCodec $codec;
89-
9086
/**
9187
* @see https://php.net/iterator.current
9288
* @return array|object|null
@@ -189,11 +185,9 @@ public function valid()
189185
*
190186
* @param ResumeCallable $resumeCallable
191187
*/
192-
public function __construct(ChangeStreamIterator $iterator, callable $resumeCallable, ?DocumentCodec $codec = null)
188+
public function __construct(private ChangeStreamIterator $iterator, callable $resumeCallable, private ?DocumentCodec $codec = null)
193189
{
194-
$this->iterator = $iterator;
195190
$this->resumeCallable = $resumeCallable;
196-
$this->codec = $codec;
197191

198192
if ($codec) {
199193
$this->iterator->getInnerIterator()->setTypeMap(['root' => 'bson']);

src/Collection.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,6 @@ class Collection
8686

8787
private ?DocumentCodec $codec = null;
8888

89-
private string $collectionName;
90-
91-
private string $databaseName;
92-
93-
private Manager $manager;
94-
9589
private ReadConcern $readConcern;
9690

9791
private ReadPreference $readPreference;
@@ -130,7 +124,7 @@ class Collection
130124
* @param array $options Collection options
131125
* @throws InvalidArgumentException for parameter/option parsing errors
132126
*/
133-
public function __construct(Manager $manager, string $databaseName, string $collectionName, array $options = [])
127+
public function __construct(private Manager $manager, private string $databaseName, private string $collectionName, array $options = [])
134128
{
135129
if (strlen($databaseName) < 1) {
136130
throw new InvalidArgumentException('$databaseName is invalid: ' . $databaseName);
@@ -160,10 +154,6 @@ public function __construct(Manager $manager, string $databaseName, string $coll
160154
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
161155
}
162156

163-
$this->manager = $manager;
164-
$this->databaseName = $databaseName;
165-
$this->collectionName = $collectionName;
166-
167157
$this->codec = $options['codec'] ?? null;
168158
$this->readConcern = $options['readConcern'] ?? $this->manager->getReadConcern();
169159
$this->readPreference = $options['readPreference'] ?? $this->manager->getReadPreference();

src/Command/ListCollections.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@
3838
*/
3939
class ListCollections implements Executable
4040
{
41-
private string $databaseName;
42-
43-
private array $options;
44-
4541
/**
4642
* Constructs a listCollections command.
4743
*
@@ -71,7 +67,7 @@ class ListCollections implements Executable
7167
* @param array $options Command options
7268
* @throws InvalidArgumentException for parameter/option parsing errors
7369
*/
74-
public function __construct(string $databaseName, array $options = [])
70+
public function __construct(private string $databaseName, private array $options = [])
7571
{
7672
if (isset($options['authorizedCollections']) && ! is_bool($options['authorizedCollections'])) {
7773
throw InvalidArgumentException::invalidType('"authorizedCollections" option', $options['authorizedCollections'], 'boolean');
@@ -92,9 +88,6 @@ public function __construct(string $databaseName, array $options = [])
9288
if (isset($options['session']) && ! $options['session'] instanceof Session) {
9389
throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
9490
}
95-
96-
$this->databaseName = $databaseName;
97-
$this->options = $options;
9891
}
9992

10093
/**

src/Command/ListDatabases.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
*/
4040
class ListDatabases implements Executable
4141
{
42-
private array $options;
43-
4442
/**
4543
* Constructs a listDatabases command.
4644
*
@@ -69,7 +67,7 @@ class ListDatabases implements Executable
6967
* @param array $options Command options
7068
* @throws InvalidArgumentException for parameter/option parsing errors
7169
*/
72-
public function __construct(array $options = [])
70+
public function __construct(private array $options = [])
7371
{
7472
if (isset($options['authorizedDatabases']) && ! is_bool($options['authorizedDatabases'])) {
7573
throw InvalidArgumentException::invalidType('"authorizedDatabases" option', $options['authorizedDatabases'], 'boolean');
@@ -90,8 +88,6 @@ public function __construct(array $options = [])
9088
if (isset($options['session']) && ! $options['session'] instanceof Session) {
9189
throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
9290
}
93-
94-
$this->options = $options;
9591
}
9692

9793
/**

src/Database.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ class Database
6161

6262
private const WIRE_VERSION_FOR_READ_CONCERN_WITH_WRITE_STAGE = 8;
6363

64-
private string $databaseName;
65-
66-
private Manager $manager;
67-
6864
private ReadConcern $readConcern;
6965

7066
private ReadPreference $readPreference;
@@ -100,7 +96,7 @@ class Database
10096
* @param array $options Database options
10197
* @throws InvalidArgumentException for parameter/option parsing errors
10298
*/
103-
public function __construct(Manager $manager, string $databaseName, array $options = [])
99+
public function __construct(private Manager $manager, private string $databaseName, array $options = [])
104100
{
105101
if (strlen($databaseName) < 1) {
106102
throw new InvalidArgumentException('$databaseName is invalid: ' . $databaseName);
@@ -122,8 +118,6 @@ public function __construct(Manager $manager, string $databaseName, array $optio
122118
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
123119
}
124120

125-
$this->manager = $manager;
126-
$this->databaseName = $databaseName;
127121
$this->readConcern = $options['readConcern'] ?? $this->manager->getReadConcern();
128122
$this->readPreference = $options['readPreference'] ?? $this->manager->getReadPreference();
129123
$this->typeMap = $options['typeMap'] ?? self::DEFAULT_TYPE_MAP;

src/DeleteResult.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,10 @@
2525
*/
2626
class DeleteResult
2727
{
28-
private WriteResult $writeResult;
29-
3028
private bool $isAcknowledged;
3129

32-
public function __construct(WriteResult $writeResult)
30+
public function __construct(private WriteResult $writeResult)
3331
{
34-
$this->writeResult = $writeResult;
3532
$this->isAcknowledged = $writeResult->isAcknowledged();
3633
}
3734

src/Exception/CreateEncryptedCollectionException.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,9 @@
2727
*/
2828
final class CreateEncryptedCollectionException extends RuntimeException
2929
{
30-
private array $encryptedFields;
31-
32-
public function __construct(Throwable $previous, array $encryptedFields)
30+
public function __construct(Throwable $previous, private array $encryptedFields)
3331
{
3432
parent::__construct(sprintf('Creating encrypted collection failed due to previous %s: %s', $previous::class, $previous->getMessage()), 0, $previous);
35-
36-
$this->encryptedFields = $encryptedFields;
3733
}
3834

3935
/**

0 commit comments

Comments
 (0)