Skip to content

Make AbstractSql render state private so subclasses can't corrupt it #246

Open

Description

@simon-mundy

AbstractSql::$processInfo and AbstractSql::$instanceParameterIndex are protected. They hold the parameter-naming state used while rendering: paramPrefix, subselectCount and, after #236, expressionCount. Any subclass, including driver platform decorators, can therefore write to them, and a bad write would produce clashing or unstable parameter names.

Current readers outside AbstractSql

  • PhpDb\Sql\Select::processLimit() and processOffset() read $this->processInfo['paramPrefix'].
  • PhpDb\Mysql\Sql\SelectDecorator::processLimit() and processOffset() do the same.
  • PhpDb\Sqlite\Sql\SelectDecorator::processLimit() and processOffset() do the same.
  • PhpDb\Sqlite SelectDecoratorTest sets processInfo through reflection.

Nothing outside AbstractSql uses $instanceParameterIndex.

Proposal

  1. Make both properties private. The cross-instance writes in processSubSelect() and the get_object_vars() copy in localizeVariables() keep working, because both run in AbstractSql scope.
  2. Add a read-only getter for subclasses:
    final protected function getParamPrefix(): string
    Then switch Select over to it.
  3. Make resetRenderState() (added in Render the same parameter names every time a statement is rendered #236) final protected. An override that doesn't call the parent would bring back the stale-state bug. It can't be private because AbstractPreparableSql::prepareStatement() calls it.

processExpression(), processSubSelect() and resolveColumnValue() don't need to be final. Once the state is private, an override can't corrupt it.

Downstream

This is a BC break for phpdb-mysql and phpdb-sqlite. Each needs a follow-up PR that switches to getParamPrefix() and raises its minimum phpdb version. The sqlite PR also needs to replace the reflection in its test. Those PRs land after the phpdb release.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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