Skip to content

Result can possibly wrap a bare mysqli connection for write queries, breaking iteration #59

Description

@tyrsson

What was done to fix the analysis findings

mago analyze flagged 3 non-existent-method findings in src/Result.php:

  • rewind(): $this->resource->data_seek(0) where data_seek does not exist on plain mysqli.
  • loadFromMysqliResult(): $this->resource->fetch_assoc() where fetch_assoc does not exist on plain mysqli.

$this->resource is typed mysqli|mysqli_result|mysqli_stmt. Both call sites previously assumed the resource is never a bare mysqli connection object.

Added explicit instanceof guards immediately before each call, throwing a RuntimeException if the resource is not the expected type, instead of letting the call fail with an undefined method error, and instead of suppressing the finding with @mago-expect.

Rationale

Connection::execute() can pass the raw mysqli connection object itself into Driver::createResult() when a query is a non-SELECT (write) query that succeeds (true === $resultResource ? $this->resource : $resultResource). This means a Result instance can genuinely wrap a bare mysqli connection object rather than a mysqli_result or mysqli_stmt.

Calling rewind() or otherwise iterating such a Result would previously crash with an undefined method fatal error. The guards added turn that into a clear, specific RuntimeException instead.

Fixing the underlying design gap in this PR (whether Result should even wrap a bare mysqli connection for write queries, or whether iteration over such a Result should be handled differently upstream). Chose not to suppress the mago finding because doing so would make this gap invisible going forward.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    Status
    Todo

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions