Skip to content

Commit

Permalink
Annotate internal functions with the mixed type
Browse files Browse the repository at this point in the history
Closes phpGH-5618
  • Loading branch information
kocsismate committed May 25, 2020
1 parent cbf86ef commit b371843
Show file tree
Hide file tree
Showing 23 changed files with 181 additions and 273 deletions.
7 changes: 3 additions & 4 deletions Zend/zend_builtin_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ function zend_version(): string {}

function func_num_args(): int {}

/** @return mixed */
function func_get_arg(int $arg_num) {}
function func_get_arg(int $arg_num): mixed {}

function func_get_args(): array {}

Expand Down Expand Up @@ -69,12 +68,12 @@ function trigger_error(string $message, int $error_type = E_USER_NOTICE): bool {
/** @alias trigger_error */
function user_error(string $message, int $error_type = E_USER_NOTICE): bool {}

/** @return mixed */
/** @return string|array|object|null */
function set_error_handler($error_handler, int $error_types = E_ALL) {}

function restore_error_handler(): bool {}

/** @return mixed */
/** @return string|array|object|null */
function set_exception_handler($exception_handler) {}

function restore_exception_handler(): bool {}
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_builtin_functions_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_func_num_args, 0, 0, IS_LONG, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_func_get_arg, 0, 0, 1)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_func_get_arg, 0, 1, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO(0, arg_num, IS_LONG, 0)
ZEND_END_ARG_INFO()

Expand Down
3 changes: 1 addition & 2 deletions Zend/zend_closures.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public static function bind(Closure $closure, ?object $newthis, $newscope = UNKN
/** @alias Closure::bind */
public function bindTo(?object $newthis, $newscope = UNKNOWN): ?Closure {}

/** @return mixed */
public function call(object $newthis, ...$parameters) {}
public function call(object $newthis, mixed ...$parameters): mixed {}

/** @param callable $callable Not a proper type annotation due to bug #78770 */
public static function fromCallable($callable): Closure {}
Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_closures_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Closure_bindTo, 0, 1, Closu
ZEND_ARG_INFO(0, newscope)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Closure_call, 0, 0, 1)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Closure_call, 0, 1, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO(0, newthis, IS_OBJECT, 0)
ZEND_ARG_VARIADIC_INFO(0, parameters)
ZEND_ARG_VARIADIC_TYPE_INFO(0, parameters, IS_MIXED, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Closure_fromCallable, 0, 1, Closure, 0)
Expand Down
15 changes: 5 additions & 10 deletions Zend/zend_generators.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@ public function rewind(): void {}

public function valid(): bool {}

/** @return mixed */
public function current() {}
public function current(): mixed {}

/** @return mixed */
public function key() {}
public function key(): mixed {}

public function next(): void {}

/** @return mixed */
public function send($value) {}
public function send(mixed $value): mixed {}

/** @return mixed */
public function throw(Throwable $exception) {}
public function throw(Throwable $exception): mixed {}

/** @return mixed */
public function getReturn() {}
public function getReturn(): mixed {}
}
8 changes: 4 additions & 4 deletions Zend/zend_generators_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Generator_valid, 0, 0, _IS_BOOL, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Generator_current, 0, 0, 0)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Generator_current, 0, 0, IS_MIXED, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_Generator_key arginfo_class_Generator_current

#define arginfo_class_Generator_next arginfo_class_Generator_rewind

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Generator_send, 0, 0, 1)
ZEND_ARG_INFO(0, value)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Generator_send, 0, 1, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO(0, value, IS_MIXED, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Generator_throw, 0, 0, 1)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Generator_throw, 0, 1, IS_MIXED, 0)
ZEND_ARG_OBJ_INFO(0, exception, Throwable, 0)
ZEND_END_ARG_INFO()

Expand Down
15 changes: 13 additions & 2 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public function __construct(string $name, bool $isBuiltin) {

public static function fromNode(Node $node) {
if ($node instanceof Node\Name) {
if ($node->toString() === "mixed") {
return new SimpleType($node->toString(), true);
}

assert($node->isFullyQualified());
return new SimpleType($node->toString(), false);
}
Expand Down Expand Up @@ -97,6 +101,8 @@ public function toTypeCode() {
return "IS_VOID";
case "callable":
return "IS_CALLABLE";
case "mixed":
return "IS_MIXED";
default:
throw new Exception("Not implemented: $this->name");
}
Expand All @@ -123,6 +129,8 @@ public function toTypeMask() {
return "MAY_BE_OBJECT";
case "callable":
return "MAY_BE_CALLABLE";
case "mixed":
return "MAY_BE_ANY";
default:
throw new Exception("Not implemented: $this->name");
}
Expand Down Expand Up @@ -637,8 +645,11 @@ function parseFunctionLike(
$param->default->name->toLowerString() === "null" &&
$type && !$type->isNullable()
) {
throw new Exception(
"Parameter $varName of function $name has null default, but is not nullable");
$simpleType = $type->tryToSimpleType();
if ($simpleType === null || $simpleType->name !== "mixed") {
throw new Exception(
"Parameter $varName of function $name has null default, but is not nullable");
}
}

$foundVariadic = $param->variadic;
Expand Down
35 changes: 11 additions & 24 deletions ext/filter/filter.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,17 @@

function filter_has_var(int $type, string $variable_name): bool {}

/**
* @param mixed $options
* @return mixed
*/
function filter_input(int $type, string $variable_name, int $filter = FILTER_DEFAULT, $options = null) {}

/**
* @param mixed $variable
* @param mixed $options
* @return mixed
*/
function filter_var($variable, int $filter = FILTER_DEFAULT, $options = null) {}

/**
* @param mixed $options
* @return mixed
*/
function filter_input_array(int $type, $options = FILTER_DEFAULT, bool $add_empty = true) {}

/**
* @param mixed $options
* @return mixed
*/
function filter_var_array(array $data, $options = FILTER_DEFAULT, bool $add_empty = true) {}
/** @param array|int $options */
function filter_input(int $type, string $variable_name, int $filter = FILTER_DEFAULT, $options = null): mixed {}

/** @param array|int $options */
function filter_var(mixed $variable, int $filter = FILTER_DEFAULT, $options = null): mixed {}

/** @param array|int $options */
function filter_input_array(int $type, $options = FILTER_DEFAULT, bool $add_empty = true): array|false|null {}

/** @param array|int $options */
function filter_var_array(array $data, $options = FILTER_DEFAULT, bool $add_empty = true): array|false|null {}

function filter_list(): array {}

Expand Down
10 changes: 5 additions & 5 deletions ext/filter/filter_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_filter_has_var, 0, 2, _IS_BOOL,
ZEND_ARG_TYPE_INFO(0, variable_name, IS_STRING, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_filter_input, 0, 0, 2)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_filter_input, 0, 2, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO(0, type, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, variable_name, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, filter, IS_LONG, 0, "FILTER_DEFAULT")
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, options, "null")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_filter_var, 0, 0, 1)
ZEND_ARG_INFO(0, variable)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_filter_var, 0, 1, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO(0, variable, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, filter, IS_LONG, 0, "FILTER_DEFAULT")
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, options, "null")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_filter_input_array, 0, 0, 1)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_filter_input_array, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE|MAY_BE_NULL)
ZEND_ARG_TYPE_INFO(0, type, IS_LONG, 0)
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, options, "FILTER_DEFAULT")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, add_empty, _IS_BOOL, 0, "true")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_filter_var_array, 0, 0, 1)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_filter_var_array, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE|MAY_BE_NULL)
ZEND_ARG_TYPE_INFO(0, data, IS_ARRAY, 0)
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, options, "FILTER_DEFAULT")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, add_empty, _IS_BOOL, 0, "true")
Expand Down
5 changes: 2 additions & 3 deletions ext/json/json.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

/** @generate-function-entries */

function json_encode($value, int $options = 0, int $depth = 512): string|false {}
function json_encode(mixed $value, int $options = 0, int $depth = 512): string|false {}

/** @return mixed */
function json_decode(string $json, ?bool $assoc = null, int $depth = 512, int $options = 0) {}
function json_decode(string $json, ?bool $assoc = null, int $depth = 512, int $options = 0): mixed {}

function json_last_error(): int {}

Expand Down
4 changes: 2 additions & 2 deletions ext/json/json_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* This is a generated file, edit the .stub.php file instead. */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_json_encode, 0, 1, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_INFO(0, value)
ZEND_ARG_TYPE_INFO(0, value, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_LONG, 0, "0")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, depth, IS_LONG, 0, "512")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_json_decode, 0, 0, 1)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_json_decode, 0, 1, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO(0, json, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, assoc, _IS_BOOL, 1, "null")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, depth, IS_LONG, 0, "512")
Expand Down
5 changes: 2 additions & 3 deletions ext/mysqli/mysqli.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public function attr_set(int $attr, int $mode_in) {}
* @return bool
* @alias mysqli_stmt_bind_param
*/
public function bind_param(string $types, &...$vars) {}
public function bind_param(string $types, mixed &...$vars) {}

/**
* @return bool
Expand Down Expand Up @@ -681,8 +681,7 @@ function mysqli_stmt_attr_get(mysqli_stmt $mysql_stmt, int $attr): int|false {}

function mysqli_stmt_attr_set(mysqli_stmt $mysql_stmt, int $attr, int $mode_in): bool {}

/** @param mixed &...$vars */
function mysqli_stmt_bind_param(mysqli_stmt $mysql_stmt, string $types, &...$vars): bool {}
function mysqli_stmt_bind_param(mysqli_stmt $mysql_stmt, string $types, mixed &...$vars): bool {}

/** @param mixed &...$vars */
function mysqli_stmt_bind_result(mysqli_stmt $mysql_stmt, &...$vars): bool {}
Expand Down
4 changes: 2 additions & 2 deletions ext/mysqli/mysqli_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mysqli_stmt_bind_param, 0, 2, _IS_BOOL, 0)
ZEND_ARG_OBJ_INFO(0, mysql_stmt, mysqli_stmt, 0)
ZEND_ARG_TYPE_INFO(0, types, IS_STRING, 0)
ZEND_ARG_VARIADIC_INFO(1, vars)
ZEND_ARG_VARIADIC_TYPE_INFO(1, vars, IS_MIXED, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mysqli_stmt_bind_result, 0, 1, _IS_BOOL, 0)
Expand Down Expand Up @@ -633,7 +633,7 @@ ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_mysqli_stmt_bind_param, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, types, IS_STRING, 0)
ZEND_ARG_VARIADIC_INFO(1, vars)
ZEND_ARG_VARIADIC_TYPE_INFO(1, vars, IS_MIXED, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_mysqli_stmt_bind_result, 0, 0, 0)
Expand Down
2 changes: 1 addition & 1 deletion ext/opcache/Optimizer/zend_func_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ static const func_info_t func_infos[] = {
F1("password_get_info", MAY_BE_NULL | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_STRING | MAY_BE_ARRAY_OF_LONG | MAY_BE_ARRAY_OF_STRING | MAY_BE_ARRAY_OF_ARRAY),
F1("convert_uuencode", MAY_BE_FALSE | MAY_BE_STRING),
F1("convert_uudecode", MAY_BE_FALSE | MAY_BE_STRING),
F1("pow", MAY_BE_NULL | MAY_BE_LONG | MAY_BE_DOUBLE | MAY_BE_OBJECT),
F1("pow", MAY_BE_LONG | MAY_BE_DOUBLE | MAY_BE_OBJECT),
F1("decbin", MAY_BE_STRING),
F1("decoct", MAY_BE_STRING),
F1("dechex", MAY_BE_STRING),
Expand Down
2 changes: 1 addition & 1 deletion ext/pdo/pdo_dbh.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function errorInfo() {}
/** @return int|false */
public function exec(string $statement) {}

/** @return mixed */
/** @return bool|int|string|array|null */
public function getAttribute(int $attribute) {}

/** @return array */
Expand Down
4 changes: 2 additions & 2 deletions ext/reflection/php_reflection.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function __toString(): string {}
public function isDisabled() {}

/** @return mixed */
public function invoke(...$args) {}
public function invoke(mixed ...$args) {}

/** @return mixed */
public function invokeArgs(array $args) {}
Expand Down Expand Up @@ -181,7 +181,7 @@ public function getClosure($object = UNKNOWN) {}
public function getModifiers() {}

/** @return mixed */
public function invoke(?object $object = null, ...$args) {}
public function invoke(?object $object = null, mixed ...$args) {}

/** @return mixed */
public function invokeArgs(?object $object, array $args) {}
Expand Down
8 changes: 5 additions & 3 deletions ext/reflection/php_reflection_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ ZEND_END_ARG_INFO()
#define arginfo_class_ReflectionFunction_isDisabled arginfo_class_ReflectionFunctionAbstract___clone

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ReflectionFunction_invoke, 0, 0, 0)
ZEND_ARG_VARIADIC_INFO(0, args)
ZEND_ARG_VARIADIC_TYPE_INFO(0, args, IS_MIXED, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ReflectionFunction_invokeArgs, 0, 0, 1)
Expand Down Expand Up @@ -125,7 +125,7 @@ ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ReflectionMethod_invoke, 0, 0, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, object, IS_OBJECT, 1, "null")
ZEND_ARG_VARIADIC_INFO(0, args)
ZEND_ARG_VARIADIC_TYPE_INFO(0, args, IS_MIXED, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ReflectionMethod_invokeArgs, 0, 0, 2)
Expand Down Expand Up @@ -221,7 +221,9 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ReflectionClass_isInstance, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, object, IS_OBJECT, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_ReflectionClass_newInstance arginfo_class_ReflectionFunction_invoke
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ReflectionClass_newInstance, 0, 0, 0)
ZEND_ARG_VARIADIC_INFO(0, args)
ZEND_END_ARG_INFO()

#define arginfo_class_ReflectionClass_newInstanceWithoutConstructor arginfo_class_ReflectionFunctionAbstract___clone

Expand Down
Loading

0 comments on commit b371843

Please sign in to comment.