Skip to content

SQLite3Stmt::execute flagged as cannot return bool, but it can #3605

@colinmo

Description

@colinmo

Bug report

When using SQLite3 's execute function, the PHP doc's declaration states it returns a SQLite3Result object but the text of the document declares that it can return false if it failed. PHPStan rejects checking if the result of a SQLite3 execute is false.

Strict comparison using === between SQLite3Result and false will always evaluate to false.

<?php declare(strict_types = 1);

class CacheAPC extends \SQLite3
{
    /**
     * Constructor
     */
    public function __construct()
    {
        define("PERMISSION_FILE", 'preferences.db');
        if (!\file_exists(PERMISSION_FILE)) {
            \touch(PERMISSION_FILE, \time());
            \chmod(PERMISSION_FILE, 0600);
        }
        parent::__construct(
            PERMISSION_FILE,
            SQLITE3_OPEN_READWRITE,
            null
        );
    }
}

$db = new CacheAPC();
if ($db !== false) {
	$stmt = $db->prepare("INSERT INTO TEST_TABLE (FIELD_1) VALUES (:field1);");
	if ($stmt !== false) {
		$value = 1;
		$stmt->bindParam(':field1', $value);
		$ret = $stmt->execute();
		if ($ret === false) {
			throw new \Exception("Failed to insert");
		}
	}
}

Unique URL: https://phpstan.org/r/951545c0-9089-40e6-b136-3c1def3713a4

Expected output

PHPStan should not raise an issue, it should accept that execute can return false.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions