diff --git a/src/Rules/Arrays/OffsetAccessValueAssignmentRule.php b/src/Rules/Arrays/OffsetAccessValueAssignmentRule.php index ea6e39aba97..df3243272f6 100644 --- a/src/Rules/Arrays/OffsetAccessValueAssignmentRule.php +++ b/src/Rules/Arrays/OffsetAccessValueAssignmentRule.php @@ -65,14 +65,28 @@ public function processNode(Node $node, Scope $scope): array $assignedValueType = $scope->getType($node); } + // The offset type (null for an append) lets method-level template types and conditional types of offsetSet() + // (e.g. `@param T[TOffset] $value`) resolve against the written key. An offset that is not accepted is reported + // by OffsetAccessAssignmentRule, so only the value is checked here. + $dimType = $arrayDimFetch->dim !== null ? $scope->getType($arrayDimFetch->dim) : null; + $acceptsValue = static function (Type $varType) use ($dimType, $assignedValueType): bool { + if (!$varType->setOffsetValueType($dimType, $assignedValueType) instanceof ErrorType) { + return true; + } + + // The write is rejected. If the offset alone is accepted, the value is what is rejected. + if (!$varType->setOffsetValueType($dimType, new MixedType()) instanceof ErrorType) { + return false; + } + + return !$varType->setOffsetValueType(new MixedType(), $assignedValueType) instanceof ErrorType; + }; + $arrayTypeResult = $this->ruleLevelHelper->findTypeToCheck( $scope, $arrayDimFetch->var, '', - static function (Type $varType) use ($assignedValueType): bool { - $result = $varType->setOffsetValueType(new MixedType(), $assignedValueType); - return !($result instanceof ErrorType); - }, + $acceptsValue, ); $arrayType = $arrayTypeResult->getType(); if ($arrayType instanceof ErrorType) { @@ -82,8 +96,7 @@ static function (Type $varType) use ($assignedValueType): bool { if (!$isOffsetAccessible->yes()) { return []; } - $resultType = $arrayType->setOffsetValueType(new MixedType(), $assignedValueType); - if (!$resultType instanceof ErrorType) { + if ($acceptsValue($arrayType)) { return []; } diff --git a/src/Type/ObjectType.php b/src/Type/ObjectType.php index 0d68e03dbd0..28917682f2f 100644 --- a/src/Type/ObjectType.php +++ b/src/Type/ObjectType.php @@ -24,6 +24,8 @@ use PHPStan\Reflection\Dummy\DummyPropertyReflection; use PHPStan\Reflection\ExtendedMethodReflection; use PHPStan\Reflection\ExtendedPropertyReflection; +use PHPStan\Reflection\ParametersAcceptor; +use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Reflection\Php\UniversalObjectCratesClassReflectionExtension; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Reflection\ReflectionProviderStaticAccessor; @@ -1594,7 +1596,7 @@ public function hasOffsetValueType(Type $offsetType): TrinaryLogic public function getOffsetValueType(Type $offsetType): Type { if ($this->isInstanceOf(ArrayAccess::class)->yes()) { - return RecursionGuard::run($this, fn (): Type => $this->getMethod('offsetGet', new OutOfClassScope())->getOnlyVariant()->getReturnType()); + return RecursionGuard::run($this, fn (): Type => $this->selectArrayAccessVariant('offsetGet', [$offsetType])->getReturnType()); } if (!$this->isExtraOffsetAccessibleClass()->no()) { @@ -1604,6 +1606,25 @@ public function getOffsetValueType(Type $offsetType): Type return new ErrorType(); } + /** + * The variant of an ArrayAccess method (offsetGet() or offsetSet()) for an offset access, + * with its method-level template types (e.g. `@template TOffset of key-of`) resolved + * against the offset (and written value) types, as for an explicit method call. + * + * @param list $argTypes + */ + private function selectArrayAccessVariant(string $methodName, array $argTypes): ParametersAcceptor + { + $variants = $this->getMethod($methodName, new OutOfClassScope())->getVariants(); + + // Nothing to resolve, so skip the resolution on this hot path. + if (count($variants) === 1 && !ParametersAcceptorSelector::hasAcceptorTemplateOrLateResolvableType($variants[0])) { + return $variants[0]; + } + + return ParametersAcceptorSelector::selectFromTypes($argTypes, $variants, false); + } + public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type { if ($this->isOffsetAccessible()->no()) { @@ -1611,9 +1632,13 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni } if ($this->isInstanceOf(ArrayAccess::class)->yes()) { + if ($offsetType === null) { + $offsetType = new NullType(); + } + $acceptedValueType = new NeverType(); - $acceptedOffsetType = RecursionGuard::run($this, function () use (&$acceptedValueType): Type { - $parameters = $this->getMethod('offsetSet', new OutOfClassScope())->getOnlyVariant()->getParameters(); + $acceptedOffsetType = RecursionGuard::run($this, function () use ($offsetType, $valueType, &$acceptedValueType): Type { + $parameters = $this->selectArrayAccessVariant('offsetSet', [$offsetType, $valueType])->getParameters(); if (count($parameters) < 2) { throw new ShouldNotHappenException(sprintf( 'Method %s::%s() has less than 2 parameters.', @@ -1628,10 +1653,6 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni return $offsetParameter->getType(); }); - if ($offsetType === null) { - $offsetType = new NullType(); - } - if ( (!$offsetType instanceof MixedType && !$acceptedOffsetType->isSuperTypeOf($offsetType)->yes()) || (!$valueType instanceof MixedType && !$acceptedValueType->isSuperTypeOf($valueType)->yes()) diff --git a/tests/PHPStan/Analyser/nsrt/bug-8549.php b/tests/PHPStan/Analyser/nsrt/bug-8549.php new file mode 100644 index 00000000000..0a37a77ef05 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-8549.php @@ -0,0 +1,66 @@ += 8.0 + +namespace Bug8549Types; + +use function PHPStan\Testing\assertType; + +/** + * @template TArray of array + * @extends \ArrayAccess, value-of> + */ +interface Options extends \ArrayAccess +{ + + /** + * @param key-of $offset + */ + public function offsetExists(mixed $offset): bool; + + /** + * @template TOffset of key-of + * @param TOffset $offset + * @return TArray[TOffset] + */ + public function offsetGet(mixed $offset); + + /** + * @template TOffset of key-of + * @param TOffset $offset + * @param TArray[TOffset] $value + */ + public function offsetSet(mixed $offset, mixed $value): void; + + /** + * @template TOffset of key-of + * @param TOffset $offset + */ + public function offsetUnset(mixed $offset): void; + +} + +class Foo +{ + + /** + * @param Options $options + */ + public function read(Options $options): void + { + assertType('string', $options['name']); + assertType('int', $options['age']); + assertType('int', $options['age'] ??= 5); + assertType('string', $options['name'] ??= 'x'); + assertType('int', $options['age'] += 1); + assertType('non-falsy-string', $options['name'] .= 'x'); + } + + /** + * @param Options $options + * @param 'name'|'age' $key + */ + public function unionKey(Options $options, string $key): void + { + assertType('int|string', $options[$key]); + } + +} diff --git a/tests/PHPStan/Rules/Arrays/OffsetAccessAssignOpRuleTest.php b/tests/PHPStan/Rules/Arrays/OffsetAccessAssignOpRuleTest.php index 5bf2e287ea1..8aad94eb013 100644 --- a/tests/PHPStan/Rules/Arrays/OffsetAccessAssignOpRuleTest.php +++ b/tests/PHPStan/Rules/Arrays/OffsetAccessAssignOpRuleTest.php @@ -54,4 +54,11 @@ public function testRuleWithNullsafeVariant(): void $this->analyse([__DIR__ . '/data/offset-access-assignop-nullsafe.php'], []); } + #[RequiresPhp('>= 8.0.0')] + public function testBug8549(): void + { + $this->checkUnions = true; + $this->analyse([__DIR__ . '/data/bug-8549.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Arrays/OffsetAccessAssignmentRuleTest.php b/tests/PHPStan/Rules/Arrays/OffsetAccessAssignmentRuleTest.php index aef7bca9100..e829627c528 100644 --- a/tests/PHPStan/Rules/Arrays/OffsetAccessAssignmentRuleTest.php +++ b/tests/PHPStan/Rules/Arrays/OffsetAccessAssignmentRuleTest.php @@ -235,4 +235,28 @@ public function testAppendToArrayWithPhpIntMaxKey(): void ]); } + #[RequiresPhp('>= 8.0.0')] + public function testBug8549(): void + { + $this->checkUnionTypes = true; + $this->analyse([__DIR__ . '/data/bug-8549.php'], [ + [ + 'Cannot assign offset \'nope\' to Bug8549\Options>.', + 64, + ], + [ + 'Cannot assign new offset to Bug8549\Options>.', + 65, + ], + [ + 'Cannot assign offset 1 to Bug8549\Options>.', + 75, + ], + [ + 'Cannot assign offset \'name\'|\'nope\' to Bug8549\Options>.', + 110, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Arrays/OffsetAccessValueAssignmentRuleTest.php b/tests/PHPStan/Rules/Arrays/OffsetAccessValueAssignmentRuleTest.php index 49d0486b99a..cf0047382f5 100644 --- a/tests/PHPStan/Rules/Arrays/OffsetAccessValueAssignmentRuleTest.php +++ b/tests/PHPStan/Rules/Arrays/OffsetAccessValueAssignmentRuleTest.php @@ -90,6 +90,44 @@ public function testBug10783(): void $this->analyse([__DIR__ . '/../Variables/data/bug-10783.php'], []); } + #[RequiresPhp('>= 8.0.0')] + public function testBug8549(): void + { + $this->analyse([__DIR__ . '/data/bug-8549.php'], [ + [ + 'Bug8549\Options does not accept int.', + 49, + ], + [ + 'Bug8549\Options does not accept string.', + 63, + ], + [ + 'Bug8549\Options does not accept string.', + 92, + ], + [ + 'Bug8549\Options does not accept float.', + 109, + ], + ]); + } + + #[RequiresPhp('>= 8.0.0')] + public function testAppendResolvesOffsetSetAgainstNull(): void + { + $this->analyse([__DIR__ . '/data/offset-access-value-assignment-append.php'], [ + [ + 'OffsetAccessValueAssignmentAppend\Storage does not accept string.', + 35, + ], + [ + 'OffsetAccessValueAssignmentAppend\Storage does not accept int.', + 38, + ], + ]); + } + public function testCoalesceAssign(): void { $this->analyse([__DIR__ . '/data/offset-access-value-assignment-coalesce.php'], [ diff --git a/tests/PHPStan/Rules/Arrays/data/bug-8549.php b/tests/PHPStan/Rules/Arrays/data/bug-8549.php new file mode 100644 index 00000000000..e852babba0a --- /dev/null +++ b/tests/PHPStan/Rules/Arrays/data/bug-8549.php @@ -0,0 +1,113 @@ += 8.0 + +namespace Bug8549; + +// @link https://github.com/symfony/symfony/blob/6.2/src/Symfony/Component/OptionsResolver/Options.php + +/** + * @template TArray of array + * @extends \ArrayAccess, value-of> + */ +interface Options extends \ArrayAccess { + + /** + * @param key-of $offset + */ + public function offsetExists(mixed $offset): bool; + + /** + * @template TOffset of key-of + * @param TOffset $offset + * @return TArray[TOffset] + */ + public function offsetGet(mixed $offset); + + /** + * @template TOffset of key-of + * @param TOffset $offset + * @param TArray[TOffset] $value + */ + public function offsetSet(mixed $offset, mixed $value): void; + + /** + * @template TOffset of key-of + * @param TOffset $offset + */ + public function offsetUnset(mixed $offset): void; + +} + +class HelloWorld +{ + /** + * @param Options $options + */ + public function sayHello(Options $options): void + { + \PHPStan\dumpType($options['name']); + $options['name'] = "foo"; + $options['name'] = 1; + } +} + +class MoreCases +{ + + /** + * @param Options $options + */ + public function shape(Options $options): void + { + $options['name'] = 'foo'; + $options['age'] = 1; + $options['age'] = 'foo'; + $options['nope'] = 'foo'; + $options[] = 'foo'; + } + + /** + * @param Options> $options + */ + public function map(Options $options, mixed $value): void + { + $options['context'] = 'edit'; + $options['_fields'] = $value; + $options[1] = 'edit'; + } + +} + +class CompoundAssignments +{ + + /** + * @param Options $options + */ + public function compound(Options $options): void + { + $options['age'] ??= 5; + $options['name'] ??= 'x'; + $options['age'] += 1; + $options['name'] .= 'x'; + $options['age'] .= 'x'; + } + +} + +class UnionKeys +{ + + /** + * @param Options $options + * @param 'name'|'age' $key + * @param 'name'|'nope' $partlyUnknownKey + */ + public function union(Options $options, string $key, string $partlyUnknownKey): void + { + $options[$key] = 'foo'; + $options[$key] = 1; + $options[$key] = 1.5; + $options[$partlyUnknownKey] = 'foo'; + } + +} diff --git a/tests/PHPStan/Rules/Arrays/data/offset-access-value-assignment-append.php b/tests/PHPStan/Rules/Arrays/data/offset-access-value-assignment-append.php new file mode 100644 index 00000000000..67d6be2d9c3 --- /dev/null +++ b/tests/PHPStan/Rules/Arrays/data/offset-access-value-assignment-append.php @@ -0,0 +1,39 @@ += 8.0 + +namespace OffsetAccessValueAssignmentAppend; + +/** @implements \ArrayAccess */ +final class Storage implements \ArrayAccess +{ + + public function offsetExists(mixed $offset): bool + { + return true; + } + + public function offsetGet(mixed $offset): mixed + { + return 1; + } + + /** + * @param int|null $offset + * @param ($offset is null ? int : string) $value + */ + public function offsetSet(mixed $offset, mixed $value): void + { + } + + public function offsetUnset(mixed $offset): void + { + } + +} + +function doFoo(Storage $storage): void +{ + $storage[] = 'str'; + $storage[] = 1; + $storage[1] = 'str'; + $storage[1] = 1; +} diff --git a/turbo-ext/src/ObjectType.cpp b/turbo-ext/src/ObjectType.cpp index 81c838d06ae..dee4879eae5 100644 --- a/turbo-ext/src/ObjectType.cpp +++ b/turbo-ext/src/ObjectType.cpp @@ -281,6 +281,8 @@ enum pt_ot_callback_kind /* ... ->getIterableKeyType() / ->getIterableValueType() on top */ PT_OTC_METHOD_RETURN_ITERABLE_KEY_TYPE, PT_OTC_METHOD_RETURN_ITERABLE_VALUE_TYPE, + /* offsetGet()'s return type, resolved against [$offsetType] in arg */ + PT_OTC_OFFSET_GET_RETURN_TYPE, /* the offsetSet() parameter types: the first one returned, the second * one into ->out when asked for */ PT_OTC_OFFSET_SET_PARAMETER_TYPE, @@ -1959,8 +1961,10 @@ class ObjectType zend_long arrayAccess = thisIsInstanceOfLiteral(PT_LC("ArrayAccess")); if (UNEXPECTED(arrayAccess < 0)) return zv::Val(); if (arrayAccess == PT_TRI_YES) { - zv::Val offsetGet = zv::Val::string(PT_LC("offsetGet")); - return guardedMethodReturnType(PT_OTC_METHOD_RETURN_TYPE, offsetGet.raw()); + /* [$offsetType], what offsetGet()'s template types resolve against */ + zv::Arr offsetTypes = zv::Arr::create(1); + offsetTypes.push(zv::Ref(offsetType)); + return guarded(PT_OTC_OFFSET_GET_RETURN_TYPE, NULL, offsetTypes.raw(), NULL); } zend_long extra = isExtraOffsetAccessibleClass(); @@ -1982,6 +1986,14 @@ class ObjectType zend_long arrayAccess = thisIsInstanceOfLiteral(PT_LC("ArrayAccess")); if (UNEXPECTED(arrayAccess < 0)) return zv::Val(); if (arrayAccess == PT_TRI_YES) { + zv::Val nullOffset; + if (offsetType == NULL) { + zval nullType; + if (UNEXPECTED(!pt_null_type_new(&nullType))) return zv::Val(); + nullOffset = zv::Val::adopt(nullType); + offsetType = nullOffset.raw(); + } + /* $acceptedValueType = new NeverType(), overwritten by the closure */ zv::Val acceptedValueType = pt_type_new_never_type(); if (UNEXPECTED(acceptedValueType.isUndef())) return zv::Val(); @@ -1989,7 +2001,11 @@ class ObjectType ZVAL_NULL(&none); ZVAL_NEW_REF(&outRef, &none); zv::Val outRefVal = zv::Val::adopt(outRef); - zv::Val acceptedOffsetType = guarded(PT_OTC_OFFSET_SET_PARAMETER_TYPES, NULL, NULL, NULL, outRefVal.raw()); + /* [$offsetType, $valueType], what offsetSet()'s template types resolve against */ + zv::Arr writtenTypes = zv::Arr::create(2); + writtenTypes.push(zv::Ref(offsetType)); + writtenTypes.push(zv::Ref(valueType)); + zv::Val acceptedOffsetType = guarded(PT_OTC_OFFSET_SET_PARAMETER_TYPES, NULL, writtenTypes.raw(), NULL, outRefVal.raw()); if (UNEXPECTED(acceptedOffsetType.isUndef())) return zv::Val(); zval *out = Z_REFVAL_P(outRefVal.raw()); if (Z_TYPE_P(out) == IS_OBJECT) { @@ -2000,14 +2016,6 @@ class ObjectType return zv::Val(); } - zv::Val nullOffset; - if (offsetType == NULL) { - zval nullType; - if (UNEXPECTED(!pt_null_type_new(&nullType))) return zv::Val(); - nullOffset = zv::Val::adopt(nullType); - offsetType = nullOffset.raw(); - } - bool rejected = false; if (!zv::Ref(offsetType).instanceOf(pt_ce_mixed_type)) { zend_long isSuperType = isSuperTypeOfTrinary(acceptedOffsetType.raw(), offsetType); @@ -3371,6 +3379,56 @@ zv::Val pt_object_type_referenced_classes_callback(zval *type) #define PT_THIS ObjectType(Z_OBJ_P(ZEND_THIS)) +/* $type->getMethod($methodName, new OutOfClassScope()); UNDEF = pending + * exception */ +static zv::Val otGetMethodOutOfClass(zend_object *type, zval *methodName) +{ + zv::Val outOfClassScope = pt_type_new(PT_CLASS_OUT_OF_CLASS_SCOPE, 0, NULL); + if (UNEXPECTED(outOfClassScope.isUndef())) return zv::Val(); + zv::Args methodArgs{methodName, outOfClassScope.raw()}; + zv::Val method = (type->ce == pt_ce_object_type || pt_type_method_is(type, PT_LC("getmethod"), otGetMethod)) + ? ObjectType(type).getMethod(methodName, outOfClassScope.raw()) + : pt_type_call(type, PT_LC("getmethod"), 2, methodArgs); + if (UNEXPECTED(method.isUndef())) return zv::Val(); + if (UNEXPECTED(!zv::Ref(method.raw()).isObject())) { + zend_type_error("phpstan_turbo: getMethod() must return an object"); + return zv::Val(); + } + return method; +} + +/* ParametersAcceptorSelector::selectFromTypes($types, $type->getMethod($methodName, new OutOfClassScope())->getVariants(), false): + * the method's variant with its method-level template types resolved + * against the given argument types; UNDEF = pending exception */ +static zv::Val otSelectMethodVariant(zend_object *type, zval *methodName, zval *types) +{ + zv::Val method = otGetMethodOutOfClass(type, methodName); + if (UNEXPECTED(method.isUndef())) return zv::Val(); + zv::Val variants = pt_extended_method_reflection_call(method.raw(), PT_MR_GET_VARIANTS); + if (UNEXPECTED(variants.isUndef())) return zv::Val(); + if (UNEXPECTED(!zv::Ref(variants.raw()).isArray() || !zv::Ref(types).isArray())) { + zend_type_error("phpstan_turbo: getVariants() and the argument types must be arrays"); + return zv::Val(); + } + /* nothing to resolve, so skip the resolution on this hot path: + * if (count($variants) === 1 && !ParametersAcceptorSelector::hasAcceptorTemplateOrLateResolvableType($variants[0])) return $variants[0]; */ + if (zv::ArrRef(variants.raw()).size() == 1) { + zval *only = zend_hash_index_find(Z_ARRVAL_P(variants.raw()), 0); + if (only != NULL && Z_TYPE_P(only) == IS_OBJECT) { + bool hasTemplate; + if (UNEXPECTED(!pt_parameters_acceptor_selector_has_acceptor_template_or_late_resolvable_type(only, hasTemplate))) return zv::Val(); + if (!hasTemplate) return zv::Val::copyOf(zv::Ref(only)); + } + } + zv::Val variant = pt_parameters_acceptor_selector_select_from_types(types, variants.raw(), false); + if (UNEXPECTED(variant.isUndef())) return zv::Val(); + if (UNEXPECTED(!zv::Ref(variant.raw()).isObject())) { + zend_type_error("phpstan_turbo: selectFromTypes() must return an object"); + return zv::Val(); + } + return variant; +} + /* the replay of a pt_ot_callback() closure's captured call (the arguments * are ignored, as the twins' closures ignore them); UNDEF = pending * exception */ @@ -3407,27 +3465,32 @@ static zv::Val otCallbackRun(zval *captures) if (kind == PT_OTC_METHOD_RETURN_ITERABLE_KEY_TYPE) return pt_type_op(Z_OBJ_P(returnType.raw()), PT_OP_GET_ITERABLE_KEY_TYPE, 0, NULL); return pt_type_op(Z_OBJ_P(returnType.raw()), PT_OP_GET_ITERABLE_VALUE_TYPE, 0, NULL); } + case PT_OTC_OFFSET_GET_RETURN_TYPE: { + /* ParametersAcceptorSelector::selectFromTypes([$offsetType], $this->getMethod('offsetGet', new OutOfClassScope())->getVariants(), false)->getReturnType() */ + zv::Val offsetGet = zv::Val::string(PT_LC("offsetGet")); + zv::Val variant = otSelectMethodVariant(Z_OBJ_P(object), offsetGet.raw(), arg); + if (UNEXPECTED(variant.isUndef())) return zv::Val(); + return pt_parameters_acceptor_call(variant.raw(), PT_PA_GET_RETURN_TYPE); + } case PT_OTC_OFFSET_SET_PARAMETER_TYPE: case PT_OTC_OFFSET_SET_PARAMETER_TYPES: { - /* $parameters = $this->getMethod('offsetSet', new OutOfClassScope())->getOnlyVariant()->getParameters() */ - zv::Val outOfClassScope = pt_type_new(PT_CLASS_OUT_OF_CLASS_SCOPE, 0, NULL); - if (UNEXPECTED(outOfClassScope.isUndef())) return zv::Val(); + /* $parameters = $this->getMethod('offsetSet', new OutOfClassScope())->getOnlyVariant()->getParameters(), + * or, for setOffsetValueType(), the variant selected from the written types */ zv::Val offsetSet = zv::Val::string(PT_LC("offsetSet")); - zv::Args methodArgs{offsetSet.raw(), outOfClassScope.raw()}; zend_object *type = Z_OBJ_P(object); - zv::Val method = (type->ce == pt_ce_object_type || pt_type_method_is(type, PT_LC("getmethod"), otGetMethod)) - ? ObjectType(type).getMethod(offsetSet.raw(), outOfClassScope.raw()) - : pt_type_call(type, PT_LC("getmethod"), 2, methodArgs); - if (UNEXPECTED(method.isUndef())) return zv::Val(); - if (UNEXPECTED(!zv::Ref(method.raw()).isObject())) { - zend_type_error("phpstan_turbo: getMethod() must return an object"); - return zv::Val(); - } - zv::Val variant = pt_extended_method_reflection_call(method.raw(), PT_MR_GET_ONLY_VARIANT); - if (UNEXPECTED(variant.isUndef())) return zv::Val(); - if (UNEXPECTED(!zv::Ref(variant.raw()).isObject())) { - zend_type_error("phpstan_turbo: getOnlyVariant() must return an object"); - return zv::Val(); + zv::Val variant; + if (kind == PT_OTC_OFFSET_SET_PARAMETER_TYPES) { + variant = otSelectMethodVariant(type, offsetSet.raw(), arg); + if (UNEXPECTED(variant.isUndef())) return zv::Val(); + } else { + zv::Val method = otGetMethodOutOfClass(type, offsetSet.raw()); + if (UNEXPECTED(method.isUndef())) return zv::Val(); + variant = pt_extended_method_reflection_call(method.raw(), PT_MR_GET_ONLY_VARIANT); + if (UNEXPECTED(variant.isUndef())) return zv::Val(); + if (UNEXPECTED(!zv::Ref(variant.raw()).isObject())) { + zend_type_error("phpstan_turbo: getOnlyVariant() must return an object"); + return zv::Val(); + } } zv::Val parameters = pt_parameters_acceptor_call(variant.raw(), PT_PA_GET_PARAMETERS); if (UNEXPECTED(parameters.isUndef())) return zv::Val(); diff --git a/turbo-ext/src/generated/ObjectType.h b/turbo-ext/src/generated/ObjectType.h index a62e858a1ac..0027c8f3abf 100644 --- a/turbo-ext/src/generated/ObjectType.h +++ b/turbo-ext/src/generated/ObjectType.h @@ -196,52 +196,55 @@ inline constexpr char strings[] = "offsetType\0" /* 2348 */ "hasOffsetValueType\0" /* 2359 */ "getOffsetValueType\0" /* 2378 */ - "valueType\0" /* 2397 */ - "unionValues\0" /* 2407 */ - "true\0" /* 2419 */ - "setOffsetValueType\0" /* 2424 */ - "setExistingOffsetValueType\0" /* 2443 */ - "unsetOffset\0" /* 2470 */ - "getEnumCases\0" /* 2482 */ - "PHPStan\\Type\\Enum\\EnumCaseObjectType\0" /* 2495 */ - "getEnumCaseObject\0" /* 2532 */ - "isCallable\0" /* 2550 */ - "getCallableParametersAcceptors\0" /* 2561 */ - "findCallableParametersAcceptors\0" /* 2592 */ - "isCloneable\0" /* 2624 */ - "isInstanceOf\0" /* 2636 */ - "subtract\0" /* 2649 */ - "getTypeWithoutSubtractedType\0" /* 2658 */ - "withoutFinalByKeywordOverride\0" /* 2687 */ - "changeSubtractedType\0" /* 2717 */ - "subtractedTypes\0" /* 2738 */ - "matchAllowedSubTypes\0" /* 2754 */ - "getSubtractedType\0" /* 2775 */ - "cb\0" /* 2793 */ - "traverse\0" /* 2796 */ - "right\0" /* 2805 */ - "traverseSimultaneously\0" /* 2811 */ - "getNakedClassReflection\0" /* 2834 */ - "getClassReflection\0" /* 2858 */ - "resolvedClassReflection\0" /* 2877 */ - "PHPStan\\Type\\ObjectType\0" /* 2901 */ - "getAncestorWithClassName\0" /* 2925 */ - "getParent\0" /* 2950 */ - "getInterfaces\0" /* 2960 */ - "typeToRemove\0" /* 2974 */ - "tryRemove\0" /* 2987 */ - "getFiniteTypes\0" /* 2997 */ - "exponent\0" /* 3012 */ - "arithmeticPower\0" /* 3021 */ - "PHPStan\\PhpDocParser\\Ast\\Type\\TypeNode\0" /* 3037 */ - "toPhpDocNode\0" /* 3076 */ - "position\0" /* 3089 */ - "PHPStan\\Type\\NativeTypePosition\0" /* 3098 */ - "toNativePhpTypeNode\0" /* 3130 */ - "hasTemplateOrLateResolvableType\0" /* 3150 */ - "hasUnresolvedTemplateArgumentType\0" /* 3182 */ - "PHPStan\\Type\\ValueSet\0" /* 3216 */ - "toValueSet"; /* 3238 */ + "argTypes\0" /* 2397 */ + "PHPStan\\Reflection\\ParametersAcceptor\0" /* 2406 */ + "selectArrayAccessVariant\0" /* 2444 */ + "valueType\0" /* 2469 */ + "unionValues\0" /* 2479 */ + "true\0" /* 2491 */ + "setOffsetValueType\0" /* 2496 */ + "setExistingOffsetValueType\0" /* 2515 */ + "unsetOffset\0" /* 2542 */ + "getEnumCases\0" /* 2554 */ + "PHPStan\\Type\\Enum\\EnumCaseObjectType\0" /* 2567 */ + "getEnumCaseObject\0" /* 2604 */ + "isCallable\0" /* 2622 */ + "getCallableParametersAcceptors\0" /* 2633 */ + "findCallableParametersAcceptors\0" /* 2664 */ + "isCloneable\0" /* 2696 */ + "isInstanceOf\0" /* 2708 */ + "subtract\0" /* 2721 */ + "getTypeWithoutSubtractedType\0" /* 2730 */ + "withoutFinalByKeywordOverride\0" /* 2759 */ + "changeSubtractedType\0" /* 2789 */ + "subtractedTypes\0" /* 2810 */ + "matchAllowedSubTypes\0" /* 2826 */ + "getSubtractedType\0" /* 2847 */ + "cb\0" /* 2865 */ + "traverse\0" /* 2868 */ + "right\0" /* 2877 */ + "traverseSimultaneously\0" /* 2883 */ + "getNakedClassReflection\0" /* 2906 */ + "getClassReflection\0" /* 2930 */ + "resolvedClassReflection\0" /* 2949 */ + "PHPStan\\Type\\ObjectType\0" /* 2973 */ + "getAncestorWithClassName\0" /* 2997 */ + "getParent\0" /* 3022 */ + "getInterfaces\0" /* 3032 */ + "typeToRemove\0" /* 3046 */ + "tryRemove\0" /* 3059 */ + "getFiniteTypes\0" /* 3069 */ + "exponent\0" /* 3084 */ + "arithmeticPower\0" /* 3093 */ + "PHPStan\\PhpDocParser\\Ast\\Type\\TypeNode\0" /* 3109 */ + "toPhpDocNode\0" /* 3148 */ + "position\0" /* 3161 */ + "PHPStan\\Type\\NativeTypePosition\0" /* 3170 */ + "toNativePhpTypeNode\0" /* 3202 */ + "hasTemplateOrLateResolvableType\0" /* 3222 */ + "hasUnresolvedTemplateArgumentType\0" /* 3254 */ + "PHPStan\\Type\\ValueSet\0" /* 3288 */ + "toValueSet"; /* 3310 */ inline constexpr reg::PackedArg args[] = { reg::packed(0, MAY_BE_STRING), /* __construct $className */ reg::packed(10, MAY_BE_NULL, 25, false, false, 43), /* __construct $subtractedType */ @@ -375,17 +378,20 @@ inline constexpr reg::PackedArg args[] = { reg::packed(111, 0, 187), /* hasOffsetValueType return */ reg::packed(2348, 0, 25), /* getOffsetValueType $offsetType */ reg::packed(111, 0, 25), /* getOffsetValueType return */ + reg::packed(1323, MAY_BE_STRING), /* selectArrayAccessVariant $methodName */ + reg::packed(2397, MAY_BE_ARRAY), /* selectArrayAccessVariant $argTypes */ + reg::packed(111, 0, 2406), /* selectArrayAccessVariant return */ reg::packed(2348, MAY_BE_NULL, 25), /* setOffsetValueType $offsetType */ - reg::packed(2397, 0, 25), /* setOffsetValueType $valueType */ - reg::packed(2407, MAY_BE_BOOL, reg::NoString, false, false, 2419), /* setOffsetValueType $unionValues */ + reg::packed(2469, 0, 25), /* setOffsetValueType $valueType */ + reg::packed(2479, MAY_BE_BOOL, reg::NoString, false, false, 2491), /* setOffsetValueType $unionValues */ reg::packed(111, 0, 25), /* setOffsetValueType return */ reg::packed(2348, 0, 25), /* setExistingOffsetValueType $offsetType */ - reg::packed(2397, 0, 25), /* setExistingOffsetValueType $valueType */ + reg::packed(2469, 0, 25), /* setExistingOffsetValueType $valueType */ reg::packed(111, 0, 25), /* setExistingOffsetValueType return */ reg::packed(2348, 0, 25), /* unsetOffset $offsetType */ reg::packed(111, 0, 25), /* unsetOffset return */ reg::packed(111, MAY_BE_ARRAY), /* getEnumCases return */ - reg::packed(111, MAY_BE_NULL, 2495), /* getEnumCaseObject return */ + reg::packed(111, MAY_BE_NULL, 2567), /* getEnumCaseObject return */ reg::packed(111, 0, 187), /* isCallable return */ reg::packed(220, 0, 226), /* getCallableParametersAcceptors $scope */ reg::packed(111, MAY_BE_ARRAY), /* getCallableParametersAcceptors return */ @@ -399,32 +405,32 @@ inline constexpr reg::PackedArg args[] = { reg::packed(111, 0, 25), /* withoutFinalByKeywordOverride return */ reg::packed(10, MAY_BE_NULL, 25), /* changeSubtractedType $subtractedType */ reg::packed(111, 0, 25), /* changeSubtractedType return */ - reg::packed(2738, MAY_BE_ARRAY), /* matchAllowedSubTypes $subtractedTypes */ + reg::packed(2810, MAY_BE_ARRAY), /* matchAllowedSubTypes $subtractedTypes */ reg::packed(111, MAY_BE_NULL, 25), /* matchAllowedSubTypes return */ reg::packed(111, MAY_BE_NULL, 25), /* getSubtractedType return */ - reg::packed(2793, MAY_BE_CALLABLE), /* traverse $cb */ + reg::packed(2865, MAY_BE_CALLABLE), /* traverse $cb */ reg::packed(111, 0, 25), /* traverse return */ - reg::packed(2805, 0, 25), /* traverseSimultaneously $right */ - reg::packed(2793, MAY_BE_CALLABLE), /* traverseSimultaneously $cb */ + reg::packed(2877, 0, 25), /* traverseSimultaneously $right */ + reg::packed(2865, MAY_BE_CALLABLE), /* traverseSimultaneously $cb */ reg::packed(111, 0, 25), /* traverseSimultaneously return */ reg::packed(111, MAY_BE_NULL, 64), /* getNakedClassReflection return */ reg::packed(111, MAY_BE_NULL, 64), /* getClassReflection return */ reg::packed(111, MAY_BE_NULL, 64), /* resolvedClassReflection return */ reg::packed(0, MAY_BE_STRING), /* getAncestorWithClassName $className */ - reg::packed(111, MAY_BE_NULL, 2901), /* getAncestorWithClassName return */ - reg::packed(111, MAY_BE_NULL, 2901), /* getParent return */ + reg::packed(111, MAY_BE_NULL, 2973), /* getAncestorWithClassName return */ + reg::packed(111, MAY_BE_NULL, 2973), /* getParent return */ reg::packed(111, MAY_BE_ARRAY), /* getInterfaces return */ - reg::packed(2974, 0, 25), /* tryRemove $typeToRemove */ + reg::packed(3046, 0, 25), /* tryRemove $typeToRemove */ reg::packed(111, MAY_BE_NULL, 25), /* tryRemove return */ reg::packed(111, MAY_BE_ARRAY), /* getFiniteTypes return */ - reg::packed(3012, 0, 25), /* arithmeticPower $exponent */ + reg::packed(3084, 0, 25), /* arithmeticPower $exponent */ reg::packed(2233, 0, 2244), /* arithmeticPower $phpVersion */ reg::packed(111, 0, 25), /* arithmeticPower return */ - reg::packed(111, 0, 3037), /* toPhpDocNode return */ - reg::packed(3089, 0, 3098), /* toNativePhpTypeNode $position */ + reg::packed(111, 0, 3109), /* toPhpDocNode return */ + reg::packed(3161, 0, 3170), /* toNativePhpTypeNode $position */ reg::packed(111, MAY_BE_BOOL), /* hasTemplateOrLateResolvableType return */ reg::packed(111, MAY_BE_BOOL), /* hasUnresolvedTemplateArgumentType return */ - reg::packed(111, 0, 3216), /* toValueSet return */ + reg::packed(111, 0, 3288), /* toValueSet return */ }; using Sig = reg::Sig; } // namespace sigtab @@ -521,38 +527,39 @@ inline constexpr sigtab::Sig isOffsetAccessible = { { 2309 /* isOffsetAccessible inline constexpr sigtab::Sig isOffsetAccessLegal = { { 2328 /* isOffsetAccessLegal */, 0, 127, 0, 127, ZEND_ACC_PUBLIC } }; inline constexpr sigtab::Sig hasOffsetValueType = { { 2359 /* hasOffsetValueType */, 1, 128, 1, 129, ZEND_ACC_PUBLIC } }; inline constexpr sigtab::Sig getOffsetValueType = { { 2378 /* getOffsetValueType */, 1, 130, 1, 131, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig setOffsetValueType = { { 2424 /* setOffsetValueType */, 2, 132, 3, 135, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig setExistingOffsetValueType = { { 2443 /* setExistingOffsetValueType */, 2, 136, 2, 138, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig unsetOffset = { { 2470 /* unsetOffset */, 1, 139, 1, 140, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig getEnumCases = { { 2482 /* getEnumCases */, 0, 141, 0, 141, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig getEnumCaseObject = { { 2532 /* getEnumCaseObject */, 0, 142, 0, 142, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig isCallable = { { 2550 /* isCallable */, 0, 143, 0, 143, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig getCallableParametersAcceptors = { { 2561 /* getCallableParametersAcceptors */, 1, 144, 1, 145, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig findCallableParametersAcceptors = { { 2592 /* findCallableParametersAcceptors */, 0, 146, 0, 146, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig isCloneable = { { 2624 /* isCloneable */, 0, 147, 0, 147, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig isInstanceOf = { { 2636 /* isInstanceOf */, 1, 148, 1, 149, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig subtract = { { 2649 /* subtract */, 1, 150, 1, 151, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig getTypeWithoutSubtractedType = { { 2658 /* getTypeWithoutSubtractedType */, 0, 152, 0, 152, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig withoutFinalByKeywordOverride = { { 2687 /* withoutFinalByKeywordOverride */, 0, 153, 0, 153, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig changeSubtractedType = { { 2717 /* changeSubtractedType */, 1, 154, 1, 155, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig matchAllowedSubTypes = { { 2754 /* matchAllowedSubTypes */, 1, 156, 1, 157, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig getSubtractedType = { { 2775 /* getSubtractedType */, 0, 158, 0, 158, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig traverse = { { 2796 /* traverse */, 1, 159, 1, 160, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig traverseSimultaneously = { { 2811 /* traverseSimultaneously */, 2, 161, 2, 163, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig getNakedClassReflection = { { 2834 /* getNakedClassReflection */, 0, 164, 0, 164, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig getClassReflection = { { 2858 /* getClassReflection */, 0, 165, 0, 165, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig resolvedClassReflection = { { 2877 /* resolvedClassReflection */, 0, 166, 0, 166, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig getAncestorWithClassName = { { 2925 /* getAncestorWithClassName */, 1, 167, 1, 168, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig getParent = { { 2950 /* getParent */, 0, 169, 0, 169, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig getInterfaces = { { 2960 /* getInterfaces */, 0, 170, 0, 170, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig tryRemove = { { 2987 /* tryRemove */, 1, 171, 1, 172, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig getFiniteTypes = { { 2997 /* getFiniteTypes */, 0, 173, 0, 173, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig arithmeticPower = { { 3021 /* arithmeticPower */, 2, 174, 2, 176, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig toPhpDocNode = { { 3076 /* toPhpDocNode */, 0, 177, 0, 177, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig toNativePhpTypeNode = { { 3130 /* toNativePhpTypeNode */, 1, 178, 1, reg::NoArg, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig hasTemplateOrLateResolvableType = { { 3150 /* hasTemplateOrLateResolvableType */, 0, 179, 0, 179, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig hasUnresolvedTemplateArgumentType = { { 3182 /* hasUnresolvedTemplateArgumentType */, 0, 180, 0, 180, ZEND_ACC_PUBLIC } }; -inline constexpr sigtab::Sig toValueSet = { { 3238 /* toValueSet */, 0, 181, 0, 181, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig selectArrayAccessVariant = { { 2444 /* selectArrayAccessVariant */, 2, 132, 2, 134, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig setOffsetValueType = { { 2496 /* setOffsetValueType */, 2, 135, 3, 138, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig setExistingOffsetValueType = { { 2515 /* setExistingOffsetValueType */, 2, 139, 2, 141, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig unsetOffset = { { 2542 /* unsetOffset */, 1, 142, 1, 143, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig getEnumCases = { { 2554 /* getEnumCases */, 0, 144, 0, 144, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig getEnumCaseObject = { { 2604 /* getEnumCaseObject */, 0, 145, 0, 145, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig isCallable = { { 2622 /* isCallable */, 0, 146, 0, 146, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig getCallableParametersAcceptors = { { 2633 /* getCallableParametersAcceptors */, 1, 147, 1, 148, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig findCallableParametersAcceptors = { { 2664 /* findCallableParametersAcceptors */, 0, 149, 0, 149, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig isCloneable = { { 2696 /* isCloneable */, 0, 150, 0, 150, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig isInstanceOf = { { 2708 /* isInstanceOf */, 1, 151, 1, 152, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig subtract = { { 2721 /* subtract */, 1, 153, 1, 154, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig getTypeWithoutSubtractedType = { { 2730 /* getTypeWithoutSubtractedType */, 0, 155, 0, 155, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig withoutFinalByKeywordOverride = { { 2759 /* withoutFinalByKeywordOverride */, 0, 156, 0, 156, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig changeSubtractedType = { { 2789 /* changeSubtractedType */, 1, 157, 1, 158, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig matchAllowedSubTypes = { { 2826 /* matchAllowedSubTypes */, 1, 159, 1, 160, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig getSubtractedType = { { 2847 /* getSubtractedType */, 0, 161, 0, 161, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig traverse = { { 2868 /* traverse */, 1, 162, 1, 163, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig traverseSimultaneously = { { 2883 /* traverseSimultaneously */, 2, 164, 2, 166, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig getNakedClassReflection = { { 2906 /* getNakedClassReflection */, 0, 167, 0, 167, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig getClassReflection = { { 2930 /* getClassReflection */, 0, 168, 0, 168, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig resolvedClassReflection = { { 2949 /* resolvedClassReflection */, 0, 169, 0, 169, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig getAncestorWithClassName = { { 2997 /* getAncestorWithClassName */, 1, 170, 1, 171, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig getParent = { { 3022 /* getParent */, 0, 172, 0, 172, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig getInterfaces = { { 3032 /* getInterfaces */, 0, 173, 0, 173, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig tryRemove = { { 3059 /* tryRemove */, 1, 174, 1, 175, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig getFiniteTypes = { { 3069 /* getFiniteTypes */, 0, 176, 0, 176, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig arithmeticPower = { { 3093 /* arithmeticPower */, 2, 177, 2, 179, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig toPhpDocNode = { { 3148 /* toPhpDocNode */, 0, 180, 0, 180, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig toNativePhpTypeNode = { { 3202 /* toNativePhpTypeNode */, 1, 181, 1, reg::NoArg, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig hasTemplateOrLateResolvableType = { { 3222 /* hasTemplateOrLateResolvableType */, 0, 182, 0, 182, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig hasUnresolvedTemplateArgumentType = { { 3254 /* hasUnresolvedTemplateArgumentType */, 0, 183, 0, 183, ZEND_ACC_PUBLIC } }; +inline constexpr sigtab::Sig toValueSet = { { 3310 /* toValueSet */, 0, 184, 0, 184, ZEND_ACC_PUBLIC } }; } // namespace sig } // namespace ptdecl::ObjectType diff --git a/turbo-ext/tests/type-family-array-access-fixture.php b/turbo-ext/tests/type-family-array-access-fixture.php new file mode 100644 index 00000000000..7d27b1d5c0c --- /dev/null +++ b/turbo-ext/tests/type-family-array-access-fixture.php @@ -0,0 +1,95 @@ +, value-of> + */ +final class TemplatedArrayAccessFixture implements \ArrayAccess +{ + + /** + * @param key-of $offset + */ + #[\ReturnTypeWillChange] + public function offsetExists($offset) + { + return true; + } + + /** + * @template TOffset of key-of + * @param TOffset $offset + * @return TArray[TOffset] + */ + #[\ReturnTypeWillChange] + public function offsetGet($offset) + { + throw new \LogicException(); + } + + /** + * @template TOffset of key-of + * @param TOffset $offset + * @param TArray[TOffset] $value + */ + #[\ReturnTypeWillChange] + public function offsetSet($offset, $value) + { + } + + /** + * @template TOffset of key-of + * @param TOffset $offset + */ + #[\ReturnTypeWillChange] + public function offsetUnset($offset) + { + } + +} + +/** + * offsetSet() with a value type conditional on the offset: an append + * resolves it against a null offset. + * + * @implements \ArrayAccess + */ +final class ConditionalArrayAccessFixture implements \ArrayAccess +{ + + #[\ReturnTypeWillChange] + public function offsetExists($offset) + { + return true; + } + + /** + * @return ($offset is null ? int : string) + */ + #[\ReturnTypeWillChange] + public function offsetGet($offset) + { + throw new \LogicException(); + } + + /** + * @param int|null $offset + * @param ($offset is null ? int : string) $value + */ + #[\ReturnTypeWillChange] + public function offsetSet($offset, $value) + { + } + + #[\ReturnTypeWillChange] + public function offsetUnset($offset) + { + } + +} diff --git a/turbo-ext/tests/type-family.php b/turbo-ext/tests/type-family.php index 81cd03ed884..e66efc88c5a 100644 --- a/turbo-ext/tests/type-family.php +++ b/turbo-ext/tests/type-family.php @@ -2746,6 +2746,51 @@ public function getSubtractedType(): ?\PHPStan\Type\Type $r['after reset getMethod'] = $attempt(static fn () => $subjects['trinary']->getMethod('yes', $outOfClassScope)); $r['after reset getEnumCases'] = $view($subjects['enum']->getEnumCases()); $r['after reset getAncestorWithClassName'] = $view((new $objectClass(\ArrayIterator::class))->getAncestorWithClassName(\Traversable::class)); + // offsetGet()/offsetSet() variants resolved against the offset and the + // written value: method-level template types (TOffset of key-of, + // TArray[TOffset]) and a conditional type on the offset, the fast path + // of a variant with nothing to resolve (ArrayObject) alongside + require_once __DIR__ . '/type-family-array-access-fixture.php'; + $nameKey = new \PHPStan\Type\Constant\ConstantStringType('name'); + $ageKey = new \PHPStan\Type\Constant\ConstantStringType('age'); + $shapeBuilder = \PHPStan\Type\Constant\ConstantArrayTypeBuilder::createEmpty(); + $shapeBuilder->setOffsetValueType($nameKey, new \PHPStan\Type\StringType()); + $shapeBuilder->setOffsetValueType($ageKey, new \PHPStan\Type\IntegerType()); + $arrayAccessSubjects = [ + 'templatedShape' => new $genericClass(\PHPStanTurboTests\TemplatedArrayAccessFixture::class, [$shapeBuilder->getArray()]), + 'templatedMap' => new $genericClass(\PHPStanTurboTests\TemplatedArrayAccessFixture::class, [new \PHPStan\Type\ArrayType(new \PHPStan\Type\StringType(), new \PHPStan\Type\MixedType())]), + 'templatedRaw' => new $objectClass(\PHPStanTurboTests\TemplatedArrayAccessFixture::class), + 'conditional' => new $objectClass(\PHPStanTurboTests\ConditionalArrayAccessFixture::class), + 'arrayObject' => new $genericClass(\ArrayObject::class, [new \PHPStan\Type\StringType(), new \PHPStan\Type\IntegerType()]), + ]; + $arrayAccessOffsets = [ + 'name' => $nameKey, + 'age' => $ageKey, + 'nope' => new \PHPStan\Type\Constant\ConstantStringType('nope'), + 'nameOrAge' => new \PHPStan\Type\UnionType([$nameKey, $ageKey]), + 'string' => new \PHPStan\Type\StringType(), + 'int1' => new \PHPStan\Type\Constant\ConstantIntegerType(1), + 'null' => new \PHPStan\Type\NullType(), + 'mixed' => new \PHPStan\Type\MixedType(), + ]; + $arrayAccessValues = [ + 'x' => new \PHPStan\Type\Constant\ConstantStringType('x'), + '1' => new \PHPStan\Type\Constant\ConstantIntegerType(1), + 'float' => new \PHPStan\Type\FloatType(), + 'mixed' => new \PHPStan\Type\MixedType(), + ]; + foreach ($arrayAccessSubjects as $subjectName => $subject) { + foreach ($arrayAccessOffsets as $offsetName => $offset) { + $r["array access $subjectName getOffsetValueType $offsetName"] = $attempt(static fn () => $subject->getOffsetValueType($offset)); + $r["array access $subjectName hasOffsetValueType $offsetName"] = $attempt(static fn () => $subject->hasOffsetValueType($offset)); + foreach ($arrayAccessValues as $valueName => $value) { + $r["array access $subjectName setOffsetValueType $offsetName $valueName"] = $attempt(static fn () => $subject->setOffsetValueType($offset, $value)); + } + } + foreach ($arrayAccessValues as $valueName => $value) { + $r["array access $subjectName setOffsetValueType append $valueName"] = $attempt(static fn () => $subject->setOffsetValueType(null, $value)); + } + } foreach ($r as $key => $value) { $observations["object $key"] = $value; }