<commit_before><?php

/*
 * This file is part of the Yodler package.
 *
 * (c) aes3xs <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Aes3xs\Yodler\Common;

use Aes3xs\Yodler\Exception\ArgumentNotFoundException;

/**
 * Static helper for manipulations with closures.
 */
class CallableHelper
{
    /**
     * Extract call arguments from specified callback.
     *
     * Can be used with array notation [className, methodName].
     *
     * @param callable $callable
     *
     * @return array
     */
    public static function extractArguments(callable $callable)
    {
        if (is_array($callable)) {
            $class = is_object($callable[0]) ? get_class($callable[0]) : $callable[0];
            $reflection = new \ReflectionMethod($class, $callable[1]);
        } else {
            $reflection = new \ReflectionFunction($callable);
        }

        $arguments = $reflection->getParameters();

        $argumentNames = [];

        foreach ($arguments as $argument) {
            $argumentNames[] = $argument->getName();
        }

        return $argumentNames;
    }

    /**
     * Execute specified callback with arguments in associative array.
     *
     * @param callable $callable
     * @param array $arguments
     *
     * @return mixed
     */
    public static function call(callable $callable, array $arguments)
    {
        $argumentNames = self::extractArguments($callable);

        $argumentCallStack = [];

        foreach ($argumentNames as $name) {
            if (!array_key_exists($name, $arguments)) {
                throw new ArgumentNotFoundException($name);
            }

            $argumentCallStack[] = $arguments[$name];
        }

        return call_user_func_array($callable, $argumentCallStack);
    }<commit_msg>Fix CallableHelper to use argument defaults<commit_after><?php

/*
 * This file is part of the Yodler package.
 *
 * (c) aes3xs <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Aes3xs\Yodler\Common;

use Aes3xs\Yodler\Exception\ArgumentNotFoundException;

/**
 * Static helper for manipulations with closures.
 */
class CallableHelper
{
    /**
     * Extract call arguments from specified callback.
     *
     * Can be used with array notation [className, methodName].
     * Collects default values into $defaults array.
     *
     * @param callable $callable
     *
     * @param array $defaults
     * @return array
     */
    public static function extractArguments(callable $callable, &$defaults = [])
    {
        if (is_array($callable)) {
            $class = is_object($callable[0]) ? get_class($callable[0]) : $callable[0];
            $reflection = new \ReflectionMethod($class, $callable[1]);
        } else {
            $reflection = new \ReflectionFunction($callable);
        }

        $arguments = $reflection->getParameters();

        $argumentNames = [];

        foreach ($arguments as $argument) {
            $argumentNames[] = $argument->getName();
            if ($argument->isDefaultValueAvailable()) {
                $defaults[$argument->getName()] = $argument->getDefaultValue();
            }
        }

        return $argumentNames;
    }

    /**
     * Execute specified callback with arguments in associative array.
     *
     * @param callable $callable
     * @param array $arguments
     *
     * @return mixed
     */
    public static function call(callable $callable, array $arguments)
    {
        $argumentNames = self::extractArguments($callable, $defaults);

        $argumentCallStack = [];

        foreach ($argumentNames as $name) {
            $hasValue = array_key_exists($name, $arguments);
            $hasDefault = array_key_exists($name, $defaults);

            if (!$hasValue && !$hasDefault) {
                throw new ArgumentNotFoundException($name);
            }

            $argumentCallStack[] = $hasValue ? $arguments[$name] : $defaults[$name];
        }

        return call_user_func_array($callable, $argumentCallStack);
    }
{'fraction_non_alphanumeric': 0.11591778202676864, 'fraction_numerical': 0.0033460803059273425, 'mean_word_length': 2.464403973509934, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2109263', 'n_tokens_mistral': 1145, 'n_tokens_neox': 1115, 'n_words': 294}