On Sat, Aug 10, 2019 at 12:56 PM Nikita Popov <nikita....@gmail.com> wrote:

> Hi,
>
> Lack of type information for internal functions in Reflection is a
> long-standing issue. In PHP 8 we finally have all the necessary technical
> groundwork done to support argument and return type annotations on internal
> functions at scale.
>
> The only thing left is to actually add those type annotations ... to many
> hundreds of functions. This is something everyone can help with, as it does
> not need expertise in C.
>
> I've opened a sample PR to show the process:
> https://github.com/php/php-src/pull/4499
>
> Here, we take some existing arginfo structures in basic_functions.c and
> convert them into stubs in basic_functions.stub.php. We can take the
> argument names from the existing arginfo. To figure out the types, we need
> to look at the implementation (the php.net documentation tends to lie
> about details).
>
> For example, the first function, set_time_limit is defined in
> https://github.com/php/php-src/blob/172c71980df0fe4c9d62c3365f0a2cdb139e3e86/main/main.c#L1501.
> We see that it accepts an "l" parameter, which is an int. We see that it
> uses RETVAL_TRUE and RETVAL_FALSE, which means the return value is a bool.
>
> Once this is done, we need to auto-generate new arginfo data. If you have
> a development setup, this is done automatically when running "make".
> Otherwise, it's possible to manually run "php scripts/dev/gen_stub.php
> ext/standard/basic_functions.stub.php".
>
> Any help would be appreciated :)
>

A couple of things I didn't mention but noticed during reviews:

 * We can't add return types to methods (at least for non-final classes),
because these would force inheriting classes to specify the return type as
well. So return types on methods should be specified using @return for now
-- we should discuss if we want to add them as proper types (with the BC
break that implies).

 * For by-reference parameters, very likely no type should be specified.
This is because types are always input types. These are usually not
constrained for reference parameters. The output type may be constrained,
but there's no way to specify it in PHP right now.

 * Many internal functions have defaults that cannot be expressed as a
constant expression, e.g. because they depend on ini settings or other
parameters. In this cases by convention "$foo = UNKNOWN" is used, just to
mark that the argument is optional, but doesn't have a "proper" default
value.

Nikita

Reply via email to