Use of func_num_args() makes methods non-extendable

digitalpoint

Well-known member
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);
        }
 
Back
Top Bottom