Skip to content

Commit 88ad6bd

Browse files
committed
Remove dangerous instantiation
Instantiating static in non-final classes makes assumptions about child class constructors, which is unsafe.
1 parent 939e26d commit 88ad6bd

10 files changed

+26
-84
lines changed

psalm-baseline.xml

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -59,37 +59,6 @@
5959
<code><![CDATA[$options['session']]]></code>
6060
</MixedAssignment>
6161
</file>
62-
<file src="src/Exception/BadMethodCallException.php">
63-
<UnsafeInstantiation>
64-
<code><![CDATA[new static(sprintf('%s is immutable', $class))]]></code>
65-
<code><![CDATA[new static(sprintf('%s should not be called for an unacknowledged write result', $method))]]></code>
66-
</UnsafeInstantiation>
67-
</file>
68-
<file src="src/Exception/InvalidArgumentException.php">
69-
<UnsafeInstantiation>
70-
<code><![CDATA[new static(sprintf('Expected %s to have type "%s" but found "%s"', $name, $expectedType, get_debug_type($value)))]]></code>
71-
</UnsafeInstantiation>
72-
</file>
73-
<file src="src/Exception/ResumeTokenException.php">
74-
<UnsafeInstantiation>
75-
<code><![CDATA[new static('Resume token not found in change document')]]></code>
76-
<code><![CDATA[new static(sprintf('Expected resume token to have type "array or object" but found "%s"', get_debug_type($value)))]]></code>
77-
</UnsafeInstantiation>
78-
</file>
79-
<file src="src/Exception/UnsupportedException.php">
80-
<UnsafeInstantiation>
81-
<code><![CDATA[new static('Array filters are not supported by the server executing this operation')]]></code>
82-
<code><![CDATA[new static('Collations are not supported by the server executing this operation')]]></code>
83-
<code><![CDATA[new static('Explain is not supported by the server executing this operation')]]></code>
84-
<code><![CDATA[new static('Hint is not supported by the server executing this operation')]]></code>
85-
<code><![CDATA[new static('Read concern is not supported by the server executing this command')]]></code>
86-
<code><![CDATA[new static('The "allowDiskUse" option is not supported by the server executing this operation')]]></code>
87-
<code><![CDATA[new static('The "commitQuorum" option is not supported by the server executing this operation')]]></code>
88-
<code><![CDATA[new static('The "readConcern" option cannot be specified within a transaction. Instead, specify it when starting the transaction.')]]></code>
89-
<code><![CDATA[new static('The "writeConcern" option cannot be specified within a transaction. Instead, specify it when starting the transaction.')]]></code>
90-
<code><![CDATA[new static('Write concern is not supported by the server executing this command')]]></code>
91-
</UnsafeInstantiation>
92-
</file>
9362
<file src="src/GridFS/Bucket.php">
9463
<DocblockTypeContradiction>
9564
<code>! is_resource($destination)</code>
@@ -109,27 +78,6 @@
10978
<code><![CDATA[$options['revision']]]></code>
11079
</MixedArgument>
11180
</file>
112-
<file src="src/GridFS/Exception/CorruptFileException.php">
113-
<UnsafeInstantiation>
114-
<code><![CDATA[new static(sprintf('Chunk not found for index "%d"', $expectedIndex))]]></code>
115-
<code><![CDATA[new static(sprintf('Expected chunk to have index "%d" but found "%d"', $expectedIndex, $index))]]></code>
116-
<code><![CDATA[new static(sprintf('Expected chunk to have size "%d" but found "%d"', $expectedSize, $size))]]></code>
117-
<code><![CDATA[new static(sprintf('Invalid data found for index "%d"', $chunkIndex))]]></code>
118-
</UnsafeInstantiation>
119-
</file>
120-
<file src="src/GridFS/Exception/FileNotFoundException.php">
121-
<UnsafeInstantiation>
122-
<code><![CDATA[new static(sprintf('File "%s" not found in "%s"', $json, $namespace))]]></code>
123-
<code><![CDATA[new static(sprintf('File with name "%s" and revision "%d" not found in "%s"', $filename, $revision, $namespace))]]></code>
124-
</UnsafeInstantiation>
125-
</file>
126-
<file src="src/GridFS/Exception/StreamException.php">
127-
<UnsafeInstantiation>
128-
<code><![CDATA[new static(sprintf('Downloading file from "%s" to "%s" failed. GridFS filename: "%s"', $sourceMetadata['uri'], $destinationMetadata['uri'], $filename))]]></code>
129-
<code><![CDATA[new static(sprintf('Downloading file from "%s" to "%s" failed. GridFS identifier: "%s"', $sourceMetadata['uri'], $destinationMetadata['uri'], $idString))]]></code>
130-
<code><![CDATA[new static(sprintf('Uploading file from "%s" to "%s" failed. GridFS filename: "%s"', $sourceMetadata['uri'], $destinationUri, $filename))]]></code>
131-
</UnsafeInstantiation>
132-
</file>
13381
<file src="src/GridFS/ReadableStream.php">
13482
<MixedArgument>
13583
<code><![CDATA[$currentChunk->n]]></code>
@@ -160,9 +108,6 @@
160108
<code>$this[$key]</code>
161109
<code>$value</code>
162110
</MixedAssignment>
163-
<UnsafeInstantiation>
164-
<code>new static()</code>
165-
</UnsafeInstantiation>
166111
</file>
167112
<file src="src/Model/BSONDocument.php">
168113
<ArgumentTypeCoercion>
@@ -172,9 +117,6 @@
172117
<code>$this[$key]</code>
173118
<code>$value</code>
174119
</MixedAssignment>
175-
<UnsafeInstantiation>
176-
<code>new static()</code>
177-
</UnsafeInstantiation>
178120
</file>
179121
<file src="src/Model/BSONIterator.php">
180122
<MixedArgument>

src/Exception/BadMethodCallException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class BadMethodCallException extends BaseBadMethodCallException implements Excep
3131
*/
3232
public static function classIsImmutable(string $class)
3333
{
34-
return new static(sprintf('%s is immutable', $class));
34+
return new self(sprintf('%s is immutable', $class));
3535
}
3636

3737
/**
@@ -42,6 +42,6 @@ public static function classIsImmutable(string $class)
4242
*/
4343
public static function unacknowledgedWriteResultAccess(string $method)
4444
{
45-
return new static(sprintf('%s should not be called for an unacknowledged write result', $method));
45+
return new self(sprintf('%s should not be called for an unacknowledged write result', $method));
4646
}
4747
}

src/Exception/InvalidArgumentException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static function invalidType(string $name, $value, $expectedType)
5454
$expectedType = self::expectedTypesToString($expectedType);
5555
}
5656

57-
return new static(sprintf('Expected %s to have type "%s" but found "%s"', $name, $expectedType, get_debug_type($value)));
57+
return new self(sprintf('Expected %s to have type "%s" but found "%s"', $name, $expectedType, get_debug_type($value)));
5858
}
5959

6060
/** @param list<string> $types */

src/Exception/ResumeTokenException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ResumeTokenException extends RuntimeException
3030
*/
3131
public static function invalidType($value)
3232
{
33-
return new static(sprintf('Expected resume token to have type "array or object" but found "%s"', get_debug_type($value)));
33+
return new self(sprintf('Expected resume token to have type "array or object" but found "%s"', get_debug_type($value)));
3434
}
3535

3636
/**
@@ -40,6 +40,6 @@ public static function invalidType($value)
4040
*/
4141
public static function notFound()
4242
{
43-
return new static('Resume token not found in change document');
43+
return new self('Resume token not found in change document');
4444
}
4545
}

src/Exception/UnsupportedException.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class UnsupportedException extends RuntimeException
2626
*/
2727
public static function allowDiskUseNotSupported()
2828
{
29-
return new static('The "allowDiskUse" option is not supported by the server executing this operation');
29+
return new self('The "allowDiskUse" option is not supported by the server executing this operation');
3030
}
3131

3232
/**
@@ -39,7 +39,7 @@ public static function allowDiskUseNotSupported()
3939
*/
4040
public static function arrayFiltersNotSupported()
4141
{
42-
return new static('Array filters are not supported by the server executing this operation');
42+
return new self('Array filters are not supported by the server executing this operation');
4343
}
4444

4545
/**
@@ -52,7 +52,7 @@ public static function arrayFiltersNotSupported()
5252
*/
5353
public static function collationNotSupported()
5454
{
55-
return new static('Collations are not supported by the server executing this operation');
55+
return new self('Collations are not supported by the server executing this operation');
5656
}
5757

5858
/**
@@ -63,7 +63,7 @@ public static function collationNotSupported()
6363
*/
6464
public static function commitQuorumNotSupported()
6565
{
66-
return new static('The "commitQuorum" option is not supported by the server executing this operation');
66+
return new self('The "commitQuorum" option is not supported by the server executing this operation');
6767
}
6868

6969
/**
@@ -73,7 +73,7 @@ public static function commitQuorumNotSupported()
7373
*/
7474
public static function explainNotSupported()
7575
{
76-
return new static('Explain is not supported by the server executing this operation');
76+
return new self('Explain is not supported by the server executing this operation');
7777
}
7878

7979
/**
@@ -83,7 +83,7 @@ public static function explainNotSupported()
8383
*/
8484
public static function hintNotSupported()
8585
{
86-
return new static('Hint is not supported by the server executing this operation');
86+
return new self('Hint is not supported by the server executing this operation');
8787
}
8888

8989
/**
@@ -93,7 +93,7 @@ public static function hintNotSupported()
9393
*/
9494
public static function readConcernNotSupported()
9595
{
96-
return new static('Read concern is not supported by the server executing this command');
96+
return new self('Read concern is not supported by the server executing this command');
9797
}
9898

9999
/**
@@ -103,7 +103,7 @@ public static function readConcernNotSupported()
103103
*/
104104
public static function readConcernNotSupportedInTransaction()
105105
{
106-
return new static('The "readConcern" option cannot be specified within a transaction. Instead, specify it when starting the transaction.');
106+
return new self('The "readConcern" option cannot be specified within a transaction. Instead, specify it when starting the transaction.');
107107
}
108108

109109
/**
@@ -113,7 +113,7 @@ public static function readConcernNotSupportedInTransaction()
113113
*/
114114
public static function writeConcernNotSupported()
115115
{
116-
return new static('Write concern is not supported by the server executing this command');
116+
return new self('Write concern is not supported by the server executing this command');
117117
}
118118

119119
/**
@@ -123,6 +123,6 @@ public static function writeConcernNotSupported()
123123
*/
124124
public static function writeConcernNotSupportedInTransaction()
125125
{
126-
return new static('The "writeConcern" option cannot be specified within a transaction. Instead, specify it when starting the transaction.');
126+
return new self('The "writeConcern" option cannot be specified within a transaction. Instead, specify it when starting the transaction.');
127127
}
128128
}

src/GridFS/Exception/CorruptFileException.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class CorruptFileException extends RuntimeException
2828
*/
2929
public static function invalidChunkData(int $chunkIndex): self
3030
{
31-
return new static(sprintf('Invalid data found for index "%d"', $chunkIndex));
31+
return new self(sprintf('Invalid data found for index "%d"', $chunkIndex));
3232
}
3333

3434
/**
@@ -39,7 +39,7 @@ public static function invalidChunkData(int $chunkIndex): self
3939
*/
4040
public static function missingChunk(int $expectedIndex)
4141
{
42-
return new static(sprintf('Chunk not found for index "%d"', $expectedIndex));
42+
return new self(sprintf('Chunk not found for index "%d"', $expectedIndex));
4343
}
4444

4545
/**
@@ -51,7 +51,7 @@ public static function missingChunk(int $expectedIndex)
5151
*/
5252
public static function unexpectedIndex(int $index, int $expectedIndex)
5353
{
54-
return new static(sprintf('Expected chunk to have index "%d" but found "%d"', $expectedIndex, $index));
54+
return new self(sprintf('Expected chunk to have index "%d" but found "%d"', $expectedIndex, $index));
5555
}
5656

5757
/**
@@ -63,6 +63,6 @@ public static function unexpectedIndex(int $index, int $expectedIndex)
6363
*/
6464
public static function unexpectedSize(int $size, int $expectedSize)
6565
{
66-
return new static(sprintf('Expected chunk to have size "%d" but found "%d"', $expectedSize, $size));
66+
return new self(sprintf('Expected chunk to have size "%d" but found "%d"', $expectedSize, $size));
6767
}
6868
}

src/GridFS/Exception/FileNotFoundException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class FileNotFoundException extends RuntimeException
3535
*/
3636
public static function byFilenameAndRevision(string $filename, int $revision, string $namespace)
3737
{
38-
return new static(sprintf('File with name "%s" and revision "%d" not found in "%s"', $filename, $revision, $namespace));
38+
return new self(sprintf('File with name "%s" and revision "%d" not found in "%s"', $filename, $revision, $namespace));
3939
}
4040

4141
/**
@@ -49,6 +49,6 @@ public static function byId($id, string $namespace)
4949
{
5050
$json = toJSON(fromPHP(['_id' => $id]));
5151

52-
return new static(sprintf('File "%s" not found in "%s"', $json, $namespace));
52+
return new self(sprintf('File "%s" not found in "%s"', $json, $namespace));
5353
}
5454
}

src/GridFS/Exception/StreamException.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function downloadFromFilenameFailed(string $filename, $source, $de
2020
$sourceMetadata = stream_get_meta_data($source);
2121
$destinationMetadata = stream_get_meta_data($destination);
2222

23-
return new static(sprintf('Downloading file from "%s" to "%s" failed. GridFS filename: "%s"', $sourceMetadata['uri'], $destinationMetadata['uri'], $filename));
23+
return new self(sprintf('Downloading file from "%s" to "%s" failed. GridFS filename: "%s"', $sourceMetadata['uri'], $destinationMetadata['uri'], $filename));
2424
}
2525

2626
/**
@@ -34,14 +34,14 @@ public static function downloadFromIdFailed($id, $source, $destination): self
3434
$sourceMetadata = stream_get_meta_data($source);
3535
$destinationMetadata = stream_get_meta_data($destination);
3636

37-
return new static(sprintf('Downloading file from "%s" to "%s" failed. GridFS identifier: "%s"', $sourceMetadata['uri'], $destinationMetadata['uri'], $idString));
37+
return new self(sprintf('Downloading file from "%s" to "%s" failed. GridFS identifier: "%s"', $sourceMetadata['uri'], $destinationMetadata['uri'], $idString));
3838
}
3939

4040
/** @param resource $source */
4141
public static function uploadFailed(string $filename, $source, string $destinationUri): self
4242
{
4343
$sourceMetadata = stream_get_meta_data($source);
4444

45-
return new static(sprintf('Uploading file from "%s" to "%s" failed. GridFS filename: "%s"', $sourceMetadata['uri'], $destinationUri, $filename));
45+
return new self(sprintf('Uploading file from "%s" to "%s" failed. GridFS filename: "%s"', $sourceMetadata['uri'], $destinationUri, $filename));
4646
}
4747
}

src/Model/BSONArray.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __clone()
5555
*/
5656
public static function __set_state(array $properties)
5757
{
58-
$array = new static();
58+
$array = new self();
5959
$array->exchangeArray($properties);
6060

6161
return $array;

src/Model/BSONDocument.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function __construct(array $input = [], int $flags = ArrayObject::ARRAY_A
6666
*/
6767
public static function __set_state(array $properties)
6868
{
69-
$document = new static();
69+
$document = new self();
7070
$document->exchangeArray($properties);
7171

7272
return $document;

0 commit comments

Comments
 (0)