Skip to content

[ds] Fix incorrect numeric string key caution on Ds\Map pages - #54

Open
officialasishkumar wants to merge 1 commit into
php:mainfrom
officialasishkumar:fix/ds-map-numeric-string-keys
Open

officialasishkumar wants to merge 1 commit into
php:mainfrom
officialasishkumar:fix/ds-map-numeric-string-keys

Conversation

@officialasishkumar

Copy link
Copy Markdown

The Ds\Map::get(), Ds\Map::put() and Ds\Map::remove() pages had a caution saying that, with array syntax, scalar keys are coerced to integers, so $map["1"] would access int(1). That is not true. Ds\Map implements the dimension handlers natively and receives the offset unchanged. The engine doesn't cast numeric string offsets for objects either, so the userland polyfill (ArrayAccess) behaves the same way.

I checked this against ext-ds built from master on PHP 8.5:

$map = new \Ds\Map();
$map->put("1", "string-one");
$map->put(1, "int-one");
var_dump($map->count());        // int(2)
var_dump($map["1"], $map[1]);   // "string-one", "int-one"
$map["2"] = "s2";
var_dump($map->hasKey("2"), $map->hasKey(2)); // true, false
unset($map["1"]);
var_dump($map->hasKey("1"), $map->hasKey(1)); // false, true

ext-ds requires PHP 8.2+, so the caution doesn't apply to any supported version.

Changes:

  • Replace the caution with a note describing the real behaviour. It differs from arrays, where such keys are cast to integers, so it's worth stating.
  • Fix the array syntax note on the remove() page. It showed reading a value ($map["key"]) instead of removing it (unset($map["key"])).

Validation: php doc-base/configure.php --disable-libxml-check --enable-xml-details --redirect-stderr-to-stdout --with-base-lang=extensions; docbook-cs on the diff; rendered the three pages with PhD and checked the output.

Fixes #22

The Ds\Map::get(), Ds\Map::put() and Ds\Map::remove() pages warned that
when using array syntax, scalar keys are coerced to integers by the
engine, so that $map["1"] would access int(1). This is not the case:
Ds\Map implements the dimension handlers natively and receives the
offset unchanged, and the engine does not cast numeric string offsets
for objects either (the same holds for the userland polyfill, which
implements ArrayAccess). $map["1"] and $map->get("1") both look up the
string key "1", and $map["1"] and $map[1] are two different keys.

Verified against ext-ds built from master on PHP 8.5: reading, writing,
isset() and unset() with array syntax all keep "1" and 1 distinct.
ext-ds requires PHP 8.2 or later, so the warning does not apply to any
supported version.

Replace the caution with a note stating the actual behavior, since
it differs from arrays, where such keys are cast to integers. Also
correct the array syntax note on the remove() page, which showed
reading a value ($map["key"]) instead of removing it
(unset($map["key"])).

Fixes php#22
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.

PECL Ds\Map numeric string key warnings are incorrect

1 participant