Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPLIB-1612 Upgrade Psalm v6 #1576

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,5 @@ runs:
- name: Install dependencies with Composer
uses: ramsey/[email protected]
with:
# Revert when psalm supports PHP 8.4
# composer-options: "--no-suggest"
composer-options: "--no-suggest ${{ inputs.php-version == '8.4' && '--ignore-platform-req=php+' || '' }}"
composer-options: "--no-suggest"
working-directory: "${{ inputs.working-directory }}"
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"phpunit/phpunit": "^10.5.35",
"rector/rector": "^1.2",
"squizlabs/php_codesniffer": "^3.7",
"vimeo/psalm": "^5.13"
"vimeo/psalm": "^6.5"
},
"replace": {
"mongodb/builder": "*"
Expand Down
20 changes: 1 addition & 19 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.26.1@d747f6500b38ac4f7dfc5edbcae6e4b637d7add0">
<files psalm-version="6.5.1@3f17a6b24a2dbe543e21408c2b19108cf6a355ef">
<file src="examples/atlas_search.php">
<MixedArgument>
<code><![CDATA[$document['name']]]></code>
Expand Down Expand Up @@ -398,11 +398,6 @@
<code><![CDATA[$this->current()]]></code>
</PossiblyNullArgument>
</file>
<file src="src/Model/CodecCursor.php">
<TooManyArguments>
<code><![CDATA[getId]]></code>
</TooManyArguments>
</file>
<file src="src/Model/CollectionInfoCommandIterator.php">
<DeprecatedInterface>
<code><![CDATA[CollectionInfoCommandIterator]]></code>
Expand Down Expand Up @@ -754,9 +749,6 @@
<code><![CDATA[$options['writeConcern']]]></code>
<code><![CDATA[$value]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code><![CDATA[array|object|null]]></code>
</MixedInferredReturnType>
<MixedMethodCall>
<code><![CDATA[decode]]></code>
<code><![CDATA[isInTransaction]]></code>
Expand All @@ -772,9 +764,6 @@
<MixedAssignment>
<code><![CDATA[$document]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code><![CDATA[array|object|null]]></code>
</MixedInferredReturnType>
<MixedReturnStatement>
<code><![CDATA[$document === false ? null : $document]]></code>
<code><![CDATA[$document === false ? null : $document]]></code>
Expand Down Expand Up @@ -922,9 +911,6 @@
<MixedAssignment>
<code><![CDATA[$this->postBatchResumeToken]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code><![CDATA[array|object|null]]></code>
</MixedInferredReturnType>
<MixedPropertyFetch>
<code><![CDATA[$reply->cursor->firstBatch]]></code>
<code><![CDATA[$reply->cursor->postBatchResumeToken]]></code>
Expand Down Expand Up @@ -963,10 +949,6 @@
<code><![CDATA[$typeMap['fieldPaths'][$fieldPath]]]></code>
<code><![CDATA[$value]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code><![CDATA[array|object|null]]></code>
<code><![CDATA[array|object|null]]></code>
</MixedInferredReturnType>
<MixedReturnStatement>
<code><![CDATA[$collectionInfo['options']['encryptedFields'] ?? null]]></code>
<code><![CDATA[$collectionInfo['options']['encryptedFields'] ?? null]]></code>
Expand Down
8 changes: 2 additions & 6 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<psalm
errorLevel="1"
errorBaseline="psalm-baseline.xml"
ignoreInternalFunctionFalseReturn="true"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


ERROR: PossiblyFalseArgument
at examples/gridfs_stream.php:44:21
Argument 1 of strlen cannot be false, possibly string value expected (see https://psalm.dev/104)
    $size += strlen($data);


ERROR: PossiblyFalseArgument
at examples/gridfs_upload.php:31:8
Argument 1 of fwrite cannot be false, possibly resource value expected (see https://psalm.dev/104)
fwrite($stream, 'Hello world!');


ERROR: PossiblyFalseArgument
at examples/gridfs_upload.php:32:8
Argument 1 of rewind cannot be false, possibly resource value expected (see https://psalm.dev/104)
rewind($stream);


ERROR: PossiblyFalseArgument
at examples/gridfs_upload.php:35:46
Argument 2 of MongoDB\GridFS\Bucket::uploadFromStream cannot be false, possibly resource value expected (see https://psalm.dev/104)
$id = $gridfs->uploadFromStream('hello.txt', $stream);


ERROR: PossiblyFalseArgument
at examples/gridfs_upload.php:38:8
Argument 1 of fclose cannot be false, possibly resource value expected (see https://psalm.dev/104)
fclose($stream);


ERROR: PossiblyFalseArgument
at examples/gridfs_upload.php:42:46
Argument 2 of MongoDB\GridFS\Bucket::downloadToStreamByName cannot be false, possibly resource value expected (see https://psalm.dev/104)
$gridfs->downloadToStreamByName('hello.txt', $stream);


ERROR: PossiblyFalseArgument
at examples/gridfs_upload.php:43:8
Argument 1 of rewind cannot be false, possibly resource value expected (see https://psalm.dev/104)
rewind($stream);


ERROR: PossiblyFalseArgument
at examples/gridfs_upload.php:44:45
Argument 1 of stream_get_contents cannot be false, possibly resource value expected (see https://psalm.dev/104)
echo 'File contents: ', stream_get_contents($stream), "\n";


ERROR: InvalidFalsableReturnType
at src/Collection.php:338:16
The declared return type 'string' for MongoDB\Collection::createIndex does not allow false, but 'false|string' contains false (see https://psalm.dev/143)
     * @return string The name of the created index


ERROR: FalsableReturnStatement
at src/Collection.php:349:16
The declared return type 'string' for MongoDB\Collection::createIndex does not allow false, but the function returns 'false|string' (see https://psalm.dev/137)
        return current($this->createIndexes([['key' => $key] + $indexOptions], $operationOptions));


ERROR: InvalidFalsableReturnType
at src/Collection.php:396:16
The declared return type 'string' for MongoDB\Collection::createSearchIndex does not allow false, but 'false|string' contains false (see https://psalm.dev/143)
     * @return string The name of the created search index


ERROR: FalsableReturnStatement
at src/Collection.php:411:16
The declared return type 'string' for MongoDB\Collection::createSearchIndex does not allow false, but the function returns 'false|string' (see https://psalm.dev/137)
        return current($names);


ERROR: InvalidFalsableReturnType
at src/GridFS/Bucket.php:593:16
The declared return type 'resource' for MongoDB\GridFS\Bucket::openUploadStream does not allow false, but 'false|resource' contains false (see https://psalm.dev/143)
     * @return resource


ERROR: FalsableReturnStatement
at src/GridFS/Bucket.php:611:16
The declared return type 'resource' for MongoDB\GridFS\Bucket::openUploadStream does not allow false, but the function returns 'false|resource' (see https://psalm.dev/137)
        return fopen($path, 'w', false, $context);


ERROR: InvalidFalsableReturnType
at src/GridFS/Bucket.php:798:16
The declared return type 'resource' for MongoDB\GridFS\Bucket::openDownloadStreamByFile does not allow false, but 'false|resource' contains false (see https://psalm.dev/143)
     * @return resource


ERROR: FalsableReturnStatement
at src/GridFS/Bucket.php:810:16
The declared return type 'resource' for MongoDB\GridFS\Bucket::openDownloadStreamByFile does not allow false, but the function returns 'false|resource' (see https://psalm.dev/137)
        return fopen($path, 'r', false, $context);


ERROR: PossiblyInvalidArrayAccess
at src/Model/BSONIterator.php:142:12
Cannot access array value on non-array variable  of type false (see https://psalm.dev/109)
        [, $documentLength] = unpack('V', substr($this->buffer, $this->position, self::BSON_SIZE));


ERROR: PossiblyFalseArgument
at src/Model/DatabaseInfoLegacyIterator.php:50:33
Argument 1 of MongoDB\Model\DatabaseInfo::__construct cannot be false, possibly array<array-key, mixed> value expected (see https://psalm.dev/104)
        return new DatabaseInfo(current($this->databases));


ERROR: InvalidFalsableReturnType
at src/Operation/CreateCollection.php:252:16
The declared return type 'array<array-key, mixed>|object' for MongoDB\Operation\CreateCollection::execute does not allow false, but 'array<array-key, mixed>|false|object' contains false (see https://psalm.dev/143)
     * @return array|object Command result document


ERROR: FalsableReturnStatement
at src/Operation/CreateCollection.php:263:16
The declared return type 'array<array-key, mixed>|object' for MongoDB\Operation\CreateCollection::execute does not allow false, but the function returns 'array<array-key, mixed>|false|object' (see https://psalm.dev/137)
        return current($cursor->toArray());


ERROR: InvalidFalsableReturnType
at src/Operation/DropCollection.php:89:16
The declared return type 'array<array-key, mixed>|object' for MongoDB\Operation\DropCollection::execute does not allow false, but 'array<array-key, mixed>|false|object' contains false (see https://psalm.dev/143)
     * @return array|object Command result document


ERROR: FalsableReturnStatement
at src/Operation/DropCollection.php:117:16
The declared return type 'array<array-key, mixed>|object' for MongoDB\Operation\DropCollection::execute does not allow false, but the function returns 'array<array-key, mixed>|false|object' (see https://psalm.dev/137)
        return current($cursor->toArray());


ERROR: InvalidFalsableReturnType
at src/Operation/DropDatabase.php:84:16
The declared return type 'array<array-key, mixed>|object' for MongoDB\Operation\DropDatabase::execute does not allow false, but 'array<array-key, mixed>|false|object' contains false (see https://psalm.dev/143)
     * @return array|object Command result document


ERROR: FalsableReturnStatement
at src/Operation/DropDatabase.php:95:16
The declared return type 'array<array-key, mixed>|object' for MongoDB\Operation\DropDatabase::execute does not allow false, but the function returns 'array<array-key, mixed>|false|object' (see https://psalm.dev/137)
        return current($cursor->toArray());


ERROR: InvalidFalsableReturnType
at src/Operation/DropIndexes.php:98:16
The declared return type 'array<array-key, mixed>|object' for MongoDB\Operation\DropIndexes::execute does not allow false, but 'array<array-key, mixed>|false|object' contains false (see https://psalm.dev/143)
     * @return array|object Command result document


ERROR: FalsableReturnStatement
at src/Operation/DropIndexes.php:115:16
The declared return type 'array<array-key, mixed>|object' for MongoDB\Operation\DropIndexes::execute does not allow false, but the function returns 'array<array-key, mixed>|false|object' (see https://psalm.dev/137)
        return current($cursor->toArray());


ERROR: InvalidFalsableReturnType
at src/Operation/Explain.php:92:16
The declared return type 'array<array-key, mixed>|object' for MongoDB\Operation\Explain::execute does not allow false, but 'array<array-key, mixed>|false|object' contains false (see https://psalm.dev/143)
     * @return array|object


ERROR: FalsableReturnStatement
at src/Operation/Explain.php:104:16
The declared return type 'array<array-key, mixed>|object' for MongoDB\Operation\Explain::execute does not allow false, but the function returns 'array<array-key, mixed>|false|object' (see https://psalm.dev/137)
        return current($cursor->toArray());


ERROR: InvalidFalsableReturnType
at src/Operation/ModifyCollection.php:89:16
The declared return type 'array<array-key, mixed>|object' for MongoDB\Operation\ModifyCollection::execute does not allow false, but 'array<array-key, mixed>|false|object' contains false (see https://psalm.dev/143)
     * @return array|object Command result document


ERROR: FalsableReturnStatement
at src/Operation/ModifyCollection.php:100:16
The declared return type 'array<array-key, mixed>|object' for MongoDB\Operation\ModifyCollection::execute does not allow false, but the function returns 'array<array-key, mixed>|false|object' (see https://psalm.dev/137)
        return current($cursor->toArray());


ERROR: InvalidFalsableReturnType
at src/Operation/RenameCollection.php:103:16
The declared return type 'array<array-key, mixed>|object' for MongoDB\Operation\RenameCollection::execute does not allow false, but 'array<array-key, mixed>|false|object' contains false (see https://psalm.dev/143)
     * @return array|object Command result document


ERROR: FalsableReturnStatement
at src/Operation/RenameCollection.php:120:16
The declared return type 'array<array-key, mixed>|object' for MongoDB\Operation\RenameCollection::execute does not allow false, but the function returns 'array<array-key, mixed>|false|object' (see https://psalm.dev/137)
        return current($cursor->toArray());


ignoreInternalFunctionNullReturn="true"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this static analysis errors can be addressed. There may be edge cases when using the iterators.

ERROR: PropertyTypeCoercion
at src/Model/CachingIterator.php:161:9
$this->items expects 'list<array{0: TKey:MongoDB\Model\CachingIterator as array-key, 1: TValue:MongoDB\Model\CachingIterator as mixed}>',  parent type 'non-empty-list<array{0: (TKey:MongoDB\Model\CachingIterator as array-key)|null, 1: (TValue:MongoDB\Model\CachingIterator as mixed)|null}>' provided (see https://psalm.dev/198)
        $this->items[] = [


ERROR: PropertyTypeCoercion
at src/Model/CachingIterator.php:161:9
$this->items expects 'list<array{0: TKey:MongoDB\Model\CachingIterator as array-key, 1: TValue:MongoDB\Model\CachingIterator as mixed}>',  parent type 'non-empty-list<array{0: array-key|null, 1: mixed}>' provided (see https://psalm.dev/198)
        $this->items[] = [


ERROR: PossiblyNullArgument
at src/Model/CallbackIterator.php:62:48
Argument 1 cannot be null, possibly null value provided (see https://psalm.dev/078)
        return call_user_func($this->callback, $this->iterator->current(), $this->iterator->key());


ERROR: PossiblyNullArgument
at src/Model/CallbackIterator.php:62:76
Argument 2 cannot be null, possibly null value provided (see https://psalm.dev/078)
        return call_user_func($this->callback, $this->iterator->current(), $this->iterator->key());


ERROR: PossiblyNullArgument
at src/Model/CollectionInfoCommandIterator.php:58:35
Argument 1 of MongoDB\Model\CollectionInfo::__construct cannot be null, possibly null value provided (see https://psalm.dev/078)
        return new CollectionInfo($info);


ERROR: NullableReturnStatement
at src/Model/DatabaseInfoLegacyIterator.php:60:16
The declared return type 'int' for MongoDB\Model\DatabaseInfoLegacyIterator::key is not nullable, but the function returns 'array-key|null' (see https://psalm.dev/139)
        return key($this->databases);


ERROR: PossiblyNullArgument
at src/Model/IndexInfoIteratorIterator.php:58:38
Argument 2 of array_key_exists cannot be null, possibly null value provided (see https://psalm.dev/078)
        if (! array_key_exists('ns', $info) && $this->ns !== null) {


ERROR: PossiblyNullArgument
at src/Model/IndexInfoIteratorIterator.php:62:30
Argument 1 of MongoDB\Model\IndexInfo::__construct cannot be null, possibly null value provided (see https://psalm.dev/078)
        return new IndexInfo($info);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this static analysis errors can be addressed

Assuming you don't intend to address these in this PR, I'd suggest opening a new PHPLIB ticket that includes both sets of errors for each config option. Cross-reference it to PHPLIB-1612 and backlog it.

Copy link
Member Author

@GromNaN GromNaN Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created PHPLIB-1625

resolveFromConfigFile="true"
findUnusedBaselineEntry="true"
findUnusedCode="false"
Expand All @@ -17,12 +19,6 @@
</ignoreFiles>
</projectFiles>

<stubs>
<file name="stubs/BSON/Document.stub.php"/>
<file name="stubs/BSON/Iterator.stub.php"/>
<file name="stubs/BSON/PackedArray.stub.php"/>
jmikola marked this conversation as resolved.
Show resolved Hide resolved
</stubs>

<issueHandlers>
<!-- Class is meant to be Unserialized by the extension only -->
<PropertyNotSetInConstructor>
Expand Down
49 changes: 0 additions & 49 deletions stubs/BSON/Document.stub.php

This file was deleted.

29 changes: 0 additions & 29 deletions stubs/BSON/Iterator.stub.php

This file was deleted.

40 changes: 0 additions & 40 deletions stubs/BSON/PackedArray.stub.php

This file was deleted.

Loading