bmewburn / vscode-intelephense

PHP intellisense for Visual Studio Code
https://intelephense.com
Other
1.61k stars 94 forks source link

PostgreSQL functions and PHP 8.1+: "Expected type 'resource'. Found 'PgSql\Connection'." #2802

Closed mikkorantalainen closed 1 week ago

mikkorantalainen commented 7 months ago

Describe the bug Intelephense incorrectly raises an error for valid PHP code using PostgreSQL API functions with instance of PgSql\Connection instead of a resource.

To Reproduce A code snippet (NOT a screenshot of a code snippet) or steps to reproduce the issue

namespace Database;

class Connection
{
    /**
     * PostgreSQL database handle for open socket
     * @var \PgSql\Connection
     */
    private $dbconnection;

    public function disconnect()
    {
        if ($this->dbconnection)
        {
            $status = pg_flush($this->dbconnection); /* error shown here */
        }
    }
}

Actual result

[{
    "resource": "/path/to/file.php",
    "owner": "_generated_diagnostic_collection_name_#4",
    "code": "P1006",
    "severity": 8,
    "message": "Expected type 'resource'. Found 'PgSql\\Connection'.",
    "source": "intelephense",
    "startLineNumber": 15,
    "startColumn": 24,
    "endLineNumber": 15,
    "endColumn": 43
}]

Expected behavior The code should have been accepted as correct because I've set workspace Intelephense settings to PHP version 8.1 which should expect PgSql\Connection as the argument to pg_flush() as documented in official documentation: https://www.php.net/manual/en/function.pg-flush.php

Note that this same error is shown for at least pg_flush(), pg_close(), pg_connection_status(), pg_send_query(), pg_get_result(), pg_last_notice() and pg_errormessage(). Probably for all functions that used to take resource instead of PgSql\Connection in PHP versions older than version 8.1.

Platform and version VS Code 1.86.2 and Intelephense version v1.10.2 running on Ubuntu 22.04 LTS.

bmewburn commented 7 months ago

I can only reproduce this if I set my intelephense.environment.phpVersion to something less than 8.1.0. Does reindexing fix it? ctrl + shift + p -> Index workspace .