<commit_before><?php
declare(strict_types=1);

namespace Selami\View\Twig\Functions;

use Selami\View\FunctionInterface;
use Selami\Stdlib\CaseConverter;
use BadMethodCallException;
use InvalidArgumentException;

use Twig\Environment;

class Widget implements FunctionInterface
{
    private $twig;
    private $config;
    private $templateFile;
    private $widgetData;

    public function __construct(Environment $twig, array $config, string $widgetNameStr, string $widgetActionStr, array $args)
    {

        $this->twig = $twig;
        $this->config = $config;
        $widgetAction = CaseConverter::toPascalCase($widgetActionStr);
        $widgetName =  CaseConverter::toPascalCase($widgetNameStr);
        $widget = '\\' . $this->config['app_namespace'] . '\\Widget\\' . $widgetName;
        $this->checkClassIfExists($widget, $widgetName, $widgetAction);
        $widgetInstance = new $widget($args);
        $templateFileBasename = $args['template'] ?? CaseConverter::toSnakeCase($widgetActionStr) . '.twig';
        $this->checkIdTemplateFileExist($templateFileBasename, $widgetNameStr, $widgetActionStr);
        $this->templateFile =  '_widgets/'
            . CaseConverter::toSnakeCase($widgetNameStr) . '/' . $templateFileBasename;
        $this->widgetData = $widgetInstance->{$widgetAction}();
    }

    private function checkClassIfExists($widget, $widgetName, $widgetAction)
    {
        if (!class_exists($widget)) {
            $message = 'Widget ' . $widgetName . '_' . $widgetAction . ' has not class name as ' . $widget;
            throw new BadMethodCallException($message);
        }
        if (!method_exists($widget, $widgetAction)) {
            $message = 'Widget ' . $widget . ' has not method name as ' . $widgetAction;
            throw new BadMethodCallException($message);
        }
    }

    private function checkIdTemplateFileExist(
        string $templateFileBasename,
        string $widgetNameStr,
        string $widgetActionStr
    ) {
        $templateFullPath = $this->config['templates_dir'] . '/_widgets/'
            . CaseConverter::toSnakeCase($widgetNameStr) . '/' . $templateFileBasename;
        if (!file_exists($templateFullPath)) {
            $message = sprintf(
                '%s  template file not found! %s needs a main template file at: %s',
                $templateFileBasename,
                $widgetNameStr . '_' . $widgetActionStr,
                $templateFullPath
            );
            throw new InvalidArgumentException($message);
        }
    }

    public function run() : string
    {
        return $this->twig->render($this->templateFile, $this->widgetData);

    }
}<commit_msg>CS fixes [skip-ci]<commit_after><?php
declare(strict_types=1);

namespace Selami\View\Twig\Functions;

use Selami\View\FunctionInterface;
use Selami\Stdlib\CaseConverter;
use BadMethodCallException;
use InvalidArgumentException;

use Twig\Environment;

class Widget implements FunctionInterface
{
    private $twig;
    private $config;
    private $templateFile;
    private $widgetData;

    public function __construct(
        Environment $twig,
        array $config,
        string $widgetNameStr,
        string $widgetActionStr,
        array $args
    ) {
        $this->twig = $twig;
        $this->config = $config;
        $widgetAction = CaseConverter::toPascalCase($widgetActionStr);
        $widgetName =  CaseConverter::toPascalCase($widgetNameStr);
        $widget = '\\' . $this->config['app_namespace'] . '\\Widget\\' . $widgetName;
        $this->checkClassIfExists($widget, $widgetName, $widgetAction);
        $widgetInstance = new $widget($args);
        $templateFileBasename = $args['template'] ?? CaseConverter::toSnakeCase($widgetActionStr) . '.twig';
        $this->checkIdTemplateFileExist($templateFileBasename, $widgetNameStr, $widgetActionStr);
        $this->templateFile =  '_widgets/'
            . CaseConverter::toSnakeCase($widgetNameStr) . '/' . $templateFileBasename;
        $this->widgetData = $widgetInstance->{$widgetAction}();
    }

    private function checkClassIfExists($widget, $widgetName, $widgetAction)
    {
        if (!class_exists($widget)) {
            $message = 'Widget ' . $widgetName . '_' . $widgetAction . ' has not class name as ' . $widget;
            throw new BadMethodCallException($message);
        }
        if (!method_exists($widget, $widgetAction)) {
            $message = 'Widget ' . $widget . ' has not method name as ' . $widgetAction;
            throw new BadMethodCallException($message);
        }
    }

    private function checkIdTemplateFileExist(
        string $templateFileBasename,
        string $widgetNameStr,
        string $widgetActionStr
    ) {
        $templateFullPath = $this->config['templates_dir'] . '/_widgets/'
            . CaseConverter::toSnakeCase($widgetNameStr) . '/' . $templateFileBasename;
        if (!file_exists($templateFullPath)) {
            $message = sprintf(
                '%s  template file not found! %s needs a main template file at: %s',
                $templateFileBasename,
                $widgetNameStr . '_' . $widgetActionStr,
                $templateFullPath
            );
            throw new InvalidArgumentException($message);
        }
    }

    public function run() : string
    {
        return $this->twig->render($this->templateFile, $this->widgetData);
    }
}

{'fraction_non_alphanumeric': 0.12453600593912398, 'fraction_numerical': 0.0003711952487008166, 'mean_word_length': 2.7711686494051784, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '2242602', 'n_tokens_mistral': 1500, 'n_tokens_neox': 1473, 'n_words': 301}