Skip to content

Widen ConnectionInterface::getResource() to resource|object|null - #227

Merged
tyrsson merged 2 commits into
0.6.xfrom
tyrsson-fix-resource-contract
Oct 8, 2026
Merged

tyrsson merged 2 commits into
0.6.xfrom
tyrsson-fix-resource-contract

Conversation

@tyrsson

@tyrsson tyrsson commented Oct 7, 2026

Copy link
Copy Markdown
Member

What

ConnectionInterface::getResource() declares @return resource, but no implementation can honour it. AbstractConnection::getResource() is annotated @return resource|null, and AbstractConnection::disconnect() assigns $this->resource = null, so null is a legitimate and reachable return value. The interface's @return resource was simply wrong about its own contract, and the mismatch was already live on 0.6.x.

Two changes:

  1. ConnectionInterface::getResource() -> @return resource|object|null.
    Returns are covariant, so widening the interface legalises the narrower @return resource|null already declared by AbstractConnection and Pdo\AbstractPdoConnection. The object arm is required because mysqli has been an object since PHP 8.1 -- the mysqli driver's connection is not a resource.
  2. AbstractConnection::$resource -- drop the @var mixed docblock.
    It was redundant (mixed is the implicit type) and actively harmful: it forced subclasses that declare their real type to be reported as incompatible-property-type. The prose "Extending classes must be covariant" is kept as a comment.

No native types are added, so runtime behaviour is unchanged.

Effect on the baseline

Baselines are generated by mago, never hand-edited -- these were pruned with mago analyze --remove-outdated-baseline-entries. analyzer-baseline.toml goes 716 -> 713 blocks; lint-baseline.toml is unchanged.

Three findings are un-suppressed because they no longer exist:

File Code Was
src/Adapter/Driver/AbstractConnection.php incompatible-return-type null|resource vs parent resource
src/Adapter/Driver/AbstractConnection.php incompatible-return-type same, for Pdo\AbstractPdoConnection::getresource()
src/Adapter/Driver/Pdo/AbstractPdoConnection.php incompatible-property-type $resource docblock incompatible with parent

Nothing else was dropped -- verified by diffing the baseline before and after.

missing-property-type and mixed-return-statement on AbstractConnection::$resource deliberately remain: the property is genuinely still untyped, and giving it a real shape is a separate decision.

Gates

mago format --check, mago lint and mago analyze are all clean (no guard config in this repo). vendor/bin/phpunit --testsuite "unit test" -- 2089 tests, 4038 assertions, OK.

Consumer verification

Verified against phpdb-mysql by temporarily aliasing this clone in as a Composer path repository (junction + mago path), then reverting the scaffolding. Measured A/B against the same phpdb commit so the delta could not confound the result:

  • 0 new findings in phpdb-mysql.
  • Exactly 3 of phpdb-mysql's baselined entries become outdated -- the inherited $resource contract findings this change clears.
  • phpdb-mysql's 193 unit tests pass against this branch.

A fourth phpdb-mysql entry (SelectDecorator::$specifications) is also reported as outdated when resolved against the current 0.6.x tip, but it is unrelated to this change and is not pruned here.

Follow-up

phpdb-mysql's composer.lock still pins b57f549, which is 270 commits behind 0.6.x. Its CI mago job installs from that lock, so the phpdb-mysql side of this contract change will be pruned once that lock is synced.

@codecov

codecov Bot commented Oct 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

mago 1.52.0 changed method-name casing in non-existent-method messages (getlastgeneratedvalue -> getLastGeneratedValue, getsqlstring -> getSqlString, preparestatement -> prepareStatement), so the 11 baselined entries for Pdo/AbstractPdo.php and Sql/Platform/* stopped matching and resurfaced.

Regenerated with mago analyze --generate-baseline under 1.52.0. The diff is exactly 11 message lines: no entries added, none removed, no findings changed.
@tyrsson tyrsson self-assigned this Oct 8, 2026
@tyrsson tyrsson added qa Improvements in quality assurance of the project next minor Target next minor release. labels Oct 8, 2026
@tyrsson tyrsson added this to the 0.6.0 milestone Oct 8, 2026
@tyrsson
tyrsson merged commit 0c4ed09 into 0.6.x Oct 8, 2026
29 checks passed
@tyrsson
tyrsson deleted the tyrsson-fix-resource-contract branch October 8, 2026 01:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

next minor Target next minor release. qa Improvements in quality assurance of the project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants