<commit_before><?php
namespace Recoil\Kernel\Api;

use BadMethodCallException;
use Recoil\Coroutine\CoroutineInterface;
use Recoil\Coroutine\CoroutineTrait;
use Recoil\Kernel\Strand\StrandInterface;

/**
 * Represents a call to a feature provided by the Kernel API.
 *
 * @see Recoil\Kernel\KernelApiInterface
 * @see Recoil\Kernel\KernelInterface::api()
 */
class KernelApiCall implements CoroutineInterface
{
    use CoroutineTrait;

    /**
     * @param string $name      The name of the kernel API function to invoke.
     * @param array  $arguments The arguments to the kernel API function.
     */
    public function __construct($name, array $arguments)
    {
        $this->name = $name;
        $this->arguments = $arguments;
    }

    /**
     * Fetch the name of the kernel API function to invoke.
     *
     * @return string The name of the kernel API function to invoke.
     */
    public function name()
    {
        return $this->name;
    }

    /**
     * Fetch the arguments to the kernel API function.
     *
     * @return array The arguments to the kernel API function.
     */
    public function arguments()
    {
        return $this->arguments;
    }

    /**
     * Start the coroutine.
     *
     * @param StrandInterface $strand The strand that is executing the coroutine.
     */
    public function call(StrandInterface $strand)
    {
        $method = [$strand->kernel()->api(), $this->name()];

        if (!is_callable($method)) {
            $strand->throwException(
                new BadMethodCallException(
                    'The kernel API does not have an operation named "' . $this->name . '".'
                )
            );

            return;
        }

        $strand->pop();

        $arguments = $this->arguments();

        array_unshift($arguments, $strand);

        // If the kernel API implementation returns a non-null value it is
        // treated as a coroutine to be executed. This allows implementation of
        // kernel API operations to be implemented as generators.<commit_msg>Avoid unecessary method calls in KernelApiCall.<commit_after><?php
namespace Recoil\Kernel\Api;

use BadMethodCallException;
use Recoil\Coroutine\CoroutineInterface;
use Recoil\Coroutine\CoroutineTrait;
use Recoil\Kernel\Strand\StrandInterface;

/**
 * Represents a call to a feature provided by the Kernel API.
 *
 * @see Recoil\Kernel\KernelApiInterface
 * @see Recoil\Kernel\KernelInterface::api()
 */
class KernelApiCall implements CoroutineInterface
{
    use CoroutineTrait;

    /**
     * @param string $name      The name of the kernel API function to invoke.
     * @param array  $arguments The arguments to the kernel API function.
     */
    public function __construct($name, array $arguments)
    {
        $this->name = $name;
        $this->arguments = $arguments;
    }

    /**
     * Fetch the name of the kernel API function to invoke.
     *
     * @return string The name of the kernel API function to invoke.
     */
    public function name()
    {
        return $this->name;
    }

    /**
     * Fetch the arguments to the kernel API function.
     *
     * @return array The arguments to the kernel API function.
     */
    public function arguments()
    {
        return $this->arguments;
    }

    /**
     * Start the coroutine.
     *
     * @param StrandInterface $strand The strand that is executing the coroutine.
     */
    public function call(StrandInterface $strand)
    {
        $method = [$strand->kernel()->api(), $this->name];

        if (!is_callable($method)) {
            $strand->throwException(
                new BadMethodCallException(
                    'The kernel API does not have an operation named "' . $this->name . '".'
                )
            );

            return;
        }

        $strand->pop();

        $arguments = $this->arguments;

        array_unshift($arguments, $strand);

        // If the kernel API implementation returns a non-null value it is
        // treated as a coroutine to be executed. This allows implementation of
        // kernel API operations to be implemented as generators.
{'fraction_non_alphanumeric': 0.09840425531914894, 'fraction_numerical': 0.0, 'mean_word_length': 2.2244738893219016, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '950607', 'n_tokens_mistral': 1138, 'n_tokens_neox': 1099, 'n_words': 368}