bartfeenstra / fu-php

🛠 Functional programming tools for PHP 🐘
https://twitter.com/BartFeenstra
MIT License
2 stars 1 forks source link

Validate callables #72

Open bartfeenstra opened 7 years ago

bartfeenstra commented 7 years ago

One of the problems is that many callables developers may want to use do not conform to the interfaces we'll be defining here:

We can optionally whitelist callables per callable type, like registering \is_string() and siblings as valid predicates, even though their signatures may not indicate so. Manual registration should be allowed, but for built-in functions we may be able to use get_defined_functions(). It also enforces developers to use type declarations while a large portion of them prefers duck typing. Catering to that audience is not a priority (type declarations improve debugging and validation experiences and that's exactly what we're trying to do here), but it would be nice if duck typing could be supported. Maybe a global assertion validation level? Maybe consider callables valid if they may not be as strictly defined as the prototype prescribes, but cannot be confirmed as otherwise incompatible? An example of this would be to allow is_string() based on the fact it takes the correct number of arguments, and does not specify any types that would violate the prototype's contract. This deviates from interface implementation compatibility (which is what we were trying to emulate here), but would prevent MANY false negatives.

bartfeenstra commented 7 years ago

The same applies to currying and partial application, as the closures they produce do not specify return values.

func_get_args() is also tricky, not because we cannot validate the number of parameters (because less than provided is always fine), but because there's no way to check the types of arguments the function obviously accepts.

I guess we have to accept any solution we come up with can tell most, but not all invalid callables are invalid, but it can never certainly say any callable is 100% valid, at the very least due to func_get_args().

coveralls commented 6 years ago

Coverage Status

Coverage decreased (-21.9%) to 76.203% when pulling f35b5bb8294ef34d82ce35b98adaf73668727e1d on assert-callable into c4afb56d31da2250efca8f974657544d1d339077 on master.

coveralls commented 6 years ago

Coverage Status

Coverage decreased (-22.2%) to 77.468% when pulling 14c2a42db3fe02c84de5b80c63c7d2ec15ebace1 on assert-callable into 0b974c9dd10e404d194d3fe6b064cc128f50004c on master.

coveralls commented 6 years ago

Coverage Status

Coverage decreased (-22.2%) to 77.468% when pulling 14c2a42db3fe02c84de5b80c63c7d2ec15ebace1 on assert-callable into 0b974c9dd10e404d194d3fe6b064cc128f50004c on master.