/** * Get a collection ref for a statically called method. * * @param Query $query The base query to return a collection * @param string $methodName The method being called * @param array $args Arguments to the method * * @return mixed */ private static function getCollectionRef(Query $query, $methodName, array $args) { // If we get here, the method does not exist within the Query class, // so we fetch the results of the query, and try the same method on the // collection class $collection = $query->fetch(); // Create a reference class against Collection $collectionRef = new \ReflectionClass(Collection::class); // Check if the method exists on the Collection class if ($collectionRef->hasMethod($methodName)) { // Invoke the method with the provided arguments $method = $collectionRef->getMethod($methodName); // Return the results return $method->invokeArgs($collection, $args); } return $collection; }