Skip to content

Declare ArrayHash offset methods in the stub - #227

Merged
ondrejmirtes merged 2 commits into
2.0.xfrom
array-hash-stub-offset-methods
Oct 12, 2026
Merged

ondrejmirtes merged 2 commits into
2.0.xfrom
array-hash-stub-offset-methods

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

The ArrayHash stub from 2.0.14 doesn't declare offsetGet(), so offset access on ArrayHash<T> falls back to ArrayAccess and is T|null. nette/utils 4.x declares @return T. Before 2.0.14, the class wasn't generic, so it wasn't a problem.

The stub now declares the offset methods with the PHPDocs of nette/utils 4.1, so the types match nette/utils' own PHPDoc. The exception is the $key parameter of offsetSet(). @param array-key $key breaks contravariance with ArrayAccess::offsetSet(), which accepts null. PHPStan reports that in stub files as a non-ignorable method.childParameterType error. So offsetSet() keeps accepting a null key, as in 2.0.14.

2.0.14 this PR
$hash['foo'], $hash->offsetGet('foo') on ArrayHash<int> int|null int
$hash['foo'] = 'string' on ArrayHash<int> error error
$hash->offsetGet(1.5) error error

from() and count() stay out of the stub. Their types are the same either way, and declaring them would drop @return static of from() on nette/utils 3.x.

ArrayHashStubTest also never ran the iterator assertions. Composer\InstalledVersions reported the nette/utils bundled in the PHPStan phar (3.2.10), not the installed 4.1.5. The test now checks the native return type of getIterator().

I checked it with nette/utils 4.1.5 and 3.1.4 (--prefer-lowest).

🤖 Generated with Claude Code

ondrejmirtes and others added 2 commits October 12, 2026 09:43
Composer\InstalledVersions also sees the nette/utils bundled in the
PHPStan phar and reported 3.2.10 even with nette/utils 4.1.5 installed,
so the iterator assertions never ran.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Without offsetGet(), offset access on ArrayHash<T> was T|null from
ArrayAccess instead of T as declared by nette/utils.

The PHPDocs mirror nette/utils 4.1, except for the $key parameter of
offsetSet(): array-key is not contravariant with ArrayAccess, which
accepts null.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@ondrejmirtes
ondrejmirtes merged commit b108419 into 2.0.x Oct 12, 2026
45 checks passed
@ondrejmirtes
ondrejmirtes deleted the array-hash-stub-offset-methods branch October 12, 2026 07:46
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