<commit_before><?php

namespace marty\plugins;

use mako\syringe\Container;
use ReflectionException;
use ReflectionFunction;
use ReflectionParameter;
use RuntimeException;
use Smarty;

/**
 * Plugin base class to inherit other plugins from.
 *
 * @author <NAME> <<EMAIL>>
 */
abstract class BasePlugin
{
    /**
     * @var Container
     */
    protected $container;

    /**
     * Name of the plugin
     *
     * @var string
     */
    protected $name;

    /**
     * Path to the plugin file.
     *
     * @var string
     */
    private $path;

    public function __construct(Container $container, $path, $name)
    {
        $this->container = $container;
        $this->path      = $path;
        $this->name      = $name;
    }

    /**
     * Register the plugin with the Smarty instance.
     */
    abstract public function register(Smarty $smarty);

    protected function loadPlugin()
    {
        require_once $this->path;
    }

    /**
     * Resolve the parameters for the plugin, as Mako does not appear to handle
     * the resolution of reference and typed parameters very well.
     *
     * @param string $functionName
     * @param array $params
     * @return mixed Whatever the function returns.
     */
    protected function callWithParameters($functionName, array $params)
    {
        $function = new ReflectionFunction($functionName);

        $functionParameters = [];

        foreach ($function->getParameters() as $parameter) {
            $name = $this->resolveParameter($parameter, $params,
                $functionParameters);
        }

        return $function->invokeArgs($functionParameters);
    }

    private function resolveParameter(ReflectionParameter $parameter,
                                      array $provided,
                                      array &$functionParameters)
    {
        $name = $parameter->getName();
        if (array_key_exists($name, $provided)) {
            if ($parameter->isPassedByReference()) {
                $functionParameters[] = &$provided[$name];
            } else {
                $functionParameters[] = $provided[$name];
            }
        } else {
            try {
                $functionParameters[] = $this->container->get($parameter->getClass());
            } catch (ReflectionException $ex) {
                throw new RuntimeException("Unable to resolve parameter $name.",
                0, $ex);
            }
        }
    }
}<commit_msg>Bugfix: correctly autoload namespaced classes.<commit_after><?php

namespace marty\plugins;

use mako\syringe\Container;
use ReflectionException;
use ReflectionFunction;
use ReflectionParameter;
use RuntimeException;
use Smarty;

/**
 * Plugin base class to inherit other plugins from.
 *
 * @author <NAME> <<EMAIL>>
 */
abstract class BasePlugin
{
    /**
     * @var Container
     */
    protected $container;

    /**
     * Name of the plugin
     *
     * @var string
     */
    protected $name;

    /**
     * Path to the plugin file.
     *
     * @var string
     */
    private $path;

    public function __construct(Container $container, $path, $name)
    {
        $this->container = $container;
        $this->path      = $path;
        $this->name      = $name;
    }

    /**
     * Register the plugin with the Smarty instance.
     */
    abstract public function register(Smarty $smarty);

    protected function loadPlugin()
    {
        require_once $this->path;
    }

    /**
     * Resolve the parameters for the plugin, as Mako does not appear to handle
     * the resolution of reference and typed parameters very well.
     *
     * @param string $functionName
     * @param array $params
     * @return mixed Whatever the function returns.
     */
    protected function callWithParameters($functionName, array $params)
    {
        $function = new ReflectionFunction($functionName);

        $functionParameters = [];

        foreach ($function->getParameters() as $parameter) {
            $name = $this->resolveParameter($parameter, $params,
                $functionParameters);
        }

        return $function->invokeArgs($functionParameters);
    }

    private function resolveParameter(ReflectionParameter $parameter,
                                      array $provided,
                                      array &$functionParameters)
    {
        $name = $parameter->getName();
        if (array_key_exists($name, $provided)) {
            if ($parameter->isPassedByReference()) {
                $functionParameters[] = &$provided[$name];
            } else {
                $functionParameters[] = $provided[$name];
            }
        } else {
            $className = $parameter->getClass()->getName();
            try {
                $functionParameters[] = $this->container->get($className);
            } catch (ReflectionException $ex) {
                throw new RuntimeException("Unable to resolve parameter $name, typehint $className.",
                0, $ex);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.11040255081705859, 'fraction_numerical': 0.00039856516540454366, 'mean_word_length': 1.9804038004750595, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4981136', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1245, 'n_words': 344}