nelmio/NelmioApiDocBundle — pull request #387 by giosh94mhz (2014-05-21 11:02:32+00:00)
Title: FormTypeParser: FormType constructor should be called when possible

I've found a problem with the FormTypeParser. When generating FormTypes used for the doc generation, the type constructor is not called.

By looking to the code, I've found that the FormType class is created from the outer space with:
```php
private function getTypeInstance($type)
{
return unserialize(sprintf('O:%d:"%s":0:{}', strlen($type), $type));
}
```
IMHO, this approach is correct if, and only if, no method of that class are called.
Consider the following simple type, with subscribers support:
```php
subscribers = array();
}

public function addSubscriber(EventSubscriberInterface $subscriber)
{
$this->subscribers[] = $subscriber;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('many_things');
foreach ($this->subscribers as $subscriber) {
$builder->addEventSubscriber($subscriber);
}
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array());
}

public function getName()
{
return 'acme_atweekabletype';
}
}
```
By parsing the type a notice is triggered since the foreach loop expects the subscriber array to be initialized. Obviously this is a simple example, but more bugs situation may arise.

With this patch, the constructor is called if available and has no required parameters. Otherwise, it try to fallback and create a new instance without calling the constructor as a fallback (and if it fails, then the developer should really consider using DI).