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

PHPLIB-1612 Upgrade Psalm v6 #1576

merged 3 commits into from
Feb 11, 2025

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Jan 27, 2025

Fix PHPLIB-1612

This is required to run psalm on PHP 8.4
Psalm v6 announcement

@@ -2,6 +2,8 @@
<psalm
errorLevel="1"
errorBaseline="psalm-baseline.xml"
ignoreInternalFunctionFalseReturn="true"
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

@@ -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());


@GromNaN GromNaN marked this pull request as ready for review February 11, 2025 11:56
@GromNaN GromNaN requested a review from a team as a code owner February 11, 2025 11:56
@GromNaN GromNaN requested a review from jmikola February 11, 2025 11:56
psalm.xml.dist Show resolved Hide resolved
@GromNaN GromNaN merged commit 910a68f into mongodb:v1.x Feb 11, 2025
31 checks passed
@GromNaN GromNaN deleted the PHPLIB-1612 branch February 11, 2025 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants