Skip to content

Commit d015636

Browse files
ondrejmirtesclaude
andcommitted
Raise TypeErrors for non-Type arguments of native traverse callbacks
The native TypeTraverser::map() callbacks of TypeCombinator and of the called-on-type member prototypes reported a non-object $type argument as an ArgumentCountError, which the twins' `Type $type` closure parameters raise as a TypeError; the argument count and the type are now checked separately. countConstantArrayValueTypes() also checks each element the way the twin's TypeTraverser::map(Type $type, ...) call does, so a scalar or a non-Type object element is that TypeError instead of reaching the callback (a scalar) or failing with an unrelated Error (an object). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent 736e0cc commit d015636

4 files changed

Lines changed: 46 additions & 15 deletions

File tree

‎turbo-ext/src/CalledOnTypeUnresolvedMethodPrototypeReflection.cpp‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,14 @@ class CalledOnTypeUnresolvedMethodPrototypeReflection
158158
static void transformStaticTypeCallback(zval *thisZv, zval *state1, uint32_t argc, zval *argv, zval *return_value)
159159
{
160160
(void) state1;
161-
if (UNEXPECTED(argc < 1 || Z_TYPE(argv[0]) != IS_OBJECT)) {
161+
if (UNEXPECTED(argc < 1)) {
162162
zend_argument_count_error("Too few arguments to function %s::{closure}(), %u passed and exactly 1 expected", ZSTR_VAL(pt_ce_called_on_type_unresolved_method_prototype_reflection->name), argc);
163163
return;
164164
}
165+
if (UNEXPECTED(Z_TYPE(argv[0]) != IS_OBJECT)) {
166+
zend_type_error("%s::{closure}(): Argument #1 ($type) must be of type %s, %s given", ZSTR_VAL(pt_ce_called_on_type_unresolved_method_prototype_reflection->name), ptcls::type, zend_zval_value_name(&argv[0]));
167+
return;
168+
}
165169
zv::Val result = transformStaticType(Z_OBJ_P(thisZv), &argv[0]);
166170
if (UNEXPECTED(result.isUndef())) return;
167171
result.intoReturnValue(return_value);
@@ -175,10 +179,14 @@ class CalledOnTypeUnresolvedMethodPrototypeReflection
175179
static void traverse(zval *thisZv, zval *state1, uint32_t argc, zval *argv, zval *return_value)
176180
{
177181
(void) state1;
178-
if (UNEXPECTED(argc < 2 || Z_TYPE(argv[0]) != IS_OBJECT)) {
182+
if (UNEXPECTED(argc < 2)) {
179183
zend_argument_count_error("Too few arguments to function %s::{closure}(), %u passed and exactly 2 expected", ZSTR_VAL(pt_ce_called_on_type_unresolved_method_prototype_reflection->name), argc);
180184
return;
181185
}
186+
if (UNEXPECTED(Z_TYPE(argv[0]) != IS_OBJECT)) {
187+
zend_type_error("%s::{closure}(): Argument #1 ($type) must be of type %s, %s given", ZSTR_VAL(pt_ce_called_on_type_unresolved_method_prototype_reflection->name), ptcls::type, zend_zval_value_name(&argv[0]));
188+
return;
189+
}
182190
CalledOnTypeUnresolvedMethodPrototypeReflection self(Z_OBJ_P(thisZv));
183191
zval *type = &argv[0];
184192
bool isGenericStatic;

‎turbo-ext/src/CalledOnTypeUnresolvedPropertyPrototypeReflection.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,14 @@ class CalledOnTypeUnresolvedPropertyPrototypeReflection
149149
static void traverse(zval *thisZv, zval *state1, uint32_t argc, zval *argv, zval *return_value)
150150
{
151151
(void) state1;
152-
if (UNEXPECTED(argc < 2 || Z_TYPE(argv[0]) != IS_OBJECT)) {
152+
if (UNEXPECTED(argc < 2)) {
153153
zend_argument_count_error("Too few arguments to function %s::{closure}(), %u passed and exactly 2 expected", ZSTR_VAL(pt_ce_called_on_type_unresolved_property_prototype_reflection->name), argc);
154154
return;
155155
}
156+
if (UNEXPECTED(Z_TYPE(argv[0]) != IS_OBJECT)) {
157+
zend_type_error("%s::{closure}(): Argument #1 ($type) must be of type %s, %s given", ZSTR_VAL(pt_ce_called_on_type_unresolved_property_prototype_reflection->name), ptcls::type, zend_zval_value_name(&argv[0]));
158+
return;
159+
}
156160
zval *type = &argv[0];
157161
bool isStatic;
158162
pt_type_instanceof_ce(type, pt_ce_static_type, isStatic);

‎turbo-ext/src/TypeCombinator.cpp‎

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,17 +1943,31 @@ class TypeCombinator
19431943
return zv::Val(std::move(result));
19441944
}
19451945

1946+
/* the twin's `static function (Type $type, callable $traverse)`
1947+
* TypeTraverser::map() callbacks: too few arguments are an
1948+
* ArgumentCountError, a non-object $type the parameter's TypeError;
1949+
* false with the error pending */
1950+
static bool checkTraverseCallbackArguments(uint32_t argc, zval *argv)
1951+
{
1952+
if (UNEXPECTED(argc < 2)) {
1953+
zend_argument_count_error("Too few arguments to function %s::{closure}(), %u passed and exactly 2 expected", ZSTR_VAL(pt_ce_type_combinator->name), argc);
1954+
return false;
1955+
}
1956+
if (UNEXPECTED(Z_TYPE_P(&argv[0]) != IS_OBJECT)) {
1957+
zend_type_error("%s::{closure}(): Argument #1 ($type) must be of type %s, %s given", ZSTR_VAL(pt_ce_type_combinator->name), ptcls::type, zend_zval_value_name(&argv[0]));
1958+
return false;
1959+
}
1960+
return true;
1961+
}
1962+
19461963
/* the body of optimizeConstantArrays()'s TypeTraverser::map() callback:
19471964
* a non-empty ConstantArrayType generalized to an oversized array over
19481965
* the union of its (generalized) keys and values, `use (&$isOversized)`
19491966
* in the holder's first state slot */
19501967
static void generalizeOversizedCallback(zval *isOversized, zval *state1, uint32_t argc, zval *argv, zval *return_value)
19511968
{
19521969
(void) state1;
1953-
if (UNEXPECTED(argc < 2 || Z_TYPE_P(&argv[0]) != IS_OBJECT)) {
1954-
zend_wrong_parameters_count_error(2, 2);
1955-
return;
1956-
}
1970+
if (UNEXPECTED(!checkTraverseCallbackArguments(argc, argv))) return;
19571971
zval *type = &argv[0];
19581972
if (!isInstance(type, pt_ce_constant_array_type)) {
19591973
zv::Val traversed = pt_type_call_callable(&argv[1], 1, type);
@@ -2070,10 +2084,7 @@ class TypeCombinator
20702084
{
20712085
(void) state0;
20722086
(void) state1;
2073-
if (UNEXPECTED(argc < 2 || Z_TYPE_P(&argv[0]) != IS_OBJECT)) {
2074-
zend_wrong_parameters_count_error(2, 2);
2075-
return;
2076-
}
2087+
if (UNEXPECTED(!checkTraverseCallbackArguments(argc, argv))) return;
20772088
zval *type = &argv[0];
20782089
if (isInstance(type, pt_ce_constant_array_type)) {
20792090
zend_long atLeastOnce = callTrinary(type, PT_LC("isiterableatleastonce"));
@@ -2293,10 +2304,7 @@ class TypeCombinator
22932304
static void countValueTypesCallback(zval *count, zval *state1, uint32_t argc, zval *argv, zval *return_value)
22942305
{
22952306
(void) state1;
2296-
if (UNEXPECTED(argc < 2 || Z_TYPE_P(&argv[0]) != IS_OBJECT)) {
2297-
zend_wrong_parameters_count_error(2, 2);
2298-
return;
2299-
}
2307+
if (UNEXPECTED(!checkTraverseCallbackArguments(argc, argv))) return;
23002308
zval *type = &argv[0];
23012309
if (isInstance(type, pt_ce_constant_array_type)) {
23022310
zv::Val valueTypes = call(type, PT_LC("getvaluetypes"));
@@ -2315,6 +2323,13 @@ class TypeCombinator
23152323
{
23162324
zend_long constantArrayValuesCount = 0;
23172325
for (zv::ArrayEntry entry : zv::ArrRef(types)) {
2326+
/* TypeTraverser::map()'s `Type $type` parameter */
2327+
bool isType;
2328+
if (UNEXPECTED(!pt_type_instanceof(entry.value().raw(), PT_CLASS_TYPE, isType))) return -1;
2329+
if (UNEXPECTED(!isType)) {
2330+
zend_type_error("%s::map(): Argument #1 ($type) must be of type %s, %s given", ZSTR_VAL(pt_ce_type_traverser->name), ptcls::type, zend_zval_value_name(entry.value().raw()));
2331+
return -1;
2332+
}
23182333
zval count;
23192334
ZVAL_LONG(&count, constantArrayValuesCount);
23202335
zv::Val callback = pt_type_native_callback(countValueTypesCallback, &count, NULL);

‎turbo-ext/tests/type-family.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8377,6 +8377,10 @@ public function getTypes(): array
83778377
$r["combinator $method $argsName"] = $misuse(static fn () => \PHPStan\Type\TypeCombinator::$method(...$args));
83788378
}
83798379
}
8380+
// countConstantArrayValueTypes() hands each element to TypeTraverser::map(Type $type, ...)
8381+
foreach (['string' => 'x', 'object' => new \stdClass()] as $elementName => $element) {
8382+
$r["combinator countConstantArrayValueTypes $elementName"] = $misuse(static fn () => \PHPStan\Type\TypeCombinator::countConstantArrayValueTypes([new \PHPStan\Type\IntegerType(), $element]));
8383+
}
83808384

83818385
foreach ($r as $key => $value) {
83828386
$observations["misuse $key"] = $value;

0 commit comments

Comments
 (0)