Skip to content

Improve type safety with covariant return types in Engine implementations #183

Description

Current Problem

Engine implementations like SqliteEngine declare return types using generic interfaces:

public function makeInsert(): Query\InsertQuery
{
    return new Query\Sqlite\InsertQuery($this);
}

This prevents static analysis tools (PHPStan, Psalm, PhpStorm) from inferring the concrete type, making it impossible to use engine-specific methods like onConflictDoUpdate() without manual type assertions.

Proposed Solution

Use covariant return types (available since PHP 7.4, which is already the minimum required version):

public function makeInsert(): Query\Sqlite\InsertQuery
{
    return new Query\Sqlite\InsertQuery($this);
}

This is backward compatible (covariance doesn't break Liskov substitution) and provides proper type information for static analysis.

I can provide with a PR.

Benefits

  • Better IDE support
  • Safer refactoring
  • No runtime impact
  • No BC break

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions