minor #16761 [Form] Cleanup (nicolas-grekas)

This PR was merged into the 2.8 branch.

Discussion
----------

[Form] Cleanup

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

ping @stof

Commits
-------

fd8e882 [Form] Cleanup
This commit is contained in:
Nicolas Grekas 2015-11-30 17:33:00 +01:00
commit 6df8badbe1
2 changed files with 32 additions and 33 deletions

View File

@ -62,48 +62,29 @@ class FormFactory implements FormFactoryInterface
public function createBuilder($type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = array())
{
$name = null;
$typeName = null;
if ($type instanceof ResolvedFormTypeInterface) {
if (method_exists($type, 'getBlockPrefix')) {
// As of Symfony 3.0, the block prefix of the type is used as
// default name
$name = $type->getBlockPrefix();
} else {
// BC
$typeName = $type->getName();
}
$typeObject = $type;
} elseif ($type instanceof FormTypeInterface) {
if (method_exists($type, 'getBlockPrefix')) {
// As of Symfony 3.0, the block prefix of the type is used as
// default name
$name = $type->getBlockPrefix();
} else {
// BC
$typeName = $type->getName();
}
$typeObject = $type;
} elseif (is_string($type)) {
$resolvedType = $this->registry->getType($type);
if (method_exists($resolvedType, 'getBlockPrefix')) {
// As of Symfony 3.0, the block prefix of the type is used as
// default name
$name = $resolvedType->getBlockPrefix();
} else {
// BC
$typeName = $type;
}
$typeObject = $this->registry->getType($type);
$name = $type;
} else {
throw new UnexpectedTypeException($type, 'string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface');
}
// BC when there is no block prefix
if (null === $name) {
if (false === strpos($typeName, '\\')) {
// No FQCN - leave unchanged for BC
$name = $typeName;
} else {
if (method_exists($typeObject, 'getBlockPrefix')) {
// As of Symfony 3.0, the block prefix of the type is used as default name
$name = $typeObject->getBlockPrefix();
} else {
// BC when there is no block prefix
if (null === $name) {
$name = $typeObject->getName();
}
if (false !== strpos($name, '\\')) {
// FQCN
$name = StringUtil::fqcnToBlockPrefix($typeName);
$name = StringUtil::fqcnToBlockPrefix($name);
}
}

View File

@ -341,6 +341,9 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
$this->assertSame('FORM', $this->factory->create('TYPE', null, $options));
}
/**
* @group legacy
*/
public function testCreateUsesTypeNameIfTypeGivenAsString()
{
$options = array('a' => '1', 'b' => '2');
@ -372,6 +375,9 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
$this->assertSame('FORM', $this->factory->create('TYPE', null, $options));
}
/**
* @group legacy
*/
public function testCreateStripsNamespaceOffTypeName()
{
$options = array('a' => '1', 'b' => '2');
@ -403,6 +409,9 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
$this->assertSame('FORM', $this->factory->create('Vendor\Name\Space\UserForm', null, $options));
}
/**
* @group legacy
*/
public function testLegacyCreateStripsNamespaceOffTypeNameAccessByFQCN()
{
$options = array('a' => '1', 'b' => '2');
@ -434,6 +443,9 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
$this->assertSame('FORM', $this->factory->create('userform', null, $options));
}
/**
* @group legacy
*/
public function testCreateStripsTypeSuffixOffTypeName()
{
$options = array('a' => '1', 'b' => '2');
@ -465,6 +477,9 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
$this->assertSame('FORM', $this->factory->create('Vendor\Name\Space\UserType', null, $options));
}
/**
* @group legacy
*/
public function testCreateDoesNotStripTypeSuffixIfResultEmpty()
{
$options = array('a' => '1', 'b' => '2');
@ -496,6 +511,9 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
$this->assertSame('FORM', $this->factory->create('Vendor\Name\Space\Type', null, $options));
}
/**
* @group legacy
*/
public function testCreateConvertsTypeToUnderscoreSyntax()
{
$options = array('a' => '1', 'b' => '2');