Skip to content

Misleading documentation for invoke_first and invoke_last #156

@phanan

Description

@phanan

The documentation for invoke_first and invoke_last says, in that order:

mixed Functional\invoke_first(array|Traversable $collection, string $methodName[, array $methodArguments]) Invokes method $methodName on the first object in the $collection and returns the results of the call

and

mixed Functional\invoke_last(array|Traversable $collection, string $methodName[, array $methodArguments]) Invokes method $methodName on the last object in the $collection and returns the results of the call

However, the implementation of these functions seems to say otherwise. Take invoke_first as an example:

function invoke_first($collection, $methodName, array $arguments = [])
{
    //... uninteresting assertions

    foreach ($collection as $element) {

        $callback = [$element, $methodName];
        if (is_callable($callback)) {
            return $callback(...$arguments);
        }
    }

    return null;
}

This means we're calling $methodName on the first applicable object in the collection. Which means, for example, we have ['foo', 'bar', $objectWithMethodName], then $objectWithMethodName->$methodName() will be called instead of 'foo'->$methodName(), and the result of $objectWithMethodName->$methodName() will be returned instead of NULL.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions