PHPDoc - Dynamic Function Names and Return Types for IDE

Viewed 64

I'm working on (somebody else's) application. I'm not sure what framework it uses if any, but it allows calls like this

Users:findByUsername('josh');

Which will return one instantiated User object/entity that matches that username. This seems to be being done by overriding __callStatic. Not an enormous fan, but would like the IDE to recognize the variable as a User object (or whatever class it's being called from).

Very ideally, I'd like the IDE to recognize that as a valid function or an alias of callStatic, but that's not a big deal.

So is there anyway to PHPDoc findByX as a wildcard? Or a better way to do it? The call static function is as follows:

    public static function __callStatic($name, $arguments)
    {
        if (substr($name, 0, 6) == 'findBy') {

            $turn_off_date_filters = (isset($arguments[1]) && is_bool($arguments[1]) && $arguments[1] === true);
            $additional_params = (isset($arguments[1]) && is_array($arguments[1]))
                ? $arguments[1]
                : [];

            $filter = array_merge(
                $additional_params,
                [\Helper::camel2snake(substr($name, 6)) => $arguments[0]]
            );

            if ($turn_off_date_filters) {
                $entity = static::where($filter)->withoutActiveDateFilters()->withoutAvailabilityDateFilters()->first();
            } else {
                $entity = static::where($filter)->first();
            }

            return $entity;

        } elseif (substr($name, 0, 9) == 'findAllBy') {

            $additional_params = (isset($arguments[1]) && is_array($arguments[1]))
                ? $arguments[1]
                : [];

            $filter = array_merge(
                $additional_params,
                [\Helper::camel2snake(substr($name, 9)) => $arguments[0]]
            );

            return static::where($filter)->select();

        } elseif (substr($name, 0, 5) == 'where') {

            return static::where([\Helper::camel2snake(substr($name, 5)) => $arguments[0]]);

        } elseif (substr($name, 0, 11) == 'deleteAllBy') {

            return static::deleteAllBy([\Helper::camel2snake(substr($name, 11)) => $arguments[0]]);
        }

        return null;
    }
0 Answers
Related