diff --git a/stubs/Utils/ArrayHash.stub b/stubs/Utils/ArrayHash.stub index cff2bc7..9c4faf9 100644 --- a/stubs/Utils/ArrayHash.stub +++ b/stubs/Utils/ArrayHash.stub @@ -7,7 +7,7 @@ namespace Nette\Utils; * @implements \IteratorAggregate * @implements \ArrayAccess */ -class ArrayHash extends \stdClass implements \ArrayAccess, \IteratorAggregate +class ArrayHash extends \stdClass implements \ArrayAccess, \Countable, \IteratorAggregate { /** @@ -18,4 +18,37 @@ class ArrayHash extends \stdClass implements \ArrayAccess, \IteratorAggregate // nothing } + /** + * @param T $value + */ + public function offsetSet($key, $value): void + { + // nothing + } + + /** + * @param array-key $key + * @return T + */ + public function offsetGet($key): mixed + { + // nothing + } + + /** + * @param array-key $key + */ + public function offsetExists($key): bool + { + // nothing + } + + /** + * @param array-key $key + */ + public function offsetUnset($key): void + { + // nothing + } + } diff --git a/tests/Type/Nette/ArrayHashStubTest.php b/tests/Type/Nette/ArrayHashStubTest.php index 0c5d19d..7fa06a6 100644 --- a/tests/Type/Nette/ArrayHashStubTest.php +++ b/tests/Type/Nette/ArrayHashStubTest.php @@ -2,11 +2,11 @@ namespace PHPStan\Type\Nette; -use Composer\InstalledVersions; -use OutOfBoundsException; +use Iterator; +use Nette\Utils\ArrayHash; use PHPStan\Testing\TypeInferenceTestCase; -use function class_exists; -use function version_compare; +use ReflectionMethod; +use ReflectionNamedType; final class ArrayHashStubTest extends TypeInferenceTestCase { @@ -15,16 +15,9 @@ public static function dataFileAsserts(): iterable { yield from self::gatherAssertTypes(__DIR__ . '/data/array-hash.php'); - try { - $utilsVersion = class_exists(InstalledVersions::class) - ? InstalledVersions::getVersion('nette/utils') - : null; - } catch (OutOfBoundsException $e) { - $utilsVersion = null; - } - // nette/utils 4.0+ declares getIterator(): \Iterator, older versions \RecursiveArrayIterator - if ($utilsVersion === null || version_compare($utilsVersion, '4.0.0', '<')) { + $iteratorReturnType = (new ReflectionMethod(ArrayHash::class, 'getIterator'))->getReturnType(); + if (!$iteratorReturnType instanceof ReflectionNamedType || $iteratorReturnType->getName() !== Iterator::class) { return; } diff --git a/tests/Type/Nette/data/array-hash.php b/tests/Type/Nette/data/array-hash.php index c15d257..875c697 100644 --- a/tests/Type/Nette/data/array-hash.php +++ b/tests/Type/Nette/data/array-hash.php @@ -11,6 +11,7 @@ function test(ArrayHash $hash): void { assertType('Nette\\Utils\\ArrayHash', $hash); - assertType('int|null', $hash['foo']); + assertType('int', $hash['foo']); + assertType('int', $hash->offsetGet('foo')); assertType('mixed', $hash->foo); }