Skip to content

[4.x] Keep string keys when iterating a DataSet - #34

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-dataset-string-keys
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-dataset-string-keys

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

DataSet accepts string offsets (constructor, offsetSet()), but key() is declared int|bool|null. Because DataSet.php does not use strict types, PHP coerces every string key to true on return, so foreach (new DataSet(['a' => $obj]) as $k => $v) gives $k === true. Every associative set loses its keys in a foreach.

This widens the return type to int|string|bool|null. Widening a return type is backward compatible for callers. bool is kept on purpose (rather than switching to int|string|null and returning null for "no current element") so that subclasses which override key() with the 4.0.0 signature int|bool|null stay compatible; dropping bool can happen in 5.0.

Testing Instructions

DataSetTest::testIterationKeepsStringKeys() iterates a set keyed 'first', 'second', 3 and expects exactly those keys. Before the change it fails with ['first', 'second', 3] vs [true, true, 3]; after, it passes.

Documentation Changes Required

docs/v3-to-v4-update.md: the key() section described a TypeError; the actual 4.0.0 behaviour is the silent coercion to true. It now says so and that later 4.x releases accept string keys again.

🤖 Generated with Claude Code

DataSet accepts string offsets, but key() was declared int|bool|null.
As the file does not use strict types, every string key was coerced to
true, so foreach over an associative set lost its keys. Allow string in
the return type; bool stays so that subclasses overriding key() with
the 4.0.0 signature remain compatible.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant