Skip to content

Commit 8369be1

Browse files
committed
Use constructor property promotion where possible
1 parent f725dca commit 8369be1

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
@@ -80,8 +80,6 @@ class ChangeStream implements Iterator
8080
/** @var ResumeCallable|null */
8181
private $resumeCallable;
8282

83-
private ChangeStreamIterator $iterator;
84-
8583
private int $key = 0;
8684

8785
/**
@@ -90,8 +88,6 @@ class ChangeStream implements Iterator
9088
*/
9189
private bool $hasAdvanced = false;
9290

93-
private ?DocumentCodec $codec;
94-
9591
/**
9692
* @see https://php.net/iterator.current
9793
* @return array|object|null
@@ -207,11 +203,9 @@ public function valid()
207203
*
208204
* @param ResumeCallable $resumeCallable
209205
*/
210-
public function __construct(ChangeStreamIterator $iterator, callable $resumeCallable, ?DocumentCodec $codec = null)
206+
public function __construct(private ChangeStreamIterator $iterator, callable $resumeCallable, private ?DocumentCodec $codec = null)
211207
{
212-
$this->iterator = $iterator;
213208
$this->resumeCallable = $resumeCallable;
214-
$this->codec = $codec;
215209

216210
if ($codec) {
217211
$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
/**

src/Exception/UnsupportedValueException.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
class UnsupportedValueException extends InvalidArgumentException implements Exception
2626
{
27-
private mixed $value;
28-
2927
/** @return mixed */
3028
public function getValue()
3129
{
@@ -42,10 +40,8 @@ public static function invalidEncodableValue(mixed $value): self
4240
return new self(sprintf('Could not encode value of type "%s".', get_debug_type($value)), $value);
4341
}
4442

45-
private function __construct(string $message, mixed $value)
43+
private function __construct(string $message, private mixed $value)
4644
{
4745
parent::__construct($message);
48-
49-
$this->value = $value;
5046
}
5147
}

src/GridFS/Bucket.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ class Bucket
8686

8787
private CollectionWrapper $collectionWrapper;
8888

89-
private string $databaseName;
90-
91-
private Manager $manager;
92-
9389
private string $bucketName;
9490

9591
private bool $disableMD5;
@@ -131,7 +127,7 @@ class Bucket
131127
* @param array $options Bucket options
132128
* @throws InvalidArgumentException for parameter/option parsing errors
133129
*/
134-
public function __construct(Manager $manager, string $databaseName, array $options = [])
130+
public function __construct(private Manager $manager, private string $databaseName, array $options = [])
135131
{
136132
if (isset($options['disableMD5']) && $options['disableMD5'] === false) {
137133
@trigger_error('Setting GridFS "disableMD5" option to "false" is deprecated since mongodb/mongodb 1.18 and will not be supported in version 2.0.', E_USER_DEPRECATED);
@@ -183,8 +179,6 @@ public function __construct(Manager $manager, string $databaseName, array $optio
183179
throw InvalidArgumentException::cannotCombineCodecAndTypeMap();
184180
}
185181

186-
$this->manager = $manager;
187-
$this->databaseName = $databaseName;
188182
$this->bucketName = $options['bucketName'];
189183
$this->chunkSizeBytes = $options['chunkSizeBytes'];
190184
$this->codec = $options['codec'] ?? null;

src/GridFS/CollectionWrapper.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,8 @@
4141
*/
4242
class CollectionWrapper
4343
{
44-
private string $bucketName;
45-
4644
private Collection $chunksCollection;
4745

48-
private string $databaseName;
49-
5046
private bool $checkedIndexes = false;
5147

5248
private Collection $filesCollection;
@@ -61,11 +57,8 @@ class CollectionWrapper
6157
* @param array $collectionOptions Collection options
6258
* @throws InvalidArgumentException
6359
*/
64-
public function __construct(Manager $manager, string $databaseName, string $bucketName, array $collectionOptions = [])
60+
public function __construct(Manager $manager, private string $databaseName, private string $bucketName, array $collectionOptions = [])
6561
{
66-
$this->databaseName = $databaseName;
67-
$this->bucketName = $bucketName;
68-
6962
$this->filesCollection = new Collection($manager, $databaseName, sprintf('%s.files', $bucketName), $collectionOptions);
7063
$this->chunksCollection = new Collection($manager, $databaseName, sprintf('%s.chunks', $bucketName), $collectionOptions);
7164
}

src/GridFS/ReadableStream.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,8 @@ class ReadableStream
5151
/** @var (CursorInterface&Iterator)|null */
5252
private ?Iterator $chunksIterator = null;
5353

54-
private CollectionWrapper $collectionWrapper;
55-
5654
private int $expectedLastChunkSize = 0;
5755

58-
private object $file;
59-
6056
private int $length;
6157

6258
private int $numChunks = 0;
@@ -68,7 +64,7 @@ class ReadableStream
6864
* @param object $file GridFS file document
6965
* @throws CorruptFileException
7066
*/
71-
public function __construct(CollectionWrapper $collectionWrapper, object $file)
67+
public function __construct(private CollectionWrapper $collectionWrapper, private object $file)
7268
{
7369
if (! isset($file->chunkSize) || ! is_integer($file->chunkSize) || $file->chunkSize < 1) {
7470
throw new CorruptFileException('file.chunkSize is not an integer >= 1');
@@ -82,12 +78,9 @@ public function __construct(CollectionWrapper $collectionWrapper, object $file)
8278
throw new CorruptFileException('file._id does not exist');
8379
}
8480

85-
$this->file = $file;
8681
$this->chunkSize = $file->chunkSize;
8782
$this->length = $file->length;
8883

89-
$this->collectionWrapper = $collectionWrapper;
90-
9184
if ($this->length > 0) {
9285
$this->numChunks = (integer) ceil($this->length / $this->chunkSize);
9386
$this->expectedLastChunkSize = $this->length - (($this->numChunks - 1) * $this->chunkSize);

src/GridFS/WritableStream.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ class WritableStream
5454

5555
private bool $disableMD5;
5656

57-
private CollectionWrapper $collectionWrapper;
58-
5957
private array $file;
6058

6159
private ?HashContext $hashCtx = null;
@@ -92,7 +90,7 @@ class WritableStream
9290
* @param array $options Upload options
9391
* @throws InvalidArgumentException
9492
*/
95-
public function __construct(CollectionWrapper $collectionWrapper, string $filename, array $options = [])
93+
public function __construct(private CollectionWrapper $collectionWrapper, string $filename, array $options = [])
9694
{
9795
$options += [
9896
'_id' => new ObjectId(),
@@ -125,7 +123,6 @@ public function __construct(CollectionWrapper $collectionWrapper, string $filena
125123
}
126124

127125
$this->chunkSize = $options['chunkSizeBytes'];
128-
$this->collectionWrapper = $collectionWrapper;
129126
$this->disableMD5 = $options['disableMD5'];
130127

131128
if (! $this->disableMD5) {

src/InsertManyResult.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,10 @@
2525
*/
2626
class InsertManyResult
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

0 commit comments

Comments
 (0)