<commit_before><?php

namespace LAG\AdminBundle\Factory;

use Exception;
use LAG\AdminBundle\Admin\Exception\AdminException;
use LAG\AdminBundle\Configuration\ActionConfiguration;
use LAG\AdminBundle\Configuration\ApplicationConfiguration;
use LAG\AdminBundle\Configuration\ApplicationConfigurationStorage;
use LAG\AdminBundle\Field\FieldInterface;
use LAG\AdminBundle\Field\TwigAwareFieldInterface;
use LAG\AdminBundle\Field\TwigAwareInterface;
use LAG\AdminBundle\Field\TranslatorAwareFieldInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Twig_Environment;

/**
 * Field factory. Instances fields.
 */
class FieldFactory
{
    /**
     * Application configuration
     *
     * @var ApplicationConfiguration
     */
    protected $configuration;

    /**
     * Field class mapping array, indexed by field type.
     *
     * @var array
     */
    protected $fieldsMapping = [];

    /**
     * Translator for field values.
     *
     * @var TranslatorInterface
     */
    protected $translator;

    /**
     * Twig engine.
     *
     * @var Twig_Environment
     */
    protected $twig;
    
    /**
     * @var ConfigurationFactory
     */
    protected $configurationFactory;
    
    /**
     * FieldFactory constructor.
     *
     * @param ApplicationConfigurationStorage                     $applicationConfigurationStorage
     * @param ConfigurationFactory $configurationFactory
     * @param TranslatorInterface                                 $translator
     * @param Twig_Environment                                    $twig
     */
    public function __construct(
        ApplicationConfigurationStorage $applicationConfigurationStorage,
        ConfigurationFactory $configurationFactory,
        TranslatorInterface $translator,
        Twig_Environment $twig
    ) {
        $this->configuration = $applicationConfigurationStorage->getConfiguration();
        $this->fieldsMapping = $this
            ->configuration
            ->getParameter('fields_mapping'); // shortcut to field mapping array
        $this->translator = $translator;
        $this->twig = $twig;
        $this->configurationFactory = $configurationFactory;
    }
    
    /**
     * @param ActionConfiguration $configuration
     *
     * @return array
     */
    public function getFields(ActionConfiguration $configuration)
    {
        $fields = [];
    
        foreach ($configuration->getParameter('fields') as $field => $fieldConfiguration) {
            $fields[] = $this->create($field, $fieldConfiguration, $configuration);
        }
    
        return $fields;
    }
    
    
    /**
     * Create a new field instance according to the given configuration.
     *
     * @param string              $name
     * @param array               $configuration
     * @param ActionConfiguration $actionConfiguration
     *
     * @return FieldInterface
     *
     * @throws Exception
     */
    public function create($name, array $configuration, ActionConfiguration $actionConfiguration)
    {
        $resolver = new OptionsResolver();
        $configuration = $this->resolveTopLevelConfiguration($configuration, $actionConfiguration);

        $field = $this->instanciateField($name, $configuration['type']);
        $field->configureOptions($resolver, $actionConfiguration);

        try {
            $field->setOptions($resolver->resolve($configuration['options']));
        } catch (Exception $exception) {
            throw new \LAG\AdminBundle\Exception\Exception(
                'An error has occurred when resolving the options for the field "'.$name.'": '.$exception->getMessage(),
                $exception->getCode(),
                $exception
            );
        }

        return $field;
    }

    /**
     * Return field class according to the field type. If the type is not present in the field mapping array, an
     * exception will be thrown.
     *
     * @param $type
     * @return string
     * @throws Exception
     */
    private function getFieldClass($type)
    {
        if (!array_key_exists($type, $this->fieldsMapping)) {
            throw new Exception("Field type {$type} not found in field mapping. Check your configuration");
        }

        return $this->fieldsMapping[$type];
    }
    
    /**
     * @param $name
     * @param $type
     *
     * @return FieldInterface
     *
     * @throws Exception
     */
    private function instanciateField($name, $type)
    {
        $fieldClass = $this->getFieldClass($type);
        $field = new $fieldClass($name);
    
        if (!$field instanceof FieldInterface) {
            throw new Exception("Field class {$fieldClass} must implements ".FieldInterface::class);
        }
    
        if ($field instanceof TranslatorAwareFieldInterface) {
            $field->setTranslator($this->translator);
        }
        if ($field instanceof TwigAwareFieldInterface) {
            $field->setTwig($this->twig);
        }
    
        return $field;
    }<commit_msg>Fix invalid phpdoc comments and invalid imports<commit_after><?php

namespace LAG\AdminBundle\Factory;

use Exception;
use LAG\AdminBundle\Configuration\ActionConfiguration;
use LAG\AdminBundle\Configuration\ApplicationConfiguration;
use LAG\AdminBundle\Configuration\ApplicationConfigurationStorage;
use LAG\AdminBundle\Field\FieldInterface;
use LAG\AdminBundle\Field\TwigAwareFieldInterface;
use LAG\AdminBundle\Field\TranslatorAwareFieldInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Twig_Environment;

/**
 * Field factory. Instances fields.
 */
class FieldFactory
{
    /**
     * Application configuration
     *
     * @var ApplicationConfiguration
     */
    protected $configuration;

    /**
     * Field class mapping array, indexed by field type.
     *
     * @var array
     */
    protected $fieldsMapping = [];

    /**
     * Translator for field values.
     *
     * @var TranslatorInterface
     */
    protected $translator;

    /**
     * Twig engine.
     *
     * @var Twig_Environment
     */
    protected $twig;
    
    /**
     * @var ConfigurationFactory
     */
    protected $configurationFactory;
    
    /**
     * FieldFactory constructor.
     *
     * @param ApplicationConfigurationStorage $applicationConfigurationStorage
     * @param ConfigurationFactory            $configurationFactory
     * @param TranslatorInterface             $translator
     * @param Twig_Environment                $twig
     */
    public function __construct(
        ApplicationConfigurationStorage $applicationConfigurationStorage,
        ConfigurationFactory $configurationFactory,
        TranslatorInterface $translator,
        Twig_Environment $twig
    ) {
        $this->configuration = $applicationConfigurationStorage->getConfiguration();
        $this->fieldsMapping = $this
            ->configuration
            ->getParameter('fields_mapping'); // shortcut to field mapping array
        $this->translator = $translator;
        $this->twig = $twig;
        $this->configurationFactory = $configurationFactory;
    }
    
    /**
     * @param ActionConfiguration $configuration
     *
     * @return array
     */
    public function getFields(ActionConfiguration $configuration): array
    {
        $fields = [];
    
        foreach ($configuration->getParameter('fields') as $field => $fieldConfiguration) {
            $fields[] = $this->create($field, $fieldConfiguration, $configuration);
        }
    
        return $fields;
    }
    
    
    /**
     * Create a new field instance according to the given configuration.
     *
     * @param string              $name
     * @param array               $configuration
     * @param ActionConfiguration $actionConfiguration
     *
     * @return FieldInterface
     *
     * @throws Exception
     */
    public function create(string $name, array $configuration, ActionConfiguration $actionConfiguration): FieldInterface
    {
        $resolver = new OptionsResolver();
        $configuration = $this->resolveTopLevelConfiguration($configuration, $actionConfiguration);

        $field = $this->instanciateField($name, $configuration['type']);
        $field->configureOptions($resolver, $actionConfiguration);

        try {
            $field->setOptions($resolver->resolve($configuration['options']));
        } catch (Exception $exception) {
            throw new \LAG\AdminBundle\Exception\Exception(
                'An error has occurred when resolving the options for the field "'.$name.'": '.$exception->getMessage(),
                $exception->getCode(),
                $exception
            );
        }

        return $field;
    }

    /**
     * Return field class according to the field type. If the type is not present in the field mapping array, an
     * exception will be thrown.
     *
     * @param string $type
     *
     * @return string
     *
     * @throws Exception
     */
    private function getFieldClass(string $type): string
    {
        if (!array_key_exists($type, $this->fieldsMapping)) {
            throw new Exception("Field type {$type} not found in field mapping. Check your configuration");
        }

        return $this->fieldsMapping[$type];
    }
    
    /**
     * @param string $name
     * @param string $type
     *
     * @return FieldInterface
     *
     * @throws Exception
     */
    private function instanciateField(string $name, string $type): FieldInterface
    {
        $fieldClass = $this->getFieldClass($type);
        $field = new $fieldClass($name);
    
        if (!$field instanceof FieldInterface) {
            throw new Exception("Field class {$fieldClass} must implements ".FieldInterface::class);
        }
    
        if ($field instanceof TranslatorAwareFieldInterface) {
            $field->setTranslator($this->translator);
        }
        if ($field instanceof TwigAwareFieldInterface) {
            $field->setTwig($this->twig);
        }
    
        return $field;
    }
{'fraction_non_alphanumeric': 0.1016159413105978, 'fraction_numerical': 0.0, 'mean_word_length': 2.4035087719298245, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5899903', 'n_tokens_mistral': 2509, 'n_tokens_neox': 2475, 'n_words': 593}