@sgiehl opened this Issue on November 10th 2022 Member

Summary

Currently it is not possible to use type hints in API methods. As all parameters are parsed from the request, they are currently always either a string or an array. Adding type hints other then string or array would though currently end up in an error of mismatching types.

Allowing type hinting for API methods would help us solve a plenty of issues around incorrect parameter types. In the past we had a lot issues like that, where e.g. someone passed an array as period or date parameter (e.g. like #19222), causing the API to throw an uncaught exception, as it was used in a way, where an array couldn't be handled.

To implement that we could simply do some parameter validation in the API proxy, based on the type hint we can receive using reflection. Passing that type to Common::getRequestVar, which we use, to receive all parameter values should do the trick.
Needs to be changed somewhere around here:
https://github.com/matomo-org/matomo/blob/a01eea35273f7c569d060ec455ec0c0de07d1582/core/API/Proxy.php#L409-L441

Note: This issue is not about adding type hints to all our API methods. For now we should only provide the possibility, so we can add it when already working on certain APIs later.

@mattab I've discussed this improvement today with @tsteur and @justinvelluppillai
It would be awesome to include this in Matomo 5. We could in theory also do it in a later release, but if we need to add a type hint in a plugin then, we would need to increase the minimum required version, to the version where this got implemented.

I already had a look at the code, and it should only take around 2 hours to implement that.

This Issue was closed on December 5th 2022
Powered by GitHub Issue Mirror