Closed Use of func_num_args() makes methods non-extendable

digitalpoint

Well-known member
Licensed customer
Affected version
2.3
The use of the func_num_args() function within the XF/Mvc/Entity/Finder class makes those methods not extendable because if you pass null to the method, it's counted as an argument that was passed.

Let's say we have a class extension that hooks into the Finder::where() method...

PHP:
namespace Addon\XF\Finder;

class User extends XFCP_User
{
    public function where($condition, $operator = null, $value = null)
    {
        return parent::where($condition, $operator, $value);
    }
}

Unless it was called with all 3 args, it will fail because calling parent:: like that will always be seen as 3 arguments (because actually passing a null argument counts as an argument as far as the func_num_args() function goes), and you end up with:

An exception occurred: [InvalidArgumentException] Operator 1 is not valid in src/XF/Mvc/Entity/Finder.php on line 314

Rather than checking for the number of args passed, check if the args are null or not. That will allow the methods in that class to be extended properly.
 
It can of course be worked around, but it involves this silliness:

PHP:
        $argCount = func_num_args();
        switch ($argCount)
        {
            case 1: return parent::where($condition);
            case 2: return parent::where($condition, $operator);
            default: return parent::where($condition, $operator, $value);
        }
 
...of course there's probably a time where you want to treat null as a valid argument (and probably why it's done that way)... so there's that. 😂
 
Automated triage: Design issue

Thank you for reporting this issue. The finder works out what a condition means from how many arguments it was given, and an explicit null is a valid value, for example to match NULL columns. A wrapper that always passes all three arguments therefore changes the meaning of the call. Changing that would mean changing how the finder API is designed. Forwarding the original arguments unchanged avoids the problem.

This is an automated assessment. A member of staff may review and update it.
 
Back
Top Bottom