On Thu, Apr 9, 2020 at 7:21 AM Stanislav Malyshev <smalys...@gmail.com> wrote:
> Hi! > > > 2. Throw a notice if a parameter name is changed. While LSP violations > are > > normally fatal errors (in PHP 8), we could use a lower-severity > diagnostic > > for this case, that allows code to still run, but makes developers aware > of > > the problem. (It should be noted that automatic fixup of parameter names > > using tooling should be fairly easy to implement.) > > I think we should have a notice for now, and eventually when people are > used to it upgrade it to an error. > > > The larger problem is that internal functions don't have a well-defined > > concept of parameter default value. Parameter defaults are implicit in C > > code, and sometimes based on argument count checks. When named parameters > > are involved, one generally cannot assume that if a later (optional) > > parameter is passed, all earlier (optional) parameters are passed as > well. > > IIRC I did some work on that when making the skipped parameter RFC, but > I think we have no choice but to make all internal functions we control > have proper default implementation. Older modules may be lagging though, > so another option would be to add some kind of flag to internal > functions that would block named parameters if not set, and make > extension writers fix their functions and then set this flag explicitly. > Of course we'll set this flag for all functions in core, after fixing > those of them that need fixing. Maybe not a flag but using different > macro/API function, etc. - the idea is that some explicit change would > be needed. Yes, that means additional work... Yeah, I'm aware of the work you did in this area, and used this as part of my previous named parameters implementation. However, I'm not sure that this is the right way forward anymore, for a couple of reasons: 1. Safety: 3rd party extension functions that don't get updated may end up crashing if it has ZEND_NUM_ARGS() based assumptions. Not just stop working or misbehave, but segfault. I think we should try to avoid that. 2. Implementation: This approach still requires ZPP support, in that we need to skip over arguments that have not been passed and leave them at their default values. This is pretty easy to do for classic ZPP, but somewhat problematic for FastZPP. This will require an extra UNDEF check for all arguments, which (I think) is not much of a concern for performance, but is a concern for code size, which is a limiting factor of this API. 3. Behavior: Even if there are no safety issues because no arg num assumptions are made, we might still be left with behavior that does not make a lot of sense. For example, the array_keys() function does not do arg count checks, but writing something like array_keys($array, strict => true); would still not make any sense, because the "strict" parameter is only meaningful if the "search_value" parameter is specified. We specify this function as follows in stubs: function array_keys(array $arg, $search_value = UNKNOWN, bool $strict = false): array {} If we simply allow the call and fall back to ZPP defaults, that means the call works, and will have the same behavior as array_keys($array), with the "strict" parameter simply going ignored. On the other hand, if we handle internal functions like userland functions, and replace missing arguments with specified default values, then this case would yield an exception: array_keys(): Argument #2 ($search_value) must be passed explicitly, because the default value is not known I think that this gives us a more sensible behavior. We have (unfortunately) quite a lot of internal functions that are "magic" in some way, and where certain parameters do not have well-defined defaults and cannot reasonably be skipped when named parameters are used. I think that this approach gives us a pretty clean handling of such cases. Of course, the big disadvantage is that it does require default value information in stubs. We have this information for nearly all bundled extensions at this point, but not for 3rd party extensions. The ZPP based approach does make most code "just work", even if it will some of the code "just crash" instead... Regards, Nikita