<commit_before><?php

namespace Knp\RadBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Knp\RadBundle\Finder\ClassFinder;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Definition;
use Knp\RadBundle\DependencyInjection\Definition\FormTypeDefinitionFactory;
use Knp\RadBundle\DependencyInjection\ServiceIdGenerator;

class RegisterFormTypesPass implements CompilerPassInterface
{
    private $bundle;
    private $classFinder;
    private $definitionFactory;
    private $serviceIdGenerator;

    public function __construct(BundleInterface $bundle, ClassFinder $classFinder = null, FormTypeDefinitionFactory $definitionFactory = null, ServiceIdGenerator $serviceIdGenerator = null)
    {
        $this->bundle = $bundle;
        $this->classFinder = $classFinder ?: new ClassFinder();
        $this->definitionFactory = $definitionFactory ?: new FormTypeDefinitionFactory;
        $this->serviceIdGenerator = $serviceIdGenerator ?: new ServiceIdGenerator;
    }

    /**
     * {@inheritDoc}
     */
    public function process(ContainerBuilder $container)
    {
        if (!$container->getParameter('knp_rad.detect.form_type')) {
            return;
        }

        if (false === $container->hasDefinition('form.extension')) {
            return;
        }

        $directory = $this->bundle->getPath().'/Form';
        $namespace = $this->bundle->getNamespace().'\\Form';

        $types   = $container->getDefinition('form.extension')->getArgument(1);
        $potentialClasses = $this->classFinder->findClassesMatching($directory, $namespace, 'Type$');
        $classes = $this->classFinder->filterClassesImplementing($potentialClasses, 'Symfony\Component\Form\FormTypeInterface');

        foreach ($classes as $class) {
            $id = $this->serviceIdGenerator->generateForBundleClass($this->bundle, $class);
            $alias = $this->getAlias($class, $id);

            if ($container->hasDefinition($id)) {
                continue;
            }

            $definition = $this->definitionFactory->createDefinition($class);
            $container->setDefinition($id, $definition);

            $types[$alias] = $id;
        }

        $container->getDefinition('form.extension')->replaceArgument(1, $types);
    }

    private function getAlias($class, $default)
    {
        if (!class_exists($class)) {
            return $default;
        }

        try {
            return unserialize(sprintf('O:%d:"%s":0:{}', strlen($class), $class))->getName();
        } catch (\Exception $e) {
        }<commit_msg>Fix abstract form type loading<commit_after><?php

namespace Knp\RadBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Knp\RadBundle\Finder\ClassFinder;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Definition;
use Knp\RadBundle\DependencyInjection\Definition\FormTypeDefinitionFactory;
use Knp\RadBundle\DependencyInjection\ServiceIdGenerator;

class RegisterFormTypesPass implements CompilerPassInterface
{
    private $bundle;
    private $classFinder;
    private $definitionFactory;
    private $serviceIdGenerator;

    public function __construct(BundleInterface $bundle, ClassFinder $classFinder = null, FormTypeDefinitionFactory $definitionFactory = null, ServiceIdGenerator $serviceIdGenerator = null)
    {
        $this->bundle = $bundle;
        $this->classFinder = $classFinder ?: new ClassFinder();
        $this->definitionFactory = $definitionFactory ?: new FormTypeDefinitionFactory;
        $this->serviceIdGenerator = $serviceIdGenerator ?: new ServiceIdGenerator;
    }

    /**
     * {@inheritDoc}
     */
    public function process(ContainerBuilder $container)
    {
        if (!$container->getParameter('knp_rad.detect.form_type')) {
            return;
        }

        if (false === $container->hasDefinition('form.extension')) {
            return;
        }

        $directory = $this->bundle->getPath().'/Form';
        $namespace = $this->bundle->getNamespace().'\\Form';

        $types   = $container->getDefinition('form.extension')->getArgument(1);
        $potentialClasses = $this->classFinder->findClassesMatching($directory, $namespace, 'Type$');
        $classes = $this->classFinder->filterClassesImplementing($potentialClasses, 'Symfony\Component\Form\FormTypeInterface');

        foreach ($classes as $class) {
            if ((new \ReflectionClass($class))->isAbstract()) {
                continue;
            }

            $id = $this->serviceIdGenerator->generateForBundleClass($this->bundle, $class);
            $alias = $this->getAlias($class, $id);

            if ($container->hasDefinition($id)) {
                continue;
            }

            $definition = $this->definitionFactory->createDefinition($class);
            $container->setDefinition($id, $definition);

            $types[$alias] = $id;
        }

        $container->getDefinition('form.extension')->replaceArgument(1, $types);
    }

    private function getAlias($class, $default)
    {
        if (!class_exists($class)) {
            return $default;
        }

        try {
            return unserialize(sprintf('O:%d:"%s":0:{}', strlen($class), $class))->getName();
        } catch (\Exception $e) {
        }
{'fraction_non_alphanumeric': 0.12951807228915663, 'fraction_numerical': 0.0010630758327427356, 'mean_word_length': 3.2960426179604263, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 58, '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': '3829782', 'n_tokens_mistral': 1472, 'n_tokens_neox': 1464, 'n_words': 248}